[antlr-interest] How to distinguish between integer an binary number?

Johannes Luber jaluber at gmx.de
Sat Dec 13 12:31:33 PST 2008


Mario Prada schrieb:
> I need to distinguish between integer an binary number in my grammar.<P>
> In Lexer I have this code:
> <P>
> <font size=2><pre>
> BINARIO : (BIN_DIGIT)+;
> INT :   DIGIT+ ;
> fragment BIN_DIGIT
> : '0'|'1';
> fragment DIGIT
>     :    '0'..'9';
> </pre></font>
> <P>
> and in Parser, when I have a grammar rule with an INT token, it seems to
> work, but this error appears:
> <P>
> mismatched input '1' expecting INT
> <P>
> How can I solve this problem?

You actually can't do that in the lexer. ANTLR does lexing all in-front,
before even the parser sees the first token. Thus all numbers fitting
the binary pattern are BINARIOs, even if they are supposed to be normal
numbers. The solution is make one lexer rule for all kinds of numbers
and to check only in the parser, if a number contains only zeros and ones.

Johannes
> 
> Thanks in advance.
> 
> -- 
> Mario Prada Arroyo
> ----------------------------------------------------------------------------------------------------------
> 
> 
> ------------------------------------------------------------------------
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list