[antlr-interest] ints and fractions - how to avoid "Decision can match input such as "INT SLASH INT" using multiple alternatives" warnings

Jim Idle jimi at temporal-wave.com
Fri Oct 31 07:41:25 PDT 2008


On Thu, 2008-10-30 at 21:49 -0700, Sven Prevrhal wrote:
> Essentially, I want a return of type double for e.g.


Consult:

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

To see how to lex floating point numbers and so on, then steal an
expression parser from one of the examples grammars such as Java or C
(whichever sample is closest to your language). The book will also help
you here of course.

Jim

> 
> 3
> 
> 1.523
> 
> 1 2/3
> 
>  
> 
> Below’s my grammar with C# actions, and I get 
> 
> “Decision can match input such as "INT SLASH INT" using multiple
> alternatives” in Antlrworks. 
> 
>  
> 
> Thanks for your help!!
> 
> Sven
> 
>  
> 
> //
> 
> //Trouble Grammar
> 
> //
> 
> number returns [double n]
> 
>                 :               
> 
>                 (INT {$n = (double)int.Parse($INT.text);}) (f =
> fraction {$n += f;})? | 
> 
>                 f = fraction {$n = f;} | 
> 
>                 AFLOAT {$n = double.Parse($AFLOAT.text);};
> 
>  
> 
> fraction  returns [double f]          :               
> 
>                 i1=INT SLASH i2=INT {$f =
> (double)int.Parse($i1.text) / (double)int.Parse($i2.text);};
> 
>  
> 
> AFLOAT                :               INT DOT INT?;
> 
>  
> 
> INT         :               '0'..'9'+;
> 
>  
> 
> SLASH   :               '/';
> 
>  
> 
>  
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081031/5e9fbec6/attachment.html 


More information about the antlr-interest mailing list