Testing ANTLR Grammar
Asked Answered
A

4

7

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. 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. How can I get gUnit. What is the best way to test grammars? Is it actually gUnit or should I just do java tests like jUnit tests?

Ainslee answered 31/5, 2012 at 17:5 Comment(2)
gUnit has been included in the antlr tools for a few versions now, no?Giana
How do I access gUnit once I've downloaded the ANTLR-complete-3.4.jar?Ainslee
C
5

I recently completed two ANTLR3 assignments (I'm working on my Master's in Computer Science) using Eclipse. I found no single document that had a process for installing, configuring, writing, and debugging a grammar in Eclipse. So, after working through various issues, I found the easiest thing to do was to stay in Eclipse for testing.

To use the process I have come to use (outlined below) you must first have the ANTLR IDE v2.1.2 installed. Add it right from inside Eclipse Indigo: http://antlrv3ide.sourceforge.net/updates. This site also has some useful doc on using the ANTLR IDE. Once installed, the IDE has to be configured. Video tutorials are a bit out of date but helpful. See a detailed how to guide on configuring ANTLR IDE in Eclipse. The main configuration item is the java output folder. 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").

Test/Debug Process for ANTLR in Eclipse Indigo with ANTLR IDE

  1. After a new project is created, right-click it, select Configure, Convert to ANTLR Project...
  2. Create the grammar in a .g file and save it. Note: filename has to match grammar name.
  3. If there are significant errors, debug the grammar. Eclipse shows the ANTLR error(s) and what line(s) are affected. 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; many times you will end up here at stackoverflow; in particular, Bart Kiers is both knowledgeable and helpful (Bart: thx for the help you didn't know you gave me)
  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.
  5. Right-click on the java output folder, select Build Path, Use As a Source Folder. This tells Eclipse where to look for the project's java source.
  6. There are likely to be errors in the new java file. Select it, then search through looking for java errors. 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.
  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.
Cornuted answered 4/6, 2012 at 19:8 Comment(1)
Edited answer with embedded link.Cornuted
P
6

The question is old, but I'm leaving a reference for completeness:

For me, the gUnit was useless. So I managed to find how test only the Lexer and then, only the parser.

I answered it here: https://mcmap.net/q/1479864/-is-there-a-way-to-generate-unit-test-to-test-my-grammar

Basically, there are links for 2 articles about how to test it:

Pornocracy answered 21/12, 2018 at 12:39 Comment(1)
Thanks for the Lexer example! I converted it to Python here gist.github.com/nmz787/cf98aa465a4d071a937cf74788687a54Jennijennica
C
5

I recently completed two ANTLR3 assignments (I'm working on my Master's in Computer Science) using Eclipse. I found no single document that had a process for installing, configuring, writing, and debugging a grammar in Eclipse. So, after working through various issues, I found the easiest thing to do was to stay in Eclipse for testing.

To use the process I have come to use (outlined below) you must first have the ANTLR IDE v2.1.2 installed. Add it right from inside Eclipse Indigo: http://antlrv3ide.sourceforge.net/updates. This site also has some useful doc on using the ANTLR IDE. Once installed, the IDE has to be configured. Video tutorials are a bit out of date but helpful. See a detailed how to guide on configuring ANTLR IDE in Eclipse. The main configuration item is the java output folder. 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").

Test/Debug Process for ANTLR in Eclipse Indigo with ANTLR IDE

  1. After a new project is created, right-click it, select Configure, Convert to ANTLR Project...
  2. Create the grammar in a .g file and save it. Note: filename has to match grammar name.
  3. If there are significant errors, debug the grammar. Eclipse shows the ANTLR error(s) and what line(s) are affected. 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; many times you will end up here at stackoverflow; in particular, Bart Kiers is both knowledgeable and helpful (Bart: thx for the help you didn't know you gave me)
  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.
  5. Right-click on the java output folder, select Build Path, Use As a Source Folder. This tells Eclipse where to look for the project's java source.
  6. There are likely to be errors in the new java file. Select it, then search through looking for java errors. 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.
  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.
Cornuted answered 4/6, 2012 at 19:8 Comment(1)
Edited answer with embedded link.Cornuted
P
1

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

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

@Dave Newton is right. As of ANTLR v3.1, gUnit is included in the main ANTLR Tool jar as is stated there.


I didn't know for gUnit till now. It looks great for grammar testing, but I think that JUnit tests will do their job to...

Peripteral answered 31/5, 2012 at 17:18 Comment(0)
G
0

This is the first time I heard of gUinit and read up on it. (I don't use ANTLR much.) It sounds interesting, but half useless.

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.

Glairy answered 1/6, 2012 at 13:6 Comment(5)
Could you tell me more about that or a location you went to find your information?Ainslee
The info on gUnit was mostly a quick google on the term. The other is professional experience of 10 years building language parses and compilers. The thing is that the grammar will live in an application; unless you only make academic examples. The application probably already has unit tests, why not just use that. Especially since what use is that the grammar is valid but the remaining code makes nonsense.Glairy
The big reason is because its a big inconvinience to determine if its running (while building a grammar from scratch) and you have to go into the grammar windown to make sure everything you've done is working. Some automatic test code would automatically tell you what has broken every compile. We don't have an application as we are designing it all ourselves, so we have to make sure its working, there is no overarching project that will tell us if its working or not. I don't know if that explains much, just let me know :)Ainslee
Are you working on an academic project? It that me or does it sound useless to develop a grammar without some minimal application? Even a code validator is some form of application. You build a unit test that contains a small fragment of code to parse and check if the result is as expected; rinse and repeat.Glairy
The application with the biggest grammar to app ratio, that i ever wrote, was the EDDL to XML converter. (Don't aks, it dumps the AST as XML.) But that thing then feed into the PNO certification tool and PNO tokeniser. But even there EDDL in XML out. You could execute the just the parser of the entire conversion chain.Glairy

© 2022 - 2024 — McMap. All rights reserved.