2

After following the accepted answer's instructions of Handling errors in ANTLR4 question I stuck up with the following error.

CustomErrorListener.java:11: cannot find symbol
symbol : variable REPORT_SYNTAX_ERRORS
location: class CustomErrorListener

I understood that ways to handle errors in ANTLR4 were different from ANTLR3, and based on the aforementioned question and its answers I ended up implementing the following error listener.

public class DescriptiveErrorListener extends BaseErrorListener {
    public static DescriptiveErrorListener INSTANCE = new DescriptiveErrorListener();

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                        int line, int charPositionInLine,
                        String msg, RecognitionException e)
    {
        if (!REPORT_SYNTAX_ERRORS) {
            return;
        }

        String sourceName = recognizer.getInputStream().getSourceName();
        if (!sourceName.isEmpty()) {
            sourceName = String.format("%s:%d:%d: ", sourceName, line, charPositionInLine);
        }

        System.err.println(sourceName+"line "+line+":"+charPositionInLine+" "+msg);
    }
}

Unfortunately I could not find anything about this REPORT_SYNTAX_ERRORS field anywhere in the ANTLR documentation. Any clue on what this could come from?

Community
  • 1
  • 1
Dracoflamme
  • 47
  • 2
  • 7
  • 1
    The symbol is in your code, it's the first line of your method. You wrote `if (!REPORT_SYNTAX_ERRORS) {` – Cory Kendall Sep 02 '13 at 23:24
  • 1
    Yes, I noticed that, it must refer to some inherited field from a parent class I reckon, but I don't know what it is not where it come from. Intuitively I guess it must check if there is actually an error, otherwise return without doing anything (which I noticed when I commented it out and I has loads of NullPointerException), but I don't know why it does not compile nor what to do with it... – Dracoflamme Sep 03 '13 at 00:01

1 Answers1

1

It's declared in the same file you copied and pasted the DescriptiveErrorListener class from. Here is the declaration:

private static final boolean REPORT_SYNTAX_ERRORS = true;

When the value is false, the syntaxError method returns without displaying errors.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280