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

Terence Parr parrt at cs.usfca.edu
Tue Jul 1 12:19:10 PDT 2008


Hi Gang, :)

My intention was that you override one method and do what you want.   
Not sure what else anybody would want. Replace System.err in override  
with errors.add(...) and you're done.  Not sure what the long  
conversation is about.  Can't think of an easier way.

Ter

On Jun 30, 2008, at 3:25 PM, Sumanto Biswas 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