[antlr-interest] How to change AST node order in Java ?

Jaak Niit jack at icefire.ee
Mon Nov 12 23:23:34 PST 2007


Curtis Clauson wrote:
> Jaak Niit wrote:
>> Is there solution to change AST node order in Java ?
>>
>> If current node have children A, B, C, D and I want to change order 
>> to A, D, B, C.
>> BaseTree has methods deleteChild(int i) and addChild(Tree t),
>> but addChild(int offset, Tree t) is missing.
>>
>> I want to write something like
>> BaseTree D = t.getChild(3);
>> t.deleteChild(3);
>> t.addChild(1,D);
>>
>> So problem is how to add node in middle of list.
>
> The default object used for an AntLr tree is CommonTree which 
> sub-classes BaseTree which implement the Tree interface. BaseTree 
> extends the Tree interface with, among others, the method
>   void setChild(int i, BaseTree t)
>
I have extended CommonTree and use ASTLabelType already, but setChild 
replaces i-th element in list, but I need to add.
When I look to void addChild(Tree t) source code then I need 
children.add(offset, t) instead of children.add(t).

I thought that this is so common problem that it should have simple 
solution :-)
> You can simply cast your tree object to BaseTree or CommonTree, or 
> specify the returned type in the grammar with the ASTLabelType option.
>
> I hope that helps.
> -- Curtis
>


-- 
# Jaak Niit 



More information about the antlr-interest mailing list