[antlr-interest] How to feedback to users the string expected on MismatchedTokenException

Silvester Pozarnik silvester.pozarnik at tracetracker.com
Tue Jun 12 06:52:38 PDT 2007


In the java runtime, when the antlr 3.0 encounter a mismatched token
parser error it issue a message like:

	"line 1:7 mismatched input 'update' expecting SELECT"

What this basically means is that parser got an 'update' string while
expecting the SELECT token. 

The problem is that SELECT token is defined as:

	...
	tokens {
  		...
		SELECT = 'select';
		...

And that I should prefer to respond to such erroneous input with the
string that is expected ('select') and not with the token type (SELECT).
As the antlr 3.0 is case sensitive the reported error is not correct.
The problem is that the generated xxx.tokens (SELECT=5) maps the
uppercase value to token type ID and that the expected value ('select')
is buried in the generated Lexer java code as:

	...
	match("select"); if (failed) return ;
	...

and not accessible to the error reporting. The solution may be to enable
additional argument to MismatchedTokenException containing the expected
string and change accordingly the Lexer.class match(String s) method.

If there is some other way to achieve this, could someone enlighten me
please? 

BR
Silvester Pozarnik




More information about the antlr-interest mailing list