处理ANTLR中的错误4 [英] Handling errors in ANTLR 4

查看:192
本文介绍了处理ANTLR中的错误4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在遵循接受的答案说明之后/ 2279200>处理ANTLR4中的错误问题我遇到以下错误。

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


CustomErrorListener.java:11:不能找到符号

符号:变量REPORT_SYNTAX_ERRORS

位置:class CustomErrorListener

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

我明白处理ANTLR4错误的方法与ANTLR3不同,根据上述问题及其答案,我最终实施了以下错误侦听器。

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);
    }
}

不幸的是,我找不到任何关于这个 REPORT_SYNTAX_ERRORS 字段在ANTLR文档的任何地方。任何关于这可能来自哪里的线索?

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?

推荐答案

它在 class from。这是声明:

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;

当值为 false 时, code> syntaxError 方法返回而不显示错误。

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

这篇关于处理ANTLR中的错误4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆