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

Scott Oakes scott.oakes63 at googlemail.com
Fri Jan 29 09:42:53 PST 2010


Hi, hoping for some help trying to write a lexer that allows you to
recognise floating point literals (2.3) as well as field accesses of the
form x.y; see grammar below. The trouble is that an input like

  3.fieldAccess

Produces two tokens, FLOAT and ID, rather than the desired three, INT, DOT
and ID.

Pointers would be much appreciated!

-------------------

grammar test;

top: expr EOF;

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

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

INT :    '0'..'9'+
    ;

DOT: '.';

FLOAT
    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   '.' ('0'..'9')+ EXPONENT?
    |   ('0'..'9')+ EXPONENT
    ;

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

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


More information about the antlr-interest mailing list