[antlr-interest] Syntax of rewrite rule

Bart Kiers bkiers at gmail.com
Thu Apr 1 03:43:13 PDT 2010


On Thu, Apr 1, 2010 at 1:21 PM, Christian Mayer <
christian.mayer at bsse.ethz.ch> wrote:

> I am wondering how to transform the rewrite rule
>
> Expr    :   mul_expr ( ( PLUS | MINUS )^ mul_expr )*;
>
> Into the explicit form with the arrow
>
> Expr    :   a=mul_expr (  op=( PLUS | MINUS ) b=mul_expr )*   ->   ? ;
>
> For later use in an AST tree parser.
>
> What is the proper way to do this? I checked ² The definitive ANTL
> reference²/online documentation but could not find anything about the
> syntax.
>

I did not test it, but I believe this would work:

Expr
  :  (a=mul_expr                   -> $a           )
     (op=(PLUS | MINUS) b=mul_expr -> ^($op $a $b) )*
  ;

Also see Chapter 7, section "Constructing AST's with rewrite rules"
paragraph "Rewrite Rules in Subrules" from "The definitive ANTLR reference".

Kind regards,

Bart.


More information about the antlr-interest mailing list