[antlr-interest] How to modify this rule?

Kevin J. Cummings cummings at kjchome.homeip.net
Mon Jan 5 19:53:22 PST 2009


chain one wrote:
> enumeration_type 
> : DOT IDENT DOT 
> ;
> 
> DOT
> : '.'
> ;
> fragment
> DIGIT
> : '0'..'9'
> ;
>    
> INT
>   : '-'? DIGIT+ 
>  ;
> 
> FLOAT
> :   '.' DIGIT* EXPONENT?
>      ;
>     
> fragment
> EXPONENT: ('e' | 'E') ('+' | '-')? (DIGIT)+;
>     
> 
> 
> IDENT
> 
> : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
> ;
> 
> when the input is ".EXTERNAL." , the lexer don't recognize '.' as a DOT 
> but try to recognize it as a FLOAT. it will fail after ".E" has been 
> recognized.

You need to merge your DOT rule with your FLOAT rule and set the type 
manually like below:

fragment
FLOAT :
       ;

DOT : '.' ( DIGIT+ EXPONENT? { $type = FLOAT; }
           | EXPONENT         { $type = FLOAT; } )
     ;

See this page for a larger example:

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

> How to make the lexer capable of back track? That is also to say if it 
> fails to recognize a FLOAT, it will try DOT.

-- 
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list