[antlr-interest] Re: Modification of Node Attributes using Tree Grammar

lgcraymer lgc at mail1.jpl.nasa.gov
Thu Jun 13 13:46:20 PDT 2002

--- In antlr-interest at y..., Terence Parr <parrt at j...> wrote:
> On Thursday, June 13, 2002, at 09:40  AM, Niall Gormley wrote:
> > Hi,
> >
> > Is it possible to use a Tree Grammar to modify Node attributes?
> >
> > The node "i" is getting modified buy the call to "setVariantType" 
but I
> > can't seem to get it copied to the output AST for the dims 
> > So
> > the real question might be, how can I get source node "i" to end 
up in 
> > the
> > output expression?
> >
> > dims!
> >   : #( LITERAL_DIM i:IDENT )
> >     {
> >     i.setVariantType(0);
> >     #dims = #( #[LITERAL_DIM], #(i));
> >     }
> >   ;
> Howdy.  i points to the output node created from IDENT so you it 
> you are setting the proper variable.  #(i) is very strange though.  
> do you mean here?  #i or i works, but #(i) is a long way to say make 
> the root of a tree with only one  node: i.  That may be something 
> which the code gen is broken.  Try removing the #(...) around the i 
> see what happens.  Let me know if this is a bug and I'll add to the 
> list.  Loring may already have found this ;)

I remember that some of the node construction stuff is broken, but not 
which part specifically.  For what you want to do, try just


and if that fails, try replacing #i with i_AST--that will work.  ANTLR 
by default builds an output tree which matches the input tree and you 
just need to set the contents of #i.  Note that AST labels behave 
differently than token labels:  if i:IDENT labels a token in the 
parser, #i translates to i; while in a tree grammar, #i translates to 
i_ast (and #i_in translates to i).

Also, if you are using heterogeneous ASTs, make sure that you specify 
them in the tree grammar as well as the parser--otherwise you will get 
 the default AST node type.  One of the things that needs to be fixed 
is that ASTFactories should know about heterogeneous ASTs:  
right now, #[FOO] always builds an AST node of the default type 
because the ASTFactory only knows about the default.


> Ter


Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

More information about the antlr-interest mailing list