Get rid of token recognition error

Hakan picture Hakan · Mar 14, 2014 · Viewed 14.3k times · Source

How can I get rid of the default ANTLR recognition error?

I want to write another message using my own error class instead of ANTLR's error.

I mean is there any possibility that some ANTLR error classes can be extended in order to display my own message?

More clearly, I do not want to see the following error message in my console :

token recognition error at:

Answer

Sam Harwell picture Sam Harwell · Mar 15, 2014

If you simply want to suppress the messages, you can call lexer.removeErrorListeners(). However, a better approach is writing your lexer rules such that all possible input is tokenized, with the following rule at the end of the lexer. This will cause all error reporting to go through the parser instead of both the parser and lexer.

// handle characters which failed to match any other token
ErrorCharacter : . ;