Handling errors in ANTLR 4

房东的猫 提交于 2020-01-04 14:11:38

问题


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?


回答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.



来源:https://stackoverflow.com/questions/18581880/handling-errors-in-antlr-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!