I am trying to implement Error Reporting and Recovery in JavaCC grammar as given in http://javacc.java.net/doc/errorrecovery.html
After mentioning the code;
void Stm() :
{}
{
try {
(
IfStm()
|
WhileStm()
)
catch (ParseException e) {
error_skipto(SEMICOLON);
}
}
void error_skipto(int kind) {
ParseException e = generateParseException(); // generate the exception object.
System.out.println(e.toString()); // print the error message
Token t;
do {
t = getNextToken();
} while (t.kind != kind);
// The above loop consumes tokens all the way up to a token of
// "kind". We use a do-while loop rather than a while because the
// current token is the one immediately before the erroneous token
// (in our case the token immediately before what should have been
// "if"/"while".
}
The file was not able to parse by JavaCC showing error at 'try' word and at the line
'void error_skipto(int kind)' .
What is the correct way to do this?
Thanks in advance
This is the error that is coming
Apparently you are not using JavaCC, but JTB 1.3.2.
JTB presumably has its own parser for .jj grammar files, and it might be the case that JTB does not support try-catch as shown. In that case, using JavaCC on the same input should give you a different result.