Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
798 views
in Technique[技术] by (71.8m points)

grammar - How to control error handling and synchronization in Antlr 4 / c#

I'm using Antlr 4 with c# target. Here is a subset of my grammar:

/*
 * Parser Rules
 */
text : term+  EOF;
term : a1 a2 a3;
a1: ....
...
...

I want to accept valid data blocks as (term)s, when error exists I want to search for the next valid term and print out the whole text which caused the error for user to analyze manually.

How to synchronize input to the next valid term? and How to get the ignored text?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will need to create your own implementation of IAntlrErrorStrategy for this, and then set the Parser.ErrorHandler property to an instance of your error strategy. The documentation for the Java versions of the ANTLRErrorStrategy interface and default implementation DefaultErrorStrategy may provide useful information for implementing an error strategy, but I must warn you going in that creating a custom error strategy is an advanced feature with limited documentation. It's expected that the implementer is already an expert in ANTLR 4's implementation of the Adaptive LL(*) parsing algorithm (we're talking researcher-level understanding).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...