[antlr-interest] newbie roblem about expressions & number representations
Gavin Lambert
antlr at mirality.co.nz
Fri Dec 11 14:12:50 PST 2009
At 13:41 11/12/2009, Duygu Altinok wrote:
>INT : (DIGIT )+
> ;
>
>FLOAT
> : ('0'..'9')+ '.' ('0'..'9')* (EXPONENT)?
> | '.' ('0'..'9')+ (EXPONENT)?
> | ('0'..'9')+ EXPONENT
> ;
These two rules have a common left prefix, and are thus
ambiguous. Even ANTLR v3 with * lookahead has trouble with this;
ANTLR v2's fixed (typically small) lookahead doesn't stand a
chance.
You will need to merge these rules -- make a single FLOAT rule,
and if you see the sequence of digits without decimal places or
exponents then change the type to an INT. You will similarly need
to get rid of the ambiguity between your first and third alts.
This is how I'd do it in v3; you may need to modify it a bit to
get it to work in v2. Also note that you'll have to set k to at
least 2, or the alt that starts with a '.' will give you similar
trouble.
fragment INT : DIGIT+; /* or "protected" in v2 */
FLOAT
: INT (EXPONENT | '.' DIGIT* EXPONENT? | { $type = INT; })
| '.' INT EXPONENT?
;
More information about the antlr-interest
mailing list