JavaCC语法中如何提及try-catch块

发布于 2024-12-15 09:47:32 字数 1164 浏览 2 评论 0原文

我正在尝试在 JavaCC 语法中实现错误报告和恢复,如 http://javacc.java.net/doc/errorrecovery.html

提到后代码;

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".
}

该文件无法由 JavaCC 解析,在“try”一词和

'void error_skipto(int kind)' .

“什么是执行此操作的正确方法?”

行处显示错误。提前致谢

This is the error that is coming

在此处输入图像描述

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2024-12-22 09:47:32

显然您使用的不是 JavaCC,而是 JTB 1.3.2。

JTB 可能有自己的 .jj 语法文件解析器,并且 JTB 可能不支持 try-catch,如图所示。在这种情况下,对相同的输入使用 JavaCC 应该会产生不同的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文