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
c # antlr4


source to share


No one has answered this question yet

Check out similar questions:

6155
What is the difference between string and string in C #?
3575
How to list a transfer?
2397
What are the correct version numbers for C #?
2058
How do I get a consistent byte representation of strings in C # without manually specifying the encoding?
1877
What is a NullReferenceException and how do I fix it?
1786
How to create Excel file (.XLS and .XLSX) in C # without installing Microsoft Office?
1743
What's the best way to give a C # auto property an initial value?
1742
How to calculate a person's age in C #?
1404
What do the two question marks mean in C #?
1204
What does the Enlue [Flags] Enum attribute mean in C #?



All Articles
Loading...
X
Show
Funny
Dev
Pics