Search code examples
antlrgrammarabstract-syntax-treeantlrworks

ANTLR ignoring AST operators


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


Solution

  • 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