[antlr-interest] Lexer for floating point numbers + field access syntax with '.'

Scott Oakes scott.oakes63 at googlemail.com
Sat Jan 30 05:30:50 PST 2010


> On Fri, Jan 29, 2010 at 6:37 PM, Jim Idle <jimi at temporal-wave.com> wrote:
> Yes, you need to follow the method in the example - what you are trying to do will not work until you left factor it.

OK, I've attempted to merge the INT, DOT and FLOAT rules together and
manually set the token types at various branch points in the rules.
I'm still not having much luck with it, I'm afraid, but here's my
grammar to date:

grammar test;

fragment INT:;
fragment DOT:;

top: expr EOF;

expr: (INT | FLOAT | ID | '(' expr ')') (DOT ID)*;

ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;


FLOAT
    :   ('0'..'9')+ (
    			{input.LA(2) >= '0' && input.LA(2) <= '9'}?=>
    			      '.' ('0'..'9')+ EXPONENT? {$type = FLOAT;}
                     | {$type = INT;} (
                           '.' {$type = DOT;}
                       )
                     	
                    )

     | '.' {$type = DOT;}

    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;


More information about the antlr-interest mailing list