NoViableAltException not thrown by execATN

17 views
Skip to first unread message

James Taylor

unread,
Dec 16, 2022, 7:08:33 PM12/16/22
to antlr-discussion
Hello,
Not sure if I'm encountering a bug or if this is expected behavior. I have an error listener that is trying to detect which rule was attempted to be matched when an error occurred, but ANTLR is reporting the parent rule instead of the child rule. I've traced it to this line which generates a NoViableAltException (the one that occurs based on the child rule failing), but then because p.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule returns alt=2, the exception isn't thrown. Instead an InputMisMatchException is thrown later for the parent rule. From my error listener, I can't seem to find a way of discovering that the error actually occurred in the child rule.

I have a couple of workarounds:
  1. Use a ParseListener and detect the child rule was entered
  2. Look at the outerContext of the Interpreter from my error listener (seems kind of hacky, though)
Is there a way of knowing which rule failed from my error listener in this case? Or is my workaround the best way to go?

Thanks,
James
Reply all
Reply to author
Forward
0 new messages