Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I diagnose ambiguities in my ANTLR4 grammar?

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.

like image 390
jonr Avatar asked Nov 10 '22 16:11

jonr


1 Answers

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);
}
like image 60
user1958322 Avatar answered Nov 14 '22 22:11

user1958322