[antlr-interest] Error Message Question

Curtis Clauson NOSPAM at TheSnakePitDev.com
Thu Nov 8 14:09:02 PST 2007


The entire error reporting and recovery system is not appropriately 
documented, and your frustration is well understood.

Both the lexer and parser call
   reportError(RecognitionException e)
to display an error message during a recognition exception.
This, in turn, calls
   displayRecognitionError(String[] tokenNames, RecognitionException e)
with the list of valid token names for error reporting.
This uses
   getErrorHeader(e)
   getErrorMessage(e, tokenNames)
to cobble together the message text, and
   emitErrorMessage(hdr + " " + msg)
to emit the message.

To customize the messages, override getErrorHeader() and 
getErrorMessage() to handle each type of RecognitionException, or 
override displayRecognitionError(...) to handle it all differently.

Override emitErrorMessage(...) to change how the messages are displayed. 
The default is to use System.err.println(msg).

Remember that the lexer has a hard-coded RecognitionException handler in 
nextToken() that calls reportError(re), and that CommonTokenStream lexes 
all tokens into the stream buffer on the first token fetch. You may need 
to override nextToken() to keep the flood of lexer error messages from 
occurring before any parser messages.

One other v3.0.1 bug-a-boo: In recoverFromMismatchedToken(...), which is 
called by the parser but never by the lexer, there is a hard-coded message
   System.err.println("BR.recoverFromMismatchedToken");
I believe that this is a debugging message that was not commented out 
like all the others. You also have to override this method to eliminate 
that call.

I had to spelunk the source to find all of this since these questions 
never get answered on this list. It has worked well for me so far.

I hope that helps.
-- Curtis


Mike Arace wrote:
> The application is a Java console app, and I am trying to get it so that 
> Antlr only outputs customized messages when lexing or parsing fails 
> entirely.  I found the getErrorMessage() function that can be overridden 
> in the lexer, but that only seems to address part of the error message 
> after the location of the exception, and also only seems to address 
> RecognitionExceptions; my Antlr installation (3.0.1) is very talkative 
> and tells me about exceptions, recoveries, replacements, etc. right in 
> stdout, and I would like to ignore these messages entirely.
> 
> I'm sure this is a solved problem already, but I am not finding anything 
> on the site, the Antlr book, or in Google that addresses this in a 
> application-wide way.  Any help would be greatly appreciated!



More information about the antlr-interest mailing list