How to silence ANTLR4’s error messages

By default a org.antlr.v4.runtime.Parser instance, as a subclass of org.antlr.v4.runtime.Recognizer, will have a org.antlr.v4.runtime.ConsoleErrorListener attached to it. Error messages will be piped through this listener and out to System.err, where they will be lost forever.

You can silence this output by calling removeErrorListeners() before parsing, and if you want to capture the error messages for parsing and display, you can add a custom BaseErrorListener implementation.

Here’s a simple one that keeps every error in a List:

You would add it to a Parser using the addErrorListener method.

Here’s a complete example: