[antlr-interest] Disambiguating Decimal and Integer '.'

Gavin Lambert antlr at mirality.co.nz
Thu Oct 1 13:06:54 PDT 2009


At 08:12 2/10/2009, Kaleb Pederson wrote:
 >INTEGER
 >    :   NUMBER+;
 >
 >DECIMAL
 >    :   INTEGER '.' INTEGER;

You should have a look at the wiki and the mailing list 
archives.  This is a fairly common problem with a fairly simple 
solution:

fragment INTEGER : NUMBER+ ;

DECIMAL
   :  INTEGER ('.' INTEGER | { $type = INTEGER; })
   ;

If that doesn't work by itself, you can add a predicate to force a 
two-character lookahead:

DECIMAL
   :  INTEGER (('.' NUMBER) => '.' INTEGER | { $type = INTEGER; })
   ;



More information about the antlr-interest mailing list