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

Jörg Rathlev joerg at jrsoftware.de
Sun May 28 12:51:37 PDT 2006


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