如何重定向“没有可行的替代”消息?

时间:2013-07-31 15:06:20

标签: error-handling antlr antlr3

我一直试图让SLF4J处理所有ANTLR错误消息,以便它们以正确的顺序显示在其他消息中,但我没有运气。

关于Error Reporting and Recovery上的示例,我尝试覆盖emitErrorMessagedisplayRecognitionErrorrecoverFromMismatchedToken

@members {
    private Logger logger = LoggerFactory.getLogger(getClass().getName());

    @Override
    public void displayRecognitionError(String[] token_names, RecognitionException e) {
        logger.error("error at " + TextUtil.join(" ", token_names), e);
    }

    @Override
    public void emitErrorMessage(String msg) {
        logger.error(msg);
    }

    @Override
    protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow) throws RecognitionException {
        throw new MismatchedTokenException(ttype, input);
    }
}

但是,我仍在STDERR上收到消息:

line 1:10 no viable alternative at character 'y'
line 1:12 no viable alternative at character 'y'

1 个答案:

答案 0 :(得分:1)

@members@parser::members的缩写,您发布的错误消息看起来是词法错误。尝试添加@lexer::members来处理它们。