[antlr-interest] Re: Floats and Integers

mzukowski at yci.com mzukowski at yci.com
Tue Sep 23 08:33:18 PDT 2003


First take a look at the java.g grammar at the rule for numbers, you need to
match both INTs and FLOATs in one lexer rule.

Then, as Lubos points out, make a "float" rule for the parser that accepts
either an INT or a FLOAT.

Monty

-----Original Message-----
From: Lubos Vnuk [mailto:lubos.vnuk at rts.at] 
Sent: Tuesday, September 23, 2003 8:20 AM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] Re: Floats and Integers

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/ 


 

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




More information about the antlr-interest mailing list