Tony Ennis
Tony Ennis

Reputation: 12289

how do I resolve this antlr ambiguity?

I have a 4000 line text file which is parsing slowly, taking perhaps 3 minutes. I am running the Intellij Antlr plugin. When I look at the profiler, I see this:

enter image description here

The time being consumed is the largest of all rules, by a factor of 15 or so. That's ok, the file is full of things I actually don't care about (hence 'trash'). However, the profiler says words_and_trash is ambiguous but I don't know why. Here are the productions in question. (There are many others of course...):

enter image description here

I have no idea why this is ambiguous. The parser isn't complaining about so_much_trash and I don't think word, trash, and OPEN_PAREN overlap.

What's my strategy for solving this ambiguity?

Upvotes: 0

Views: 593

Answers (1)

Mike Cargal
Mike Cargal

Reputation: 6785

It's ambiguous because, given your two alternatives for words_and_trash, anything that matches the first alternative, could also match the second alternative (that's the definition ambiguity in this context).

It appears you might be using a technique common in other grammar tools to handle repetition. ANTLR can do this like so:

words_and_trash: so_much_trash+;
so_much_trash: word
             | trash
             | OPEN_PAREN words_and_trash CLOSE_PAREN
             ;

You might also find the following video, useful: ANTLR4 Intellij Plugin -- Parser Preview, Parse Tree, and Profiling. It's by the author of ANTLR, and covers ambiguities.

Upvotes: 2

Related Questions