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

Jim Idle jimi at temporal-wave.com
Fri Jan 29 10:02:17 PST 2010


Please see the FAQ and complete grammar at:

http://antlr.org/wiki/display/ANTLR3/Lexer+grammar+for+floating+point%2C+dot%2C+range%2C+time+specs


All you need do is add to the predicate here:

                |   // We can of course have 0.nnnnn
                    //
                    { input.LA(2) != '.'}?=> '.'

To check :

{ input.LA(2) != '.' && input.LA(2) >= '0' && input.LA(2) <= '0' }?=> '.'

Then remove the empty alt there that allows number forms like 8.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Scott Oakes
> Sent: Friday, January 29, 2010 9:43 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Lexer for floating point numbers + field
> access syntax with '.'
> 
> 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')+ ;
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list