[antlr-interest] Why does this conflict?

Nigel Sheridan-Smith nbsherid at secsme.org.au
Thu May 26 17:13:43 PDT 2005



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Matthew Tedder
> Sent: Friday, 27 May 2005 7:48 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Why does this conflict?
> 
> 
> I created this to add boolean literals:
> 
> // Boolean Literal
> BOOLIT
>   : "true"
>   | "false"
>   ;
> 
> And it seems to conflict with:
> 
> DENT
>   options {testLiterals=true;}
>   : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*
>   ;
> 
> With this antlr.Tool output:
> 
> ANTLR Parser Generator   Version 2.7.3   1989-2004
> jGuru.com
> morph.g: warning:lexical nondeterminism between rules
> IDENT and BOOLIT upon
> morph.g:     k==1:'f','t'
> morph.g:     k==2:'a','r'
> 


BOOLLIT is a token in the lexer... If you switch it to a parser rule then
"true" and "false" will be treated as IDENT initially, but then these
"literals" will be changed to their own tokens "LITERAL_true" and
"LITERAL_false". 

So, for example, do something like the following in the parser:


expression: const_value | identifier;
identifier: IDENT;
const_value: NUMBER | boolean;
boolean: "true" | "false";


And in the lexer:


NUMBER: ('0'..'9')+ ;
IDENT: ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')* ;



The reason for the conflict is that "true" and "false" are treated as tokens
to be discovered and constructed (in the lexer), rather than literals which
are reinterpreted from another token after they are constructed (at some
point, further down the track).

Nigel 

--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435




More information about the antlr-interest mailing list