问题描述
我正在使用带有 c# 目标的 Antlr 4.这是我的语法的一个子集:
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: ....
...
...
我想接受有效的数据块作为 (term)s,当存在错误时,我想搜索下一个有效的术语并打印出导致错误的整个文本供用户手动分析.
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?
推荐答案
您需要为此创建自己的 IAntlrErrorStrategy
实现,然后设置 Parser.ErrorHandler
> 属性到您的错误策略实例.ANTLRErrorStrategy
接口和默认实现 DefaultErrorStrategy
可能会为实施错误策略提供有用的信息,但我必须警告您,创建自定义错误策略是一项具有有限文档的高级功能.预计实现者已经是 ANTLR 4 实现自适应 LL(*) 解析算法的专家(我们指的是研究人员级别的理解).
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).
这篇关于如何在 Antlr 4/c# 中控制错误处理和同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!