[antlr-interest] Lexer error reporting

Bart Kiers bkiers at gmail.com
Wed Nov 23 08:57:34 PST 2011


Hi Bill,


On Wed, Nov 23, 2011 at 5:41 PM, Bill Andersen <bill.andersen at mac.com>wrote:

> Hi Folks...
>
> Been trying to figure out how to shut off default Lexer behavior to print
> messages to System.err, such as:
>
>        line 2:4 no viable alternative at character ' '
>
> Instead, I'd like to catch these and do something with them.  Overriding
> reportError(RecognitionException) doesn't work and no other option seems
> obvious.


Both the lexer and parser have a `reportError(...)` method, and my guess is
that you did something like this:

@members {
  @Override
  public void reportError(RecognitionException e) ...
}

which is a short-hand for:

@parser::members { // note the `parser::`
  @Override
  public void reportError(RecognitionException e) ...
}

But since a "no viable alternative" error is something that comes from the
lexer, you need to explicitly override the lexer method like this:

@lexer::members {
  @Override
  public void reportError(RecognitionException e) {
    System.out.println("CUSTOM ERROR...");
  }
}

Regards,

Bart.


More information about the antlr-interest mailing list