Testing ANTLR Grammar

So, I am doing grammar in Eclipse with ANTLR v3.4, and I created one that works, and I want to make sure that when I edit it, everything works. I can deal with the interpreter every time, but it seems like a huge waste of time.

Questions: I read about gunit, but the link it gives to download gUnit: ( http://antlr.org/hudson/job/gUnit/org.antlr $ gunit / lastSuccessfulBuild /) does not work. How can I get gUnit. What is the best way to test grammars? Is it really gUnit or should I just run java tests like jUnit tests?

+6
source share
4 answers

I recently completed two ANTLR3 assignments (I'm working on my MSc in Computer Science) using Eclipse. I did not find a single document in which there was a process of installing, configuring, writing and debugging grammar in Eclipse. So, after working on various issues, I found that it was easiest to stay in Eclipse for testing.

To use this process that I used (described below), you must first install the ANTLR IDE v2.1.2. Add it directly from inside Eclipse Indigo: http://antlrv3ide.sourceforge.net/updates . This site also has a helpful document on using the ANTLR IDE. After installing the IDE you need to configure. Video tutorials are a bit outdated, but useful. See the Detailed Guide to Configuring the ANTLR IDE in Eclipse . The main configuration item is the java output folder. Do it in Eclipse, going to Windows, Preferences, ANTLR, Code Generator, check the relative Project folder, and in the "Output folder name" field, enter the name of the folder (mine is called "antlr-java", others use "generated").

Testing / Debugging Process for ANTLR in Eclipse Indigo with ANTLR IDE

  • After creating a new project, right-click it, select Configure, Convert to Project ANTLR ...
  • Create a grammar in a .g file and save it. Note: the file name must match the grammar name.
  • If there are significant errors, debug the grammar. Eclipse shows an ANTLR error and which lines are affected. At first, these errors seem difficult to understand, but they can be handled using various resources: - The final ANTLR Reference by Terence Parr, who wrote ANTLR - ANTLR Reference Manual - Google's bug; many times you get here in stackoverflow; in particular, Bart Kirs is knowledgeable and helpful (Bart: thanks for the help you did not know that you gave me)
  • At the first save, after serious ANTLR errors are resolved, the java output folder you configured in Eclipse will be created and a java file will be created in this folder.
  • Right-click on the java output folder, select "Build Path", "Use as Source Folder". This tells Eclipse where to look for the source of the java project.
  • There may be errors in the new java file. Select it, then search through for Java errors. Go back to your grammar or java files (s), correct the errors and save the grammar again until both the grammar and java files are error free and then run it.
  • From now on, this is the usual change-start-debug cycle.
  • The only other Eclipse change I needed was to create several Run Configurations to test command line options.
+5
source

You can download gUnit there , but I think there is no latest version ...

Try Jarvana ... Latest version 3.4: http://repo1.maven.org/maven2/org/antlr/gunit/3.4/gunit-3.4.jar

@ Dave Newton is right. Starting with ANTLR v3.1, gUnit is included in the main banner of the ANTLR tool, as indicated there .


I still did not know about gUnit. It works great for grammar testing, but I think JUnit tests will do the job ...

+1
source

This is the first time I heard about gUinit and read about it. (I don't use ANTLR a lot.) It sounds interesting, but half useless.

My approach to checking grammars is to actually test the entire parser using "normal" unit tests. The fact is that in any case, you should have unit tests, and the tests that check the regression of the grammar, you just add there. The fact is that most errors come in semantic analysis and reduction, and not in grammar.

0
source

The question is old, but I leave a link for completeness:

For me, this block was useless. Therefore, I managed to find how to test only Lexer, and then, only the parser.

I answered it here: fooobar.com/questions/11111121 / ...

Basically, there are links to 2 articles on how to check this:

0
source

Source: https://habr.com/ru/post/917120/


All Articles