2

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 Answers1

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.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • can you give me the link for that example?i just confuse. but thanks before lex li – Berto Tuwondila Feb 22 '12 at 06:43
  • I cannot give you a Java example, as I use ANTLR in C#. https://github.com/lextm/sharpsnmplib/blob/2f9e8d2d48547b37e7cb6d75cdcb0ffae29f82e3/SharpSnmpLib/Mib/SmiParser.cs This is a partial class defined in C#, which will be finally merged with the ANTLR generated SmiParser.cs file from Smi.g grammar file. SmiParser class is derived from ANTLR's BaseRecognizer. Here I override RecoverFromMismatchedToken function to simply throw an exception all the times. In Java you should use a similar way to override the function and then implement your own logic. – Lex Li Feb 22 '12 at 09:12
  • thanks lex li for that solution. for your case, that imposible to used the concept of ANTLR like this http://stackoverflow.com/questions/9262195/parsing-java-code-with-antlr-need-concept – Berto Tuwondila Feb 22 '12 at 14:31
  • @Berto: I think the `recoverFromMismatchedToken` will happen _before_ the AST is generated. You ought to be able to use the AST even with this approach. – sarnold Feb 22 '12 at 23:01