2
votes

I am using the ^ and ! operators to set the root node and to not not be included in the AST, respectively. However, it is not making a difference in the tree that is generated by ANTLRWorks. So I am not sure if my grammar is incorrect or if ANTLRWorks just isn't creating a correct tree.

Here is an snippet of my grammar

expr
:   '('! logExpr ')'!;

These parenthesis should not be included in the AST.

addExpr
:   multExpr ( (PLUS|MINUS)^ multExpr )*;

The PLUS or MINUS should be the root node in the AST.

However neither of these things are happening the way I expect them to. It makes no difference when I remove them or put them back. ANTLRWorks 1.4.3 ANTLR 3.4

2
Where did you look in Antlrworks to come to your conclusion. I also suppose you told antlr to output an ast?stryba
@stryba The interpreter tab that shows a diagram of the tree. Or is this the parse tree rather than the AST?Austin Henley

2 Answers

4
votes

ANTLRWorks' interpreter shows the parse tree, even though you've put output=AST; in the grammar's options-block, as you already found yourself.

However, ANTLRWorks' debugger does show the AST. To activate the debugger, press CTL+D or select Run->Debug from the menu bar.

The debugger is able to visualize the parse tree and AST. Note that it will not handle embedded code other than Java.

enter image description here

2
votes

I found the answer. ANTLRWorks shows the parse tree, not the AST. It ignores all rewrites. This led to my second question which was, how do I visualize the AST if ANTLRWorks doesn't do it for me? The answer for that is found here.