[antlr-interest] Error Message Question

Mike Arace mikearace at hotmail.com
Thu Nov 8 22:30:21 PST 2007


Thank you Curtis.  

Since my post I did more research and discovered @rulecatch which was able to give me most of the behavior I wanted, namely letting me handle the various RecognitionExceptions in my own way, or rethrow the exceptions to be handled further upstream.  I also ended up spelunking in the source to find answers; so much has changed since the 2.x series releases that nothing available using Google is current.  The new solutions are much more elegant, provided you can find them.

Thank you for the RecoverFromMismatch advice, that was one I hadn't figured out yet.  I was working around it by either typing everything perfectly or redirecting System.err to the floor. :)

Best Regards,
Mike

> To: antlr-interest at antlr.org
> From: NOSPAM at TheSnakePitDev.com
> Date: Thu, 8 Nov 2007 14:09:02 -0800
> Subject: Re: [antlr-interest] Error Message Question
> 
> 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!
> 

_________________________________________________________________
Help yourself to FREE treats served up daily at the Messenger Café. Stop by today.
http://www.cafemessenger.com/info/info_sweetstuff2.html?ocid=TXT_TAGLM_OctWLtagline
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071109/5264ae12/attachment-0001.html 


More information about the antlr-interest mailing list