[antlr-interest] AST rewrite query

Bart Kiers bkiers at gmail.com
Tue Jun 12 11:01:40 PDT 2012


Hi John, Richard,

On Tue, Jun 12, 2012 at 6:54 PM, John B. Brodie <jbb at acm.org> wrote:

>
> Just off the top of my head, untested....
>
> exp : exp_helper -> ^(EXP exp_helper) ;
>
> exp_helper :    term ((PLUS^ | MINUS^) term)*;
>

For an expression like 1+2-3, the following AST would be created:

    (EXP (PLUS (term) (MINUS (term) (term))))

(note the missing EXP root above MINUS)

I guess it would need to look like this:

exp : (term -> term) ( PLUS t=term  -> ^(EXP ^(PLUS $exp $t))
                     | MINUS t=term -> ^(EXP ^(MINUS $exp $t))
                     )*;

Regards,

Bart.


More information about the antlr-interest mailing list