[antlr-interest] Lexer rules and unreachable alternatives (trying to understand lexer)

Johannes Luber jaluber at gmx.de
Thu Apr 19 06:24:14 PDT 2007


Johannes Luber wrote:
> start
>    :  WS
>    |  FOO
>    ;
> 
> in your example? Otherwise ANTLR may choose the superset rule in favor
> of subset rule on its own.

Oops, the start rule has to be something like

start : (WS | FOO)* EOF;

or it can't recognize whitespace separated strings. Unfortunately, when
I was testing the grammar in ANTLRworks, it chose FOO over WS, until I
added the space as non-FOO character.

Even then I didn't manage to "degreedy" the WS rule - what did you do
exactly? Here my latest try.

start
   :  (WS |  FOO)* EOF
   ;

WS : (options {greedy=false;} : ' '+) ;
FOO : ~('x' | 'y' | 'z' | ' ')+ ;

And as you are new with ANTLR I can recommend the following tutorial
(which I incidentally wrote):

http://www.antlr.org/wiki/display/ANTLR3/Quick+Starter+on+Parser+Grammars+-+No+Past+Experience+Required

Maybe Terence should put a more prominent link on the webpage to it...

Best regards,
Johannes Luber


More information about the antlr-interest mailing list