[antlr-interest] ANTLR3 - Find the parent node in an AST

Varun Bhargava varun.bhargava at carsales.com.au
Tue Aug 5 16:15:00 PDT 2008


Thanks guys. I'll try out the TreeAdaptor method.

Apparently, from the fisheye source tree, looks like the next version
3.1 will have a reference back to the parent node.

-----Original Message-----
From: Gavin Lambert [mailto:antlr at mirality.co.nz] 
Sent: Wednesday, 6 August 2008 06:56
To: Johannes Luber; Varun Bhargava
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] ANTLR3 - Find the parent node in an AST

At 22:16 5/08/2008, Johannes Luber wrote:
 >You could derive another class from CommonTree which does store 
a
 >reference to its parent and use it instead of CommonTree. Then 
you
 >have to find the places where the CommonTree instances are 
created
 >and modify those to include the parent reference. The easiest 
way
 >to find all those instances is to enforce the use of this 
parameter
 >as the compiler will complain about the missing argument. 
Possibly
 >you have to modify the template files, too, which means you have 
to
 >recompile the ANTLR tool yourself.

You shouldn't need to go that far.  That's what the TreeAdaptor is 
for -- to abstract the creation and modification of the AST, so 
you can use any type you feel like.

I'm surprised that the CommonTree wouldn't have a reference to its 
parent, though.  That just seems like it should be automatic for 
any tree structure.



More information about the antlr-interest mailing list