[antlr-interest] Improvements to left recursive grammars

Kyle Ferrio kferrio at gmail.com
Thu Jan 5 21:55:54 PST 2012


OMG.  Honey Badger isn't afraid of anything.  He just keeps going!

This does work just like I hope/expect.  I might have to remind myself that
it really is LL* under the hood.  Cool!

Kyle

On Jan 5, 2012 7:47 PM, "Terence Parr" <parrt at cs.usfca.edu> wrote:
>
> Howdy. Ok,These left recursive grammar rules are craaaazy. Now they
behave just as you would expect regular rules. For example, you could have
return values and actions (on the right edge). The cool thing is you can
label the left recursive rule references (a=e) even though those disappear
in the implementation. :)
>
>    s : e {System.out.println($e.v);} ;
>    e returns [int v]
>      : a=e '*' b=e {$v = $a.v * $b.v;}
>      | a=e '+' b=e {$v = $a.v + $b.v;}
>      | INT         {$v = $INT.int;}
>      | '(' x=e ')' {$v = $x.v;}
>      ;
>
> For example, I passed an input 1+2*3 and got the following parse tree
>
> (s (e (e 1) + (e (e 2) * (e 3))))
>
> just like you would get if this were a bottom-up tool. Moreover, it fires
exit rule events as you would expect. you will see something like this, if
you pass in a listener that prints out the ctx. see the example page.
>
> exit 1: (e 1)
> exit 1: (e 2)
> exit 1: (e 3)
> exit 1: (e (e 2) * (e 3))
> exit 1: (e (e 1) + (e (e 2) * (e 3)))
> 7
> exit 0: (s (e (e 1) + (e (e 2) * (e 3))))
>
> just pushed to the website dist directory. also updated this page:
>
> http://www.antlr.org/wiki/display/ANTLR4/Examples
>
> see
>
> http://antlr.org/depot/antlr4/main/CHANGES.txt
>
> Ter
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list