按照accepted answer's问题的Handling errors in ANTLR4说明操作后,我发现了以下错误。
CustomErrorListener.java:11:找不到符号
符号:变量REPORT_SYNTAX_ERRORS
location:类CustomErrorListener
我知道处理ANTLR4中的错误的方法与ANTLR3不同,基于上述问题及其答案,我最终实现了以下错误监听器。
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);
}
}
不幸的是,在ANTLR文档中的任何地方都找不到关于此REPORT_SYNTAX_ERRORS
字段的任何内容。关于这可能来自何方的任何线索?
答案 0 :(得分:1)
在same file中声明了您复制并粘贴了DescriptiveErrorListener
类。这是宣言:
private static final boolean REPORT_SYNTAX_ERRORS = true;
当值为false
时,syntaxError
方法将返回而不显示错误。