Background: I have created an ANTLR grammar. I am able to test and debug it with ANTLRWorks and have verified that the parser creates the AST that I had in my mind. Now, I want to write a tree grammar for the AST, parse the tree and debug the tree grammar with ANTLRWorks.
Question: I want to test and debug a tree grammar with ANTLRWorks. I thus want to parse the AST that has been created by the parser. How do I specify the AST as input when testing the tree grammar with ANTLRWorks?
P.S. I have studied the question / answer at Does anyone know of a way to debug tree grammars in ANTLRWorks but it doesn't answer my question. Although accepted by the OP, he made a similar comment.
You don't need to provide the AST yourself, only the parser that produces the AST.
Given the following grammar that produces an AST:
The following would be a tree grammar for the AST produced by grammar above:
Be sure to put both
ASTDemo.g
andASTDemoWalker.g
in the same folder. Open both grammars in ANTLRWorks and generate the lexer & parser fromASTDemo.g
first by pressing CTRL+SHIFT+G and then generate the tree walker by openingASTDemoWalker.g
and pressing CTRL+SHIFT+G.Now, from the
ASTDemoWalker.g
editor panel, fire up the debugger by pressing CTRL+D and paste the following source in the text area:and press OK.
You can now step through the debugging process and at the end, you can see both what AST the parser generated:
and how the tree walker walked over said AST:
If you now make an "accidental" mistake in the tree grammar, say, instead of
^('*' expression expression)
you define^('*' expression)
. If you debug the tree grammar again, you will see it fail after passing the42
node:In the AST there is another node after the
42
node, while the tree walker expected only 1 single node (42
) after the*
root node.Of course, this is an easy grammar, but even if you're familiar with ANTLR, it's sometimes a pain in the @$& to track down errors in a tree grammar! :)