[antlr-interest] lexer: matching float vs int

Olya Krachina okrachin at purdue.edu
Tue Sep 9 13:14:31 PDT 2008


Thank you all for the replies. Thomas Brandon  mentioned that i have 2.x instead
of 3.1. I am new to antler and compilers all together so I would not have been
able to notice that. But indeed i have antler 2.x, because of certain
compatibility issues.
I tried out what everyone suggested, googled antlr 2.x tutorials and i got my
lexer to work. The final rules after some trial and error are these:

protected DIGIT:        ('0'..'9') ;
INTLITERAL:		(DIGIT)+ ('.'(DIGIT)+ {$setType(FLOATLITERAL);})?;
FLOATLITERAL:	        ('.')(DIGIT)+;

perhaps there is a redundancy somewhere here (with DIGIT), but it's functional
for all the test cases i could think of. 

Thank you all very much for the help!


Quoting Sam Harwell <sharwell at pixelminegames.com>:

> You should cross-list that article under “generating user-friendly error
> messages for bad input” or something similar. The wiki has various articles
> on error handling, but none that I saw that have user-friendly messages as a
> goal. The comments on yours as-is make it the best I’ve seen on that
> topic.
> 
>  
> 
> Sam
> 
>  
> 
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
> Sent: Tuesday, September 09, 2008 2:16 PM
> To: antlr-interest
> Subject: Re: [antlr-interest] lexer: matching float vs int
> 
>  
> 
> On Tue, 2008-09-09 at 13:10 -0500, Sam Harwell wrote: 
> 
> Here is the numeric lexer section of a C++ grammar. This method is very
> different from Jim’s method, as it doesn’t use any semantic predicates,
> 
> 
> See the leading points in the article, being that ANTLR is working out the
> lookahead for you, whereas the example is minimal in this regard, you don't
> have a range operator '..' here and the only language specific things are
> input.LA(), which is just LA() in C. Finally, your code does not accommodate
> erroneous input, it will just throw lexer exceptions. If you are using IDEs
> or want good error messages from a parser, then you need to code for that
> rather than try to intercept exceptions and work it out afterwards.
> 
> But use whatever flaots your boat ;-). The examples was just for all the new
> people that find this particular problem very thorny :-)
> 
> Jim
> 
> 
> 
> 
> 
> and has no dependency on the target language. I 
> 
> 	 
> 	 
> 
> 




More information about the antlr-interest mailing list