JJTree 中的 SimpleNode 子节点

发布于 2024-12-19 05:49:37 字数 257 浏览 1 评论 0原文

我在教程中读到,当您执行类似操作时:

SimpleNode Program() #Program : {} 
{
    (Class_decl())* <EOF>

    { return jjtThis; }
}

它应该创建一个继承自 SimpleNode 的 Programm 类(一旦您使用 jjtree 进行编译),但在我的情况下,它不会创建这样的类。我必须自己创建它还是我错过了什么?

谢谢

I read in a tutorial that when you do something like that :

SimpleNode Program() #Program : {} 
{
    (Class_decl())* <EOF>

    { return jjtThis; }
}

it should create a Programm class which inherits from SimpleNode (once you compile with jjtree), but in my case it doesn't create such as class. Do I have to create it myself or am I missing something ?

Thanks

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

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

发布评论

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

评论(1

抹茶夏天i‖ 2024-12-26 05:49:37

您是否首先在语法上使用jjtree(在javacc之前)?它对我来说工作得很好:

文件:Test.jjt

options {
  MULTI=true;
  NODE_PREFIX="";
}

PARSER_BEGIN(Test)
public class Test {
  public static void main(String[] args) throws Exception {
    Test parser = new Test(new java.io.StringReader("class A; class B;"));
    SimpleNode root = parser.Program();
    root.dump("");
  }
}
PARSER_END(Test)

TOKEN :
{
     < CLASS : "class" >
  |  < SCOL  : ";" >
  |  < ID    : (["a"-"z","A"-"Z"])+ >
}

SKIP :
{
  " " | "\t" | "\r" | "\n"
}

SimpleNode Program() #Program : 
{} 
{
  (Class_decl())* <EOF> {return jjtThis;}
}

void Class_decl() #ClassDecl :
{}
{    
  <CLASS> <ID> <SCOL>
}

然后命令:

jjtree Test.jjt
javacc Test.jj

生成以下 Java 源文件:

ClassDecl.java
JJTTestState.java
Node.java
Program.java
SimpleNode.java
TestTreeConstants.java

并且 ClassDeclProgram 都扩展 SimpleNode:

/* Generated By:JJTree: Do not edit this line. Program.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=false,TRACK_TOKENS=false,NODE_PREFIX=,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public
class Program extends SimpleNode {
  ...
}

/* Generated By:JJTree: Do not edit this line. ClassDecl.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=false,TRACK_TOKENS=false,NODE_PREFIX=,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public
class ClassDecl extends SimpleNode {
  ...
}

编译所有源文件并运行 Test 类:

javac -cp . *.java
java -cp . Test

将打印:(

Program
 ClassDecl
 ClassDecl

Program 是具有两个 ClassDecl 子级的根)

已测试带Java编译器的编译版本5.0。

Are you first using jjtree on your grammar (before javacc)? It works fine with me:

file: Test.jjt

options {
  MULTI=true;
  NODE_PREFIX="";
}

PARSER_BEGIN(Test)
public class Test {
  public static void main(String[] args) throws Exception {
    Test parser = new Test(new java.io.StringReader("class A; class B;"));
    SimpleNode root = parser.Program();
    root.dump("");
  }
}
PARSER_END(Test)

TOKEN :
{
     < CLASS : "class" >
  |  < SCOL  : ";" >
  |  < ID    : (["a"-"z","A"-"Z"])+ >
}

SKIP :
{
  " " | "\t" | "\r" | "\n"
}

SimpleNode Program() #Program : 
{} 
{
  (Class_decl())* <EOF> {return jjtThis;}
}

void Class_decl() #ClassDecl :
{}
{    
  <CLASS> <ID> <SCOL>
}

And then the commands:

jjtree Test.jjt
javacc Test.jj

which generates the following Java source files:

ClassDecl.java
JJTTestState.java
Node.java
Program.java
SimpleNode.java
TestTreeConstants.java

And both ClassDecl and Program extend SimpleNode:

/* Generated By:JJTree: Do not edit this line. Program.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=false,TRACK_TOKENS=false,NODE_PREFIX=,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public
class Program extends SimpleNode {
  ...
}

/* Generated By:JJTree: Do not edit this line. ClassDecl.java Version 4.3 */
/* JavaCCOptions:MULTI=true,NODE_USES_PARSER=false,VISITOR=false,TRACK_TOKENS=false,NODE_PREFIX=,NODE_EXTENDS=,NODE_FACTORY=,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
public
class ClassDecl extends SimpleNode {
  ...
}

Compiling all source files and running the Test class:

javac -cp . *.java
java -cp . Test

which will print:

Program
 ClassDecl
 ClassDecl

(i.e. Program is the root with two ClassDecl children)

Tested with Java Compiler Compiler Version 5.0.

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