本文介绍了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 recoveryfor example in the source program like this :student input code :

FOR(int a=0;a<10;a++){
 b=b*a;
}

程序将报告如下:程序:你的意思是关键字 FOR 是 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:错误恢复和报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 09:43