[antlr-interest] Questions about Tree Generation in v3

Travis Caldwell travis_caldwell2000 at yahoo.com
Tue May 15 00:38:36 PDT 2007


  Hi,
 
  I was looking at the ANTLR tree construction syntax and the ANTLR
 book
 and have two questions:
 
  1) In chapter 7 of the ANTLR book, it mentions that AST's should not
 have missing nodes for optional elements which are not present. 
 
  I see why this is a good idea since omitting optional elements rather
 then replacing them with a dummy element makes the trees ambiguous. 
 
  But the "?" EBNF tree generation notation does not seem condusive to
 this.
 
  From experimenting with the tree generation it seems that a rule like
 
  foo_decl : modifier? type identifier 
          -> ^(TYPEDEF modifier? type identifier);
 
  Will generate the folowing trees from the folowing input:
 
  "static int foo" -> ^(TYPEDEF static int foo)
  "int foo"        -> ^(TYPEDEF int foo)
 
  This doesn't seem to be in keeping with the concept of inserting
dummy tokens for missing elements.  
 
  Is there some way to insert dummy elements when a "?" doesn't match
in a re-write rule?
 
  Is there some other way to handle this?
 
  2) Duplicating trees when a subrule returns a list
 
  I see how when a rule specifies multiple tokens you can cause
duplicate tree generation. i.e.
 
  decl : 'int' ID ( ',' ID)* -> ^('int' ID)+
 
 Should yield a list of trees of the form ^('int' ID)
 
  But is there a way to do this if the list of items is returned from a
sub-rule?
 
  i.e 
 
  decl : 'int' list_of_ids 
  list_of_ids: ID ( ',' ID )* -> ID+
 
 The " ID+ " seems to cause list_of_ids to return a list of ID's, but I
don't know how I can take this list and get decl to generate a list of
trees of the form ^('int' ID) from this list.
 
  Is there a way to do this?
 
  Any help would be appreciated.
 
 
 
                  _Travis_Caldwell_



       
____________________________________________________________________________________Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
http://farechase.yahoo.com/


More information about the antlr-interest mailing list