[antlr-interest] Re: Floats and Integers

Lubos Vnuk lubos.vnuk at rts.at
Tue Sep 23 08:20:20 PDT 2003


How about this?

class TestParser extends Parser;
abc:"ABC" float INT;
float: INT | DEC;

class TestLexer extends Lexer;
INT: ('0'..'9')+;
DEC: INT '.' INT;

Regards,
Lubos.

--- In antlr-interest at yahoogroups.com, "Tom Zagotta" <tzagotta1 at s...> 
wrote:
> Hello everyone,
>  
> I have a grammar that needs to accept both floating-point numbers 
and
> integers. A simplified form of the grammar (whitespace skipping not 
shown)
> is like this:
> 
> class TestParser extends Parser;
> abc:"ABC" FLOAT INT;
>  
> class TestLexer extends Lexer;
> INT: ('0'..'9')+;
> FLOAT: INT '.' INT;
> 
> The problem I have is with input like this:
> 
> ABC 3 5
> 
> In this case, I need the "3" to match the FLOAT token for the abc 
rule. It
> seems that the only way to do this is to change the rule to the 
following:
> 
> abc: "ABC" (FLOAT|INT) INT;
> 
> But the problem with that is that there are a really large number 
of such
> rules in my grammar.
>  
> Is there a better/alternative way to handle this? Ideally I'd like 
to define
> a token that can match both int and float, but I still need a 
separate token
> for int. It seems this is not possible.
>  
> Thanks in advance,
>  
> - Tom


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list