Reputation: 13450
I work with Intellij IDEA /w Antlr plugin.
Let's say I write a file named mylang.abc
. This is a sample on how the final lang will look like. I have then to write the .g4 file with the grammar for this language.
However I want to test that the grammar I write is correct. This is done by antlr Preview but as the language grows I would need to test each rule with my ideal source code and chunk down the trees.
An ideal test should probably have the logic: This is the source code, (this is the rule which this line should be parsed with-optional), this should be the ideally parsed tree -> matches the test? print Ok/Oops.
Is my approach correct on Grammar's testing? If so what can be an example to the above?
Upvotes: 2
Views: 1627
Reputation: 5962
For ANTLR v3, we created gUnit, analogous to jUnit. In the end, I decided that it was just as easy to have a series of methods like this:
assertTreeEquals("a=b;", "expr", "(expr a = b)");
That is one way to isolate what is wrong at a fine-grained detail with your grammar. Another approach is to simply have lots and lots of full inputs that cover all possible phrases you care about. You can then have a text file with the serialized tree to compare against the tree created by the parser. Once you are happy with these trees, they can easily detect regressions when you tweak your grammar.
On the other hand, I'm contemplating creating an automated mechanism within the Intellij plug-in to create unit tests associated with every rule. In other words, once you have a large input that you are sure has the right parse tree, you would click a button and the tool would automatically create a series of unit tests, one for each phrase/rule pair. I'm not yet convinced that it's more useful than just having a directory full of input and output errors, but I will investigate.
Upvotes: 3