JavaCC/JJTree 节点类中缺少字段?

发布于 2024-10-24 01:31:25 字数 1330 浏览 1 评论 0原文

我从JavaCC“继承”了一个使用JJTree的项目来实现一个简单的语言解析器。由于代码已有 5 年多了,我决定在进行任何实际开发之前将所有依赖项(包括 JavaCC)更新到最新版本。

因此,我删除了所有JJTree/JavaCC生成的文件并使用最新版本(5.0)重建它们。然而,生成的文件似乎缺少以前存在的字段和方法,并且代码不再编译。

我假设 JavaCC 中发生了一些变化,需要我更新语法文件,因此我尝试了当前 JavaCC 发行版中的 Interpretter 示例,得到了相同的结果:缺少不允许代码的类字段来编译。

例如,以下是 Interpretter 示例的 SPL.jjt 的定义:

void Id() :
{
   Token t;
}
{
   t = <IDENTIFIER>  { jjtThis.name = t.image; }
}

此示例的文件夹包含一个 ASTId.java 文件,该文件最初有这样的内容:

public class ASTId extends SimpleNode {
  String name;

  public ASTId(int id) {
    super(id);
  }

  public ASTId(SPLParser p, int id) {
    super(p, id);
  }

  public void interpret()
  {
     stack[++top] = symtab.get(name);
  }
}

在我重新生成 AST*.java 文件后,内容发生了变化:

public class ASTId extends SimpleNode {
  public ASTId(int id) {
    super(id);
  }

  public ASTId(SPLParser p, int id) {
    super(p, id);
  }
}

这里缺少很多,因此 SPLParser.java< /code> 生成的文件无法编译,因为它使用了相应类中未定义的字段。

我缺少什么?是否有我必须使用的 JJTree 或 JavaCC 选项?也许要在语法文件中进行修改?或者,由于我真的不知道原始文件是否已被编辑,我是否应该直接修改生成的文件并手动添加缺失的位?

我没有使用 JavaCC 的经验,因此我希望得到任何解决此问题的提示。

I "inherited" a project that uses JJTree from JavaCC to implement a simple language parser. Since the code was over 5 years old, I decided to update all dependencies, including JavaCC, to the latest release before I do any actual development.

Therefore, I deleted all the files generated by JJTree/JavaCC and used the latest version (5.0) to rebuild them. The resulting files, however, seem to miss fields and methods that were previously present and the code does not compile any more.

I assumed that something changed in JavaCC that requires me to update the grammar file, so I tried out the Interpretter example from the current JavaCC distribution, with the same results: missing class fields that do not allow the code to compile.

For example, here is a definition from SPL.jjt of the Interpretter example:

void Id() :
{
   Token t;
}
{
   t = <IDENTIFIER>  { jjtThis.name = t.image; }
}

The folder for this example contains an ASTId.java file which originally had this content:

public class ASTId extends SimpleNode {
  String name;

  public ASTId(int id) {
    super(id);
  }

  public ASTId(SPLParser p, int id) {
    super(p, id);
  }

  public void interpret()
  {
     stack[++top] = symtab.get(name);
  }
}

After I regenerated the AST*.java files, the content changed:

public class ASTId extends SimpleNode {
  public ASTId(int id) {
    super(id);
  }

  public ASTId(SPLParser p, int id) {
    super(p, id);
  }
}

There is a lot missing here and as a result the SPLParser.java generated file does not compile because it uses fields that are not defined in the corresponding classes.

What am I missing? Is there a JJTree or JavaCC option that I have to use? Perhaps a modification to make in the grammar file? Or, as I don't really know if the original files have been edited, am I supposed to directly modify the generated files and add the missing bits manually?

I have no experience with JavaCC, so I would appreciate any hints to resolve this issue.

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

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

发布评论

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

评论(2

汐鸠 2024-10-31 01:31:25

编辑生成的节点文件是一种非常标准的做法……但解析器文件和令牌管理器不应更改;这就是 TOKEN_MGR_DECLS 和语法文件中的代码的用途。

人们编辑这些文件的原因是它们不会经常更改......当然,当它们更改时,会有点痛苦。

It's a pretty standard practice to edit the generated nodes files... the parser file and the token manager shouldn't be changed though; that's what TOKEN_MGR_DECLS and the code inside the grammar file is for.

The reason folks edit these files is that they don't change very often... although of course when they do, it's a bit of a pain.

硬不硬你别怂 2024-10-31 01:31:25

缺少的内容可能是由原始开发人员编辑的。这并不一定表明 JavaCC 或 JJTree 中发生了某些变化。这些都是比较成熟的项目。

希望原始代码已签入版本控制系统或进行备份,以便您可以取回代码。也许删除并重新编译的决定并不是最好的。

The stuff that's missing was probably edited in by the original developers. It's not necessarily a sign that something has changed in JavaCC or JJTree. Those are relatively mature projects.

Here's hoping that the original was checked into a version control system or backed up so you can get the code back. Perhaps the decision to delete and recompile wasn't the best.

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