How can I tell the ANTLR4 parser in C # what to do with the parser errors?

I am using ANTLR4, C # -Target to validate a data structure. Basically, I created an EBNF grammar that describes the allowed sequence of occurrences in certain elements. The file itself contains many items and I am using ANTLR to check each item in one operation (they are different, independent and separate items, so I cannot parse the whole file). Typically, I could have a file containing about 5000 items, about 1000 of them are invalid.

Retrieving individual items from a file takes about half a second with ANTLR on my machine, but when I try to inspect each of the items, I get many NoViableAltExceptions or RecognitionExceptions coming from the generated parser and they cause significant performance degradation. A file without any invalid objects can be parsed in one second, but when I have about 1000 errors it can take up to a minute.

Basically, I just want ANTLR to tell me if the input assignment matches a given rule, if not, I want ANTLR to just report an error, abort the parsing process (since I just want to get a true or false answer) and NOT throw away an exception.

I reworked with overriding / inheriting from BaseErrorListener, DefaultErrorStrategy and Parser itself, and was somewhat able to change the behavior of the generated parser, but I still have one problem:

The auto-generated file itself gets the line:

                default:
                    throw new NoViableAltException(this);

      

Is there a way to tell ANTLR4 how to handle errors in the auto-generated code since that can't be taken care of by overriding the framework class?

Thank!

+3


source to share





All Articles