[antlr-interest] Problem in grammar (#a, #b, #c, #d, #f are not well recognized)

Kevin J. Cummings cummings at kjchome.homeip.net
Fri Feb 25 13:56:20 PST 2011


On 02/25/2011 02:42 PM, Aurélien Baudet wrote:
>    Hello,
> 
> I'm currently writing an xtext plugin for css. I have a problem and I don't
> find any solution. My grammar works quite well for many css files but it
> fails on that:

> The grammar:
> 
> grammar css;

> elem:
> 	IDENT
> 	| '#' IDENT
> 	| '.' IDENT;

>  COLOR:
> 	'#' ('0'..'9' | 'a'..'f' | 'A'..'F')+;

Those are the 2 rules rules in conflict.  You need to find a way to
disambiguate the use of the # as a token.  It would help you to go back
to the general advice of not using quoted tokens in your parser.
Instead, have LEXER rules for each one.  You will probably need to merge
the COLOR and HASH rules together using a syntactic predicate in the
lexer rules.  Something like:

fragment
COLOR: ;

HASH: '#' ('0'..'9' | 'a'..'f' | 'A'..'F')=> (('0'..'9' | 'a'..'f' |
'A'..'F')+ { $type=COLOR; });

-- 
Kevin J. Cummings
kjchome at verizon.net
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list