How do I diagnose ambiguities in my ANTLR4 grammar?

北慕城南 提交于 2019-12-10 14:49:30

问题


Short version:

I am using

parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);

But when I run my parser I don't see any output to the affect of 'reportAmbiguity ...' as the ANTLR4 book shows. How can I see ambiguities in my grammar?

Long version:

The parsing speed I currently get with ANTLR4 is around 90kb/s on a 8-core 2.67ghz Xeon E5640 machine. The lexing speed is about 5mb/s, so that is fine. While trying to optimize my parser I discovered two stage parsing, where the first stage parses using PredictionMode.SLL, and if that fails then the second stage uses PredictionMode.LL. Using these two modes works, and I see some inputs succeed with SLL in less time than they would have taken with LL.

The issue is I would like all (or most) to succeed with SLL. I assume I need to remove ambiguities from my grammar to accomplish this. I would like ANTLR4 to tell me about any ambiguities so that I may resolve them.


回答1:


I was facing same problem when I removed ConsoleErrorListener with parser.removeErrorListener();. You can check you listeners with code bellow

for (ANTLRErrorListener listener : parser.getErrorListeners()) {
        System.out.println(listener);
}


来源:https://stackoverflow.com/questions/29441249/how-do-i-diagnose-ambiguities-in-my-antlr4-grammar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!