[antlr-interest] how to set lookahead in v3

Johannes Luber jaluber at gmx.de
Thu May 3 02:10:57 PDT 2007


Markus Kuhla wrote:
> Hi.
> 
> I'm using antlr, v3beta7. The LL(*) algorithm determines automatically how much lookahead is necessary to choose a alternative?!
> 
> I have the problem, that the parser can find 1 or 2 tokens matching to an alternative of a parser rule, e.g.
> next input: "NEWLINE BLANK BLANK DASH DASH ..."
> the parser is currently in this rule:
> 
> text : line+;
> line : NEWLINE? blanks? all_chars_but_not_dash
>      | EOF;
> 
> The parser chooses the 1st alternative of line, and gives an error of course.
> In the grammar is another possibility, to continue with "NEWLINE blanks? DASH" - So the parser has to go out of the line-rule, if there is a dash!
> 
> 
> How can you realize a behavior like a real LL(*) parser, that there is enough lookahead to make a appropriate decision (leave rule line or continue with alternative1)

The problem isn't that LL(*) is unsufficient, but that your grammar is
ambiguous. The tokens "NEWLINE BLANK" can be either matched by one line
token or by two line tokens. I think that the following rules don't
exhibit this behaviour:

text : (NEWLINE? blanks? line)+ EOF;
line : all_chars_but_not_dash;

Best regards,
Johannes Luber


More information about the antlr-interest mailing list