[antlr-interest] Please advice about float

Mark Kattenbelt mark.kattenbelt at gmail.com
Mon Sep 19 10:27:59 PDT 2005


My solution:

REAL_OR_INT
    : ( INT '.' ) => REAL { $setType(REAL); }
    | ('-' INT '.' ) => REAL { $setType(REAL); }
    | INT { $setType(INT); }
    ;

protected INT options { paraphrase = "a constant integer"; }
    : ('0' .. '9')+
    ;

protected REAL options { paraphrase = "a floating point value"; }
    : ('-')? INT '.' INT
    ;

Hope it helps,

Mark Kattenbelt

> Dear all,
>
>  
>
> I would like to parse integers and simple floats without exponents. Is 
> this the correct grammar?
>
>  
>
> class MyLexer extends Lexer;
>
> options {
>
>             k=2;
>
> }
>
>                                                                        
>
> NUMBER          :           INT ((FLOAT)?)
>
>                         ;
>
> protected
>
> FLOAT              : ('.' INT)
>
>                         ;
>
> protected
>
> INT                   :           (DIGIT)+
>
>                         ;
>
>                                                 
>                                                                                                 
>
>
> protected                                              
>
> DIGIT                :           '0'..'9'
>
>                         ;
>
>  
>
> WS     :
>
>     (' '
>
>     | '\t'
>
>     | '\r' '\n' { newline(); }
>
>     | '\n'      { newline(); }
>
>     )
>
>     { $setType(Token.SKIP); }
>
>   ;
>



More information about the antlr-interest mailing list