我正在尝试使用 antlr4 为我的简单语法编写一些错误检查。
语法本身是由函数构造的。
IE
FUNCTION hello (n){
......
}
FUNCTION main (n) {
......
}
我不确定它应该如何捕获特定错误,例如缺少函数名称或缺少主函数
这是我的 ErrorListener 的样子
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class SimpleErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {
List<String> stack = ((Parser) recognizer.getRuleInvocationStack();
Collections.reverse(stack);
System.err.println("rule stack: " + stack);
System.err.println("line" + line + ":" +
charPositionInLine + "at" + offendingSymbol + ": " + msg);
}
}
我还删除了控制台错误侦听器并添加了这个,但我不知道如何处理这些特定错误。任何建议表示赞赏。非常感谢。