[antlr-interest] AST rewrite query

Richard Kenyon richardlkenyon at gmail.com
Tue Jun 12 14:06:37 PDT 2012


On 12 June 2012 19:01, Bart Kiers <bkiers at gmail.com> wrote:
> 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.

Hi John, Bart,

Both solutions are good.

Though Bart's solution happens to be closer to what I was looking for.
It's great, I don't think I have understood it yet though.

Thanks for the help,

Richard


More information about the antlr-interest mailing list