[antlr-interest] Access to lexer warning/error messages after parsing

Raphael Reitzig r_reitzi at cs.uni-kl.de
Mon Jun 30 23:54:43 PDT 2008


Hi!

I second that for I am about to write something quite similar. System.err
is no good in a user oriented GUI application.

I can think of two possibilties to integrate such behaviour in ANTLR:
* grammar option like "warnMode", i.e. with values "console" and "collect".
I'd like to have _one_ exceptions thrown if there ocurred any error while
parsing.
* possibility to set output stream for error messages via grammar option:
@errors { System.err } (default)
Implementation of either should be no obstacle (*guess*).

Greetings,
Raphael

On Mon, 30 Jun 2008 15:25:07 -0700, "Sumanto Biswas" <sumanto at gmail.com>
wrote:
> Hi,
> 
> Many times the lexer gives warnings but tries to continue. When the
> parser is not able to handle, then we get exception.
> 
> As an example, for an input for my lexer-parser, from the parser I get
> NoViableAltException, with the following (formatted) information
> :"Mesg: Line number:0, Column no:-1, Message:no viable alternative at
> input '<EOF>'". Earlier the lexer had given the following warning:
> "line 1:17 mismatched character '}' expecting '"'".
> 
> If I use antlr within a product, the lexer warning seems to be much
> better to show since its clear and the line and column nos are correct
> with respect to the input. But the messages are in the error stream,
> and not stored somewhere. So I have to do something like what is
> pasted below.
> 
> I think it would be nice to have a list of structures which contain
> the line no, column no, and the error message, and the exception so
> that programmatically it would be easy to get that to see warnings
> generated. Maybe something like 'getWarnings()' on the lexer.
> 
> Regards,
> Sumanto
> 
> ==========================================================
> @lexer::members {
>   private List<String> messages = new ArrayList<String>();
> 
>   public void displayRecognitionError(String[] tokenNames,
> RecognitionException e) {
>     String hdr = getErrorHeader(e);
>     String msg = getErrorMessage(e, tokenNames);
>     messages.add(hdr+" "+msg);
>   }
> 
>   public List<String> getMessages() {
>     return messages;
>   }
> 
>   // And other methods to get line no/column no as needed.
> }
> ==========================================================



More information about the antlr-interest mailing list