[antlr-interest] How to modify parent of currently parsed subtree?

Koehne Kai Kai.Koehne at student.hpi.uni-potsdam.de
Mon May 29 00:52:44 PDT 2006


 
Hi,
 
your example will not work because the parent AST is constructed AFTER all of its child nodes! So there cannot be any method to access the parent AST within a child node ...
 
Try it the other way round: Just push the declaration of the temporary variable to the stack, and include it explicitly while generating the parent AST. That worked for me.

Regards,
 
Kai Koehne

 
________________________________

Von: antlr-interest-bounces at antlr.org im Auftrag von Jörg Rathlev
Gesendet: So 28.05.2006 21:51
An: antlr-interest at antlr.org
Betreff: [antlr-interest] How to modify parent of currently parsed subtree?



Hi,

how can I modify a parent node of the subtree I'm currently matching in
a tree parser?

I am currently working on a project where one of the AST transformations
I have to do involves having to duplicate an expression. Because I
cannot guarantee that the expression does not have side effects, I don't
want to simply duplicate its AST. Instead, I am trying to create a
temporary variable, which needs to be declared in the block (slist) that
contains the expression. I have tried to create a stack of enclosing
blocks and then modify the innermost one this way (in an action that is
located in a subrule of slist):

AST enclosingSList = enclosingSListStack.peek();
AST prevFirstChild = enclosingSList.getFirstChild();
AST tmp = #([VARIABLE_DEF, "VARIABLE_DEF"], ... );
tmp.setNextSibling(prevFirstChild);
enclosingSList.setFirstChild(tmp);
System.out.println(enclosingSList.toStringList());

The AST printed by the last line is the intended one, however, those
changes are not applied to the final AST that the tree parser will
return as its result of parsing.

Why does this happen? And how can I work around this?

Thanks!

Joerg




More information about the antlr-interest mailing list