gpt4 book ai didi

java - 在antlr4中向Lexer/Parser文件添加自定义异常的正确方法

转载 作者:行者123 更新时间:2023-12-01 09:40:59 25 4
gpt4 key购买 nike

我有一个习惯ParsingException(字符串消息,int位置,字符串违规文本)

我希望我的解析器在遇到解析/词法错误时抛出此异常。

这是正确的吗?

@parser::members
{
@Override
public void notifyErrorListeners(Token offendingToken, String msg, RecognitionException ex)
{
throw new ParsingException(msg,offendingToken.getStartIndex(),offendingToken.getText());
}
}
@lexer::members {
@Override
public void recover(RecognitionException ex)
{
throw new ParsingException(ex.getMessage(),getCharPositionInLine(),ex.getOffendingToken().getText());
}
}

我收到一个 UnhandledException 错误。

最佳答案

您应该重写 BaseErrorListenersyntaxError 方法,而不是 notifyErrorListenersrecover ,如下所述: Handling errors in ANTLR4

关于java - 在antlr4中向Lexer/Parser文件添加自定义异常的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38456912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com