处理 ANTLR 4 中的错误 [英] Handling errors in ANTLR 4
问题描述
按照接受的答案处理 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
位置:类 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);
}
}
不幸的是,我在 ANTLR 文档的任何地方都找不到关于此 REPORT_SYNTAX_ERRORS
字段的任何信息.关于这可能来自什么的任何线索?
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?
推荐答案
它在 相同的文件,您从中复制并粘贴了 DescriptiveErrorListener
类.这是声明:
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
时,syntaxError
方法返回而不显示错误.
When the value is false
, the syntaxError
method returns without displaying errors.
这篇关于处理 ANTLR 4 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!