[antlr-interest] Resolving ambiguities in Lexer rules

Joseph Areeda newsreply at areeda.com
Sat Aug 15 00:34:14 PDT 2009


I don't see this as an ambiguity issue but rather a decision of whether 
your grammar uses reserved words or not.
I'm not an expert by any means but that doesn't mean I don't have an 
opinion just that you should take it with a grain of salt.

You can either handle this with a symbol table later in the process or 
rewrite the requestline to something like
requestline : ver EQUAL (SPECIAL_STRING | ver);

Joe


Achint Mehta wrote:
> Hi All,
>
> The section "Ambiguities and Non determinisms" of the book "The 
> definitive ANTLR guide" talks about the ambiguities in lexer rules, 
> but I am not sure how to resolve them.
>
> Consider a following grammar which assigns a value to an ID. The ID 
> can either be VERSION or COUNT while its value can be anything:
> -----------------------------------------------
> grammar sample_parser;
>
> requestline : ver EQUAL SPECIAL_STRING ;
>
> /* Tokens */
> ver:('VERSION'| 'V') {}
>       | ('COUNT' | 'C') {} ;
>
>    
> SPECIAL_STRING:(CHAR)+ ;
> WHITESPACE: ' ';
> NEWLINE: ('\r')? '\n';
> EQUAL: '=';
>
> fragment
> CHAR: (('a'..'z')|('A'..'Z'));
> -----------------------------------------------
>
> If the input is given as
> VERSION=FIRST
> Then it works, but if following input is given
> VERSION=VERSION
> Then I get an error (MissingTokenException after the "=").
>
> How can this ambiguity be resolved ?
>
> Thanks in advance.
>
> Regards,
> Achint
> ------------------------------------------------------------------------
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090815/b8f1fca8/attachment.html 


More information about the antlr-interest mailing list