简体   繁体   English

测试ANTLR语法

[英]Testing ANTLR Grammar

So I've been making a grammar in Eclipse with ANTLR v3.4 and I've made one that works and I want to make sure when I edit it everything still works. 所以我一直在使用ANTLR v3.4在Eclipse中制作语法,我已经制作了一个可行的语法,我想确保在编辑它时一切仍然有效。 I can go into the interpretter everytime but that seems like a huge waste of time. 我每次都可以进入解释器,但这似乎是浪费时间。

Questions: I've read about gunit but the link it gives to download gUnit: ( http://antlr.org/hudson/job/gUnit/org.antlr $gunit/lastSuccessfulBuild/ ) doesn't work. 问题:我已经阅读了关于gunit但是下载gUnit的链接:( http://antlr.org/hudson/job/gUnit/org.antlr $ gunit / lastSuccessfulBuild /)不起作用。 How can I get gUnit. 我怎样才能获得gUnit。 What is the best way to test grammars? 测试语法的最佳方法是什么? Is it actually gUnit or should I just do java tests like jUnit tests? 它实际上是gUnit还是我应该像jUnit测试一样进行java测试?

I recently completed two ANTLR3 assignments (I'm working on my Master's in Computer Science) using Eclipse. 我最近使用Eclipse完成了两项ANTLR3任务(我正在攻读计算机科学硕士学位)。 I found no single document that had a process for installing, configuring, writing, and debugging a grammar in Eclipse. 我发现没有一个文档有一个在Eclipse中安装,配置,编写和调试语法的过程。 So, after working through various issues, I found the easiest thing to do was to stay in Eclipse for testing. 因此,在解决了各种问题之后,我发现最简单的事情就是留在Eclipse中进行测试。

To use the process I have come to use (outlined below) you must first have the ANTLR IDE v2.1.2 installed. 要使用我开始使用的过程(如下所述),您必须先安装ANTLR IDE v2.1.2。 Add it right from inside Eclipse Indigo: http://antlrv3ide.sourceforge.net/updates . 从Eclipse Indigo中添加它: http//antlrv3ide.sourceforge.net/updates This site also has some useful doc on using the ANTLR IDE. 该站点还有一些关于使用ANTLR IDE的有用文档。 Once installed, the IDE has to be configured. 安装后,必须配置IDE。 Video tutorials are a bit out of date but helpful. 视频教程有点过时但很有帮助。 See a detailed how to guide on configuring ANTLR IDE in Eclipse . 查看有关如何在Eclipse中配置ANTLR IDE的详细指南。 The main configuration item is the java output folder. 主要配置项是java输出文件夹。 Do this in Eclipse by going to Windows, Preferences, ANTLR, Code Generator, check Project relative folder and in the Output folder name box type a folder name (mine is called "antlr-java", others use "generated"). 在Eclipse中执行此操作,方法是转到Windows,首选项,ANTLR,代码生成器,检查项目相关文件夹,然后在输出文件夹名称框中键入文件夹名称(我的名称为“antlr-java”,其他人使用“generated”)。

Test/Debug Process for ANTLR in Eclipse Indigo with ANTLR IDE 使用ANTLR IDE在Eclipse Indigo中测试/调试ANTLR的过程

  1. After a new project is created, right-click it, select Configure, Convert to ANTLR Project... 创建新项目后,右键单击它,选择配置,转换为ANTLR项目...
  2. Create the grammar in a .g file and save it. 在.g文件中创建语法并保存。 Note: filename has to match grammar name. 注意:filename必须匹配语法名称。
  3. If there are significant errors, debug the grammar. 如果存在重大错误,请调试语法。 Eclipse shows the ANTLR error(s) and what line(s) are affected. Eclipse显示ANTLR错误以及受影响的行。 At first, these errors seem hard to understand but they can be worked through by using various resources: - The Definitive ANTLR Reference by Terence Parr the guy who wrote ANTLR - the ANTLR Reference Manual - google the error; 起初,这些错误似乎很难理解,但它们可以通过使用各种资源来解决: - Terence Parr的最终ANTLR参考编写ANTLR的人 - ANTLR参考手册 - 谷歌错误; many times you will end up here at stackoverflow; 很多时候你会在stackoverflow结束这里; in particular, Bart Kiers is both knowledgeable and helpful (Bart: thx for the help you didn't know you gave me) 特别是Bart Kiers既知识渊博又乐于助人(Bart:thx因为你不知道你给我的帮助)
  4. On the first save after the serious ANTLR errors are resolved, the java output folder you configured in Eclipse will be created and a java file in that folder will also be created. 在解决严重的ANTLR错误后的第一次保存中,将创建在Eclipse中配置的java输出文件夹,并且还将创建该文件夹中的java文件。
  5. Right-click on the java output folder, select Build Path, Use As a Source Folder. 右键单击java输出文件夹,选择Build Path,Use As a Source Folder。 This tells Eclipse where to look for the project's java source. 这告诉Eclipse在哪里查找项目的java源代码。
  6. There are likely to be errors in the new java file. 新的java文件中可能存在错误。 Select it, then search through looking for java errors. 选择它,然后搜索查找java错误。 Go back to your grammar or java file(s), correct the errors, and re-save the grammar until both grammar and java files are error free, then run it. 回到你的语法或java文件,纠正错误,并重新保存语法,直到语法和java文件都没有错误,然后运行它。
  7. From this point on, it's the usual modify-run-debug cycle. 从现在开始,这是通常的修改运行调试周期。
  8. The only other Eclipse change I needed was to create a few Run Configurations for testing command line parameters. 我需要的唯一其他Eclipse更改是创建一些运行配置来测试命令行参数。

The question is old, but I'm leaving a reference for completeness: 问题是陈旧的,但我要留下完整性的参考:

For me, the gUnit was useless. 对我来说,gUnit毫无用处。 So I managed to find how test only the Lexer and then, only the parser. 所以我设法找到了如何只测试Lexer然后只测试解析器。

I answered it here: https://stackoverflow.com/a/53884851/976948 我在这里回答: https//stackoverflow.com/a/53884851/976948

Basically, there are links for 2 articles about how to test it: 基本上,有2篇文章的链接有关如何测试它:

You can download gUnit there but I think there is no latest version... 你可以在那里下载gUnit 但我认为没有最新版本......

Try Jarvana ... Latest version there is 3.4: http://repo1.maven.org/maven2/org/antlr/gunit/3.4/gunit-3.4.jar 试试Jarvana ...最新版本有3.4: http//repo1.maven.org/maven2/org/antlr/gunit/3.4/gunit-3.4.jar

@Dave Newton is right. @Dave Newton是对的。 As of ANTLR v3.1, gUnit is included in the main ANTLR Tool jar as is stated there . 由于ANTLR V3.1中,gUnit包括在ANTLR工具主要水瓶中陈述存在


I didn't know for gUnit till now. 到目前为止我还不知道gUnit。 It looks great for grammar testing, but I think that JUnit tests will do their job to... 它看起来很适合语法测试,但我认为JUnit测试将完成他们的工作......

This is the first time I heard of gUinit and read up on it. 这是我第一次听说gUinit并阅读它。 (I don't use ANTLR much.) It sounds interesting, but half useless. (我不太使用ANTLR。)听起来很有趣,但是一半没用。

My approach to validating grammars is to actually validate the the entire parser with "normal" unit tests. 我验证语法的方法是用“正常”单元测试来实际验证整个解析器。 The thing is, you should have unit tests in place anyway and the tests that check for grammar regression you just add it there. 问题是,无论如何你都应该进行单元测试,检查语法回归的测试只需将其添加到那里。 The thing is in my experience that most errors come in semantic analysis and reduction and not the grammar. 根据我的经验,大多数错误都来自语义分析和缩减,而不是语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM