ANTLR: error recovery and reporting

[亡魂溺海] 提交于 2019-12-12 20:51:48

问题


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!


回答1:


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 exception MismatchedTokenException.



来源:https://stackoverflow.com/questions/9388900/antlr-error-recovery-and-reporting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!