使用有用的消息中止解析错误

时间:2016-03-10 18:50:02

标签: java parsing antlr antlr4 error-reporting

我有一个ANTLR 4语法,并从中构建了一个词法分析器和解析器。现在我试图以这样的方式实例化该解析器,它将解析直到遇到错误。如果遇到错误,则不应继续解析,但应提供有关问题的有用信息;理想情况下,机器可读的位置和人类可读的消息。

这就是我现在所拥有的:

grammar Toy;

@parser::members {

    public static void main(String[] args) {
        for (String arg: args)
            System.out.println(arg + " => " + parse(arg));
    }

    public static String parse(String code) {
        ErrorListener errorListener = new ErrorListener();
        CharStream cstream = new ANTLRInputStream(code);
        ToyLexer lexer = new ToyLexer(cstream);
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);
        TokenStream tstream = new CommonTokenStream(lexer);
        ToyParser parser = new ToyParser(tstream);
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);
        parser.setErrorHandler(new BailErrorStrategy());
        try {
            String res = parser.top().str;
            if (errorListener.message != null)
                return "Parsed, but " + errorListener.toString();
            return res;
        } catch (ParseCancellationException e) {
            if (errorListener.message != null)
                return "Failed, because " + errorListener.toString();
            throw e;
        }
    }

    static class ErrorListener extends BaseErrorListener {

        String message = null;
        int start = -2, stop = -2, line = -2;

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer,
                                Object offendingSymbol,
                                int line,
                                int charPositionInLine,
                                String msg,
                                RecognitionException e) {
            if (message != null) return;
            if (offendingSymbol instanceof Token) {
                Token t = (Token) offendingSymbol;
                start = t.getStartIndex();
                stop = t.getStopIndex();
            } else if (recognizer instanceof ToyLexer) {
                ToyLexer lexer = (ToyLexer)recognizer;
                start = lexer._tokenStartCharIndex;
                stop = lexer._input.index();
            }
            this.line = line;
            message = msg;
        }

        @Override public String toString() {
            return start + "-" + stop + " l." + line + ": " + message;
        }
    }

}

top returns [String str]: e* EOF {$str = "All went well.";};
e: 'a' 'b' | 'a' 'c' e;

将其保存到Toy.g,然后尝试以下命令:

> java -jar antlr-4.5.2-complete.jar Toy.g
> javac -cp antlr-4.5.2-complete.jar Toy*.java
> java -cp .:tools/antlr-4.5.2-complete.jar ToyParser ab acab acc axb abc
ab => All went well.
acab => All went well.
acc => Failed, because 2-2 l.1: no viable alternative at input 'c'
axb => Parsed, but 1-1 l.1: token recognition error at: 'x'
Exception in thread "main" org.antlr.v4.runtime.misc.ParseCancellationException
    at org.antlr.v4.runtime.BailErrorStrategy.recoverInline(BailErrorStrategy.java:90)
    at org.antlr.v4.runtime.Parser.match(Parser.java:229)
    at ToyParser.top(ToyParser.java:187)
    at ToyParser.parse(ToyParser.java:95)
    at ToyParser.main(ToyParser.java:80)
Caused by: org.antlr.v4.runtime.InputMismatchException
    at org.antlr.v4.runtime.BailErrorStrategy.recoverInline(BailErrorStrategy.java:85)
    ... 4 more

一方面,我觉得我已经做得太多了。看看我写了多少代码应该是一个简单而常见的任务,我不禁想知道我是否缺少一些简单的解决方案。另一方面,由于两个原因,即便这样也不够。首先,当我设法报告lexer错误时,它们仍然不会阻止解析器继续处理剩余的流。这是输入Parsed, but的{​​{1}}字符串的证据。其次,我仍然留下错误,这些错误不会报告给错误监听器,如堆栈跟踪所示。

如果我没有安装axb,我会得到更有用的输出:

BailErrorStrategy

有没有办法得到这种错误信息,但仍然会因错误而保释?我可以see from the sources acc => Parsed, but 2-2 l.1: mismatched input 'c' expecting 'a' axb => Parsed, but 1-1 l.1: token recognition error at: 'x' abc => Parsed, but 2-2 l.1: extraneous input 'c' expecting {<EOF>, 'a'} 消息确实是由extraneous input生成的,显然是在它确定了如何修复问题之后。我应该让它做到这一点然后然后纾困,即写下我自己的DefaultErrorStrategy变体,在投掷之前调用super?

2 个答案:

答案 0 :(得分:8)

In the same situation I ended up with extending DefaultErrorStrategy and overriding report* methods. It's pretty straightforward (you can use ANTLRErrorStrategy as well).

Here you can find an example of fail-fast strategy. I think in your case you can collect all errors in the same way and build detailed report.

答案 1 :(得分:1)

一种方法可能是修改错误侦听器而不是错误策略。可以将默认策略与以下侦听器一起使用:

class ErrorListener extends BaseErrorListener {
    @Override
    public void syntaxError(Recognizer<?, ?> recognizer,
                            Object offendingSymbol,
                            int line,
                            int charPositionInLine,
                            String msg,
                            RecognitionException e) {
        throw new ParseException(msg, e, line);
    }
}

class ParseException extends RuntimeException {
    int line;
    public ParseException(String message, Throwable cause, int line) {
        super(message, cause);
        this.line = line;
    }
}

这样就可以将错误格式化为输出格式,但是要报告的第一个错误将导致编译因抛出命名异常而中止。由于这是一个未经检查的异常,因此必须确保捕获它,因为如果您忘记这样做,编译器不会发出警告。

关于机器可读的位置,如果除了行号之外你还想要输入的有问题部分的源文本偏移,这样的代码似乎在syntaxError方法内工作:

        int start = 0, stop = -1;
        if (offendingSymbol instanceof Token) {
            Token t = (Token) offendingSymbol;
            start = t.getStartIndex();
            stop = t.getStopIndex();
        } else if (recognizer instanceof Lexer) {
            Lexer lexer = (Lexer)recognizer;
            start = lexer._tokenStartCharIndex;
            stop = lexer._input.index();
        }