ANTLR:错误恢复和报告
我在使用 ANTLR v3 恢复和重新发布错误时遇到问题。我点击此链接 http://www.antlr.org/blog/antlr3/error .handling.tml 但我没有那个解决方案。 我想做一些报告和恢复 例如在源程序中是这样的: 学生输入代码:
FOR(int a=0;a<10;a++){
b=b*a;
}
程序将报告如下: 程序:“您的意思是关键字 FOR 是 for 吗?” 学生回答:“是” 之后系统自动恢复并修改源代码。 如何使用 ANTLR v3 做到这一点?使用 ANTLR 不可能做到这一点? 需要帮助。谢谢你们!
i have problems to recovery and reposting error with ANTLR v3. i follow this link http://www.antlr.org/blog/antlr3/error.handling.tml but i don't have that solutions.
i want to make some reporting and recovery
for example in the source program like this :
student input code :
FOR(int a=0;a<10;a++){
b=b*a;
}
and the program will report like this:
Program : "are you meant the keyword FOR is for?"
student answer:"yes"
after that, the system recovery and modified the source code automatically.
How to do like that with ANTLR v3?imposible to do with ANTLR?
need help. thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要在生成的解析器类中重写 org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken 。
当 ANTLR 检测到无效令牌时调用此函数。因此,在您自己的函数中,您可以询问用户是否需要恢复。
如果需要,您可以调用 BaseRecognizer.recoverFromMismatchedToken 来执行恢复。如果没有,您可以抛出异常
MismatchedTokenException
。I think you need to override
org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken
inside your generated parser class.This function is called when ANTLR detects an invalid token. So in your own function you can asks the user whether recovery is needed.
If needed, then you can call
BaseRecognizer.recoverFromMismatchedToken
to perform the recovery. If not, you can throw an exceptionMismatchedTokenException
.