[antlr-interest] Tree re-write does not create "new" tree nodes in JAVA target.

Ramanand Mandayam ramanand_mandayam at yahoo.com
Thu Aug 14 07:12:09 PDT 2008


I am using tree grammar T1 to process my AST
and possibly rewrite by adding a few tree nodes.
I then use grammar T2 to traverse this modified 
output from T1. 

In the examples below, even though the semantic predicate
in T1 is satisfied and the JAVA code does appear
to execute the API calls to add new nodes, 
the tree walker in T2 never sees the new node
since I only ever see the output "No change".

In T2, if I remove the rule alternative that 
does not contain the new/imaginary node then
T2 fails with a mismatched node error.

I am only at an advanced beginner level in JAVA 
so I may be missing something that is required
to make the tree persistent across grammar/tree
walk invocations.

Cheers

Ramanand


/* START TREE GRAMMAR */
tree grammar T1;
option {
    tokenVocab=myParser; ASTLabelType = myTreeNode; output=AST;
    rewrite=true;
}
tokens { IMAGINARY_TOKEN; }

A
@init{ boolean addNode = false; }
:  B
   { addNode = <method-returning-boolean>; }
   ->{addNode}?^(IMAGINARY_TOKEN B);
/* END GRAMMAR T1 */

/* START GRAMMAR T2 */
tree grammar T2;
option {
    tokenVocab=T1; ASTLabelType = myTreeNode; output=template;
}
A:  
    ^(IMAGINARY_TOKEN B) -> {%{"Node Added"}}  // Never executed.
  | B {%{"No change"}}
/* END GRAMMAR T2 */

I am using ANTLR v3.1.


      


More information about the antlr-interest mailing list