[antlr-interest] Improvements to left recursive grammars

Terence Parr parrt at cs.usfca.edu
Thu Jan 5 18:47:08 PST 2012


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



More information about the antlr-interest mailing list