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

Sumanto Biswas sumanto at gmail.com
Mon Jun 30 15:25:07 PDT 2008


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