[antlr-interest] always create an ast node for an optional token

Gavin Lambert antlr at mirality.co.nz
Mon May 5 12:44:04 PDT 2008


At 00:06 6/05/2008, Felix Dorner wrote:
 >declaration: VAR ID -> ^(VAR ID)
 >
 >forStatement : 'for' (VAR? ID 'in'...) -> ^(FOR ^(VAR ID ...))
 >
 >You see, that I'd like to put a var node in the tree in all 
cases,
 >even if it's ommited in the text in unambiguous situations. It
 >seems like antlr doesn't like it this way, I get
 >RewriteEmptyStreamExceptions in this case.

Haven't tested this, but you ought to be able to do what you want 
by being a bit more explicit:

forStatement
   :  'for'
      ( v=VAR ID 'in' ... -> ^(FOR ^($v ID ...))
      | ID 'in' ... -> ^(FOR ^(VAR ID ...))
      )
   ;

Though personally I wouldn't bother putting the VAR node into the 
AST at all.  I'd make it more like ^(FOR ID ...).  Since you know 
that the first child of a FOR tree is the id, it's already 
unambiguously a VAR.



More information about the antlr-interest mailing list