[antlr-interest] transforming AST's in place when generated

Carter Cheng carter_cheng at yahoo.com
Mon Feb 25 08:23:22 PST 2008


Well it's probably a bit of a beginner question but I
was wondering if you could in general reach into
already created subtrees and reconstitute them as you
pass them up the tree. Or perhaps return a forest i.e.
do something like this-

RuleA: RuleB RuleC RuleD RuleE;

Rule1:
       RuleA RuleF -> ^(DEF RuleB RuleC RuleD RuleE
RuleF)
     ;

i.e. somehow have RuleA pass back something where one
could reach into it an extract RuleB RuleC RuleD RuleE
and construct a new tree discarding information or
just raw passing of RuleB RuleC RuleD RuleE without
messing with the returns syntax.

I however think what your initial reply suggested is
enough for me to complete the task I have in hand so
thanks again.

Regards,

Carter.

--- Mark Wright <markwright at internode.on.net> wrote:

> > Thanks Mark. Just one followup question is there a
> way
> > to explicitly pass back a forest in ANTLR?
> 
> Hello Carter,
> 
> It sort of looks like it is passing back a forest
> of trees (with DD and FUNCTION_DECLARATION roots).
> 
> > i.e. when you do 
> > 
> > -> init_declarator+
> > 
> > it seems like you are not really passing back a
> rooted
> > tree.
> > 
> > Thanks again,
> > 
> > Carter.
> 
> This question seems too easy,  I am not sure I
> really
> understand the question.  If you wanted a rooted
> tree you could do something like:
> 
> -> ^(DECL init_declarator+)
> 
> I guess you would want to create the AST in which
> ever structure is easiest to handle with the next
> processing
> phase of the compiler or whatever program it is.
> 
> Regards, Mark
> 
> > 
> > --- Mark Wright <markwright at internode.on.net>
> wrote:
> > 
> > > Hello Carter,
> > > 
> > > The idea is to pass the int type_id AST as a
> > > paramater to the
> > > init_declarator_list, and the
> init_declarator_list
> > > passes it as a
> > > parameter to the init_declarator, something
> like:
> > > 
> > > declaration
> > >   :    storage_class_specifier? type_id
> > >
> init_declarator_list[$storage_class_specifier.tree,
> > > $type_id.tree] ';'
> > >           -> init_declarator_list
> > >   ;
> > > 
> > > init_declarator_list[CommonTree scs, CommonTree
> ti]
> > >   :   init_declarator[$scs, $ti] (','
> > > init_declarator[$scs, $ti])*
> > >           -> init_declarator+
> > >   ;
> > > 
> > > init_declarator[CommonTree scs, CommonTree ti]
> > >   :    function_declarator
> > >           -> {(scs == null)}?
> ^(FUNCTION_DECLARATION
> > > {$ti} function_declarator)
> > >           -> {(scs != null)}?
> ^(FUNCTION_DECLARATION
> > > {$scs} {$ti} function_declarator)
> > >           ->
> > >   |   declarator initializer?
> > >           -> {(scs == null)}? ^(DD {$ti}
> declarator
> > > initializer?)
> > >           -> {(scs != null)}? ^(DD {$scs} {$ti}
> > > declarator initializer?)
> > >           ->
> > >   ;
> > > 
> > > etc.
> > > 
> > > Regards, Mark
> > > 
> > > -- 
> 
> -- 
> 



      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 



More information about the antlr-interest mailing list