[antlr-interest] Tree rewriting -- adding subtrees

Stephen Tuttlebee themightystephen at googlemail.com
Wed Apr 13 06:26:36 PDT 2011


Hello,

I'm working on a tree grammar that needs to perform some tree rewriting 
for a further tree parsing phase.

My question is, is it possible to do something like this (semi pseudo-code):

r : ^(SOME_NODE x y)
      -> ^(SOME_NODE x y
                 { // action
                 // for loop iterates over a data structure getting info 
and inserting it into a new subtree
                 for(every item in data structure) {
                     add new subtree ^(MY_NEW_SUBTREE item)
                 }
                 }
             ) // tree rewrite ends here
     ;

where the rewritten tree will have a new subtree created from each 
iteration of the loop. I suppose what I want is to dynamically build new 
subtrees of the resulting rewritten tree for the rule.

The above is invalid ANTLR syntax but I'm wondering whether this is 
possible somehow. I would rather use ANTLR's friendly tree building 
syntax as above (i.e. ^(MY_NEW_SUBTREE item) ); I don't want to have to 
'manually' build the tree inside the action (using ANTLR's tree building 
API) if I can help it. There might even be a simpler method of doing 
this that I've missed...

Thanks,
Stephen


More information about the antlr-interest mailing list