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

lgcraymer lgc at mail1.jpl.nasa.gov
Thu Jun 13 15:18:30 PDT 2002


--- In antlr-interest at y..., "Niall Gormley" <ngormley at h...> wrote:
> 
> Loring,
> 
> Spot on. Works a treat.
> 
> Thanks for the help.
> 
> Any ideas on how to go about fixing the AST Gen code so that an node 
type 
> can be associated with a class, just as in the Parser? I'm willing 
to lend a 
> hand.... Or are heterogeneous ASTs a bad idea anyway?

All you need to do is copy the heterogeneous AST assignments from the 
parser grammar's "tokens" section into a "tokens" section for the tree 
grammar and they should work--except for #[FOO, "name"] in actions 
which translate to calling the ASTFactory with default object type.  
Heterogeneous ASTs are quite useful:  it's the implementation which is 
suboptimal.  A good implementation approach would be to have a table 
associating types with classes assigned to the ASTFactory by the 
grammar processor--right now, the grammar processor passes class 
information to the AST factory for every AST creation, and creations 
in actions bypass the grammar processor's knowledge.  C++ would do it 
differently, but the same principle applies.

--Loring

> 
> Thanks,
> Niall
> 
> >From: "lgcraymer" <lgc at m...>
> >Reply-To: antlr-interest at y...
> >To: antlr-interest at y...
> >Subject: [antlr-interest] Re: Modification of Node Attributes using 
Tree 
> >Grammar
> >Date: Thu, 13 Jun 2002 20:46:20 -0000
> >
> >--- 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
> >expression.
> > > > 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
> >seems
> > > you are setting the proper variable.  #(i) is very strange 
though.
> >What
> > > do you mean here?  #i or i works, but #(i) is a long way to say 
make
> >i
> > > the root of a tree with only one  node: i.  That may be 
something
> >for
> > > which the code gen is broken.  Try removing the #(...) around 
the i
> >and
> > > 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
> >
> >dims
> >    : #( LITERAL_DIM i:IDENT )
> >      {
> >      #i.setVariantType(0);
> >      }
> >    ;
> >
> >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.
> >
> >--Loring
> >
> > >
> > > Ter
> >
> >
> >
> >
> >Your use of Yahoo! Groups is subject to 
http://docs.yahoo.com/info/terms/
> 
> 
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: 
http://messenger.msn.com


 

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



More information about the antlr-interest mailing list