1

我正在尝试在JavaCC语法中实现错误报告和恢复

我在.jjt语法文件中提到了以下代码

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

当我执行命令时jjtree CMinus.jjt,出现以下错误:

从文件 CMinus_ragu.jjt 中读取。. . 解析输入时出错:org.javacc.jjtree.ParseException:在第 111 行第 30 列遇到“”{“”{“”。期待以下之一:“throws”...“:”...“#”.. .

代码中的错误是什么,我应该如何处理错误恢复?

4

1 回答 1

1

语法文件中的错误处理代码之前应添加关键字 JAVACODE。因此,该方法应如下所示:

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

这是因为在使用 java 风格制作之前应该添加关键字 JAVACODE。

于 2012-03-17T16:53:41.230 回答