[antlr-interest] expecting "class", found 'class'

Martin Probst mail at martin-probst.com
Tue Feb 13 23:34:01 PST 2007


Hi,

> i have lexer and parser in one file. Using the
> traceLexer or traceParser didn't help me.
>
> What does this message mean anyway?

It means that the parser expected a token with the text 'class' and  
found one with the text 'class'. The problem is that the parser  
actually expected a token with a certain token type, and it found one  
with a different token type. However the error message doesn't show  
that (I hope this was fixed in ANTLR 3...).

So this is usually a bug coming from a rule IDENT like this:
IDENT: ('a'..'z' | 'A'..'Z')+;
And a rule:
CLASS: 'class';

Where the text 'class' can be matched by both rules, and somehow it  
ends up being matched by IDENT where the parser expects a CLASS  
token. This is a very common problem, and you can usually solve it by  
removing the CLASS rule altogether and replacing it with a  
testLiterals=true and simply write "class" in the parser, instead of  
CLASS.

To further debug it, you will probably have to turn of the default  
error handler, catch the exception, and investigate the actual token  
types that are available from the exception. Those token types are  
offsets into YourParser._tokenNames[], look there to find the token  
name.

HTH,
Martin


More information about the antlr-interest mailing list