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

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


Yes, you need to follow the method in the example - what you are trying to do will not work until you left factor it.
 
Jim
 
From: Scott Oakes [mailto:scott.oakes63 at googlemail.com] 
Sent: Friday, January 29, 2010 10:30 AM
To: Jim Idle
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Lexer for floating point numbers + field access syntax with '.'
 
Thanks Jim, the link looks very useful, albeit a bit daunting. I tried amending my FLOAT to:

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

Unfortunately I get a "rule FLOAT failed predicate" error.
On Fri, Jan 29, 2010 at 6:02 PM, Jim Idle <jimi at temporal-wave.com> wrote:
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')+ ;
 




More information about the antlr-interest mailing list