[antlr-interest] Problem with MismatchedTokenException

Kirby Bohling kirby.bohling at gmail.com
Sat Apr 9 14:12:48 PDT 2011


The problem is pretty simple there (but a sign you need to think
everything through more).
"test=toto" will generate the following Tokens:

NAME GENERATED_EQUALS NAME

Which your parser rules won't allow.

"test=toto0" will generate the following Tokens:

NAME GENERATED_EQUALS VALUE

Which your parser does allow.

Parroting the commonly given advice, stop doing this low level
validation in the lexer.  Let the 'bad' push through as long as
absolutely possible.  You can't generate a decent error beyond exactly
this type of thing.  If you let this percolate all the way to the end,
you could generate a nice error that had plenty of context.

In this case, one concept would be to use VALUE on both sides of the
equal sign.  When you get to the tree walking phase, you'd examine the
contents of the token on the left side of the token, if it included an
illegal character like "toto2=bar" at that point you could generate an
error like: 'toto2' is not a legal ${L-Name} on line 47 of the input
file.

Kirby



On Sat, Apr 9, 2011 at 3:56 PM, Kazuki <kazuki.rebirth at hotmail.fr> wrote:
> Hi,
>
> I have just started using ANTLR and I get stuck with this trivial grammar :
>
> grammar Test;
>
> options {
>       language = Java;
> }
>
> test  :       NAME '=' VALUE;
>
> NAME    :       LETTER+;
> VALUE   :       (LETTER | DIGIT)+;
>
> fragment
> LETTER
>       :       'a'..'z'|'A'..'Z'
>       ;
>
> fragment
> DIGIT
>       :       '0'..'9'
>       ;
>
> With the input "test=toto" I have a MismatchedTokenException on the token
> "toto" while I get a correct syntax with an input using a digit like
> "test=toto0".
> Can someone explain what is my mistake in the grammar ?
>
> Thank you.
>
> --
> View this message in context: http://antlr.1301665.n2.nabble.com/Problem-with-MismatchedTokenException-tp6257670p6257670.html
> Sent from the ANTLR mailing list archive at Nabble.com.
>
> 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