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

Mario Prada mario.prada at gmail.com
Sat Dec 13 14:39:56 PST 2008


Thanks all for the quick responses.
I´ll try to maintain the two lexer rules, and manage them from the parser.


Mario Prada

On Sat, Dec 13, 2008 at 9:31 PM, Johannes Luber <jaluber at gmx.de> wrote:

> 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
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081213/e8719451/attachment.html 


More information about the antlr-interest mailing list