[antlr-interest] Look-ahead problem parsing phrase?

Jim Idle jimi at temporal-wave.com
Sun Jun 28 19:45:45 PDT 2009


Nick Vlassopoulos wrote:
> Hi Sean,
>
> I am not sure about this, but I think you have to rearrange the order 
> of the rules. More specifically,
> antlr will use rules in the order they appear, therefore, WS should 
> probably near the end of your list.
> Maybe something like this would work:
>
> grammar Phrase;
>
> line : WS? PHRASE EOL?;
>
> PHRASE : WORD (WS WORD)*;

It's because you have WS here which MUST be followed by WORD, but it is 
followed by EOF so it fails. Try:

line : WS? PHRASE EOF;
PHRASE : (WORD WS?)* EOL?;

Jim



More information about the antlr-interest mailing list