ANTLR:错误恢复和报告 [英] ANTLR: error recovery and reporting

查看:98
本文介绍了ANTLR:错误恢复和报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ANTLR v3进行恢复和重新发布错误时遇到问题.我点击此链接 http://www.antlr.org/blog/antlr3/error .handling.tml ,但我没有该解决方案. 我想进行一些报告和恢复 例如在这样的源程序中: 学生输入代码:

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;
}

,程序将报告如下: 程序:您的意思是关键字FOR用于吗?" 学生回答:是" 之后,系统会自动恢复并修改源代码. 使用ANTLR v3怎么办?无法使用ANTLR? 需要帮忙.谢谢大家!

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!

推荐答案

我认为您需要在生成的解析器类中覆盖org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken.

I think you need to override org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken inside your generated parser class.

当ANTLR检测到无效令牌时,将调用此函数.因此,在您自己的功能中,您可以询问用户是否需要恢复.

This function is called when ANTLR detects an invalid token. So in your own function you can asks the user whether recovery is needed.

如果需要,则可以调用BaseRecognizer.recoverFromMismatchedToken进行恢复.如果不是,则可以引发异常MismatchedTokenException.

If needed, then you can call BaseRecognizer.recoverFromMismatchedToken to perform the recovery. If not, you can throw an exception MismatchedTokenException.

这篇关于ANTLR:错误恢复和报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆