[antlr-interest] Nestes Parenthetical in Rewrite Rule

Foust javafoust at gmail.com
Wed Aug 27 01:11:59 PDT 2008


Mike,

> Is there any way to write this that won't give an error:
> shape :    '(' 'rect' (ID | STRING) float float ')' 
> -> ^('rect' (ID | STRING) float float);

I believe the problem is that you cannot use the or (|) operator after the
arrow (->).

Instead, you can write:

  shape :   '(' 'rect' (ID | STRING) p1=float p2=float ')'
		-> ^('rect' ID? STRING? $p1 $p2)

or you can split the "(ID | STRING)" expression out into its own rule and
then just include that rule in the tree output:

  shape :   '(' 'rect' rectId p1=float p2=float ')'
		-> ^('rect' rectId $p1 $p2)

  rectId : ID | STRING;

Brent



More information about the antlr-interest mailing list