[antlr-interest] parser backtrack issue

David Holroyd dave at badgers-in-foil.co.uk
Sat Jan 19 04:58:21 PST 2008


On Fri, Jan 18, 2008 at 06:44:17PM -0800, Ashish wrote:
> I can get rid of the error if booleanExpressions is defined as
>  booleanExpressions
>      :  booleanExpression ( (BOOLEAN_OR  | BOOLEAN_AND) booleanExpression )*
> 
> however I am not sure how to go about writing the rewrite rule for AST
> generation for this one line version.

It's quite simple with tree structuring operators (assuming you don't
have additional reasons to use a rewrite),

  booleanExpressions
      :  booleanExpression ( boolOp^ booleanExpression )*
      ;

  boolOp
      :  BOOLEAN_OR  | BOOLEAN_AND
      ;

Expressing this with the rewrite syntax is a bit more fiddly,

  booleanExpressions
      :  (   booleanExpression
             -> booleanExpression
	 )
         (   boolOp booleanExpression
             -> ^(booleanOp $booleanExpression booleanExpression)
         )*
      ;

The (apparently redundant) booleanExpression->booleanExpression is
required to set the initial value of the $booleanExpressions result
tree.


ta,
dave

-- 
http://david.holroyd.me.uk/


More information about the antlr-interest mailing list