JavaCC技术手册之JJTree参考文档

javacc+jjtree使用教程 minijava编译器的核心工具,javacc+jjtree使用教程,帮助进行词法分析、语法分析、建立抽象语法树 立即下载

         不知道大家了解Lucence这个项目么,一个开源的搜索引擎。不过想看懂很难,因为需要很多相关的知识,我这里就翻译了一篇文档,JavaCC技术手册之JJTree参考文档,Lucence在一些主要的文法分析中用到了JavaCC,而JJTree是JavaCC的一个预处理。下面就是翻译:

JavaCC技术手册:JJTree参考文档 [翻译:Kevin Wang]

简洁

    JJTree在JavaCC源代码的不同地方嵌入语法树构建动作,可以理解为JavaCC的预处理工具。通过运行JavaCC创建解析器生成JJTree输出文件。这篇文档描述了如何使用JJTree,以及如何从中分离你的解析器。

    JJTree默认为每个非终结符生成代码来创建语法分析树节点。这种行为可以被修改以便于一些非终结符不生成节点或者因为一部分内容扩充而生成一个节点。

       JJTree已经定义了一个所有语法分析树节点必须实现的接口Node,这个接口定义诸如:设置节点的父节点、增加子节点、重新获得子节点 等操作方法。

       JJTree(为想要更多条件)可以设置 “simple” 和 “multi”两种模式之一。在“simple”[单一]模式下语法分析树节点都是“SimpleNode”这个具体类型。而在“multi”[多个]模式下语法分析树节点类型取决于节点的名字。如果你不为“Node”接口提供实现JJTree会为你生成一些基于“SimpleNode”的样品实现,你可以修改这个实现以适应需求。

       虽然JavaCC是一个从顶至下的解析器(即LL(K)  通常我们只要用LL(1)即可 ),但是JJTree构造语法树过程却是从底至上构建的。为此使用堆栈,当他们被创建后它可以把节点压入堆栈中。当它找到一个父节点时,从堆栈中弹出子节点并且将其添加到父节点下,最后把新的父节点压入堆栈。堆栈是开放的意味着你可以使用内部语法行为对其访问:入堆栈,出堆栈,另外不管你有多么的适应请熟练使用它的内容。更多的重要信息请参看下面“节点作用域和用户行为”章节的介绍。

       JJTree提供2种基本节点种类定义,可以依据造句法的缩写使其使用起来更方便。

1、  明确节点:一个以指定子节点数创建的节点。其中许多节点可以被弹出堆栈成为新节点的子节点,然后把这个新节点压入栈。你可以像这样定义一个明确节点:

        #ADefiniteNode(INTEGER EXPRESSION)

   虽然INTEGER是目前为止使用最多的表达式,但明确节点参数“EXPRESSION”可以使用任何INTEGER 表达式。

2、  条件节点:当且仅当条件值为True时,带着所有被压入节点区域内部的子节点创建条件节点。如果条件值为False节点不会被创建,所有的子节点将残留在节点堆栈中。你可以像这样定义一个条件节点:

#ConditionalNode(BOOLEAN EXPRESSION)

       一个条件节点描述符“EXPRESSION”可以使用任何BOOLEAN 表达式。下边有2个常用的条件节点内容需要记忆:

                1、不确定节点

                      #IndefiniteNode 是 #IndefiniteNode(true) 的简写

          2、大节点

              #GTNode(>1) 是 #GTNode(jjtree.arity() > 1) 的简写

 

       在JJTree源代码中,当简写的不确定节点表达跟着一个括号表达式时,可能导致歧义。在那种情况下简写必须用完全写法替代。例如:

            ( ... ) #N ( a() ) 

       上面表达式逻辑不清,你必须明确的使用条件式:

          ( ... ) #N(true) ( a() )

       注意:节点描述符表达式不能有副作用,JJTree并没有指定表达式将被计算多少次。

       默认情况下JJTree把每一个非终结符作为一个不确定节点对待,从它的产生式的名                  

字衍生出节点名字。也可以用下面的语法给他一个不同的名字:

          void P1() #MyNode : { ... } { ... }

当解析器识别出了一个P1非终结符,它先定义一个节点,并在堆栈中进行标记,以便于一些由扩展P1非终结符而构建和入栈的分析树节点被弹出并成为 MyNode节点的子节点。

       如果想要使某个产生式禁止创建节点,你可以使用下面的语法:

           void P2() #void : { ... } { ... }

    这样,一些根据非终结符入栈作为P2扩展的语法分析树节点会留在堆栈中,会被将来的产生式弹出作为其子节点,对non-decorated[非包装]节点你也可以通过使用NODE_DEFAULT_VOID选项使之成为默认行为。

               void P3() : {}
               {
                   P4() ( P5() )+ P6()
               }

    这个例子中,一个不确定的节点P3被启动并标记堆栈,然后是1个P4节点,1个或多个P5节点和1个P6节点被解析。被入堆栈的任何节点都会被弹出成为P3的子节点。你可以更深一步的定制生成树:

                 void P3() : {}
               {
                       P4() ( P5() )+ #ListOfP5s P6()
                       我认为这里是P4() ( P5() ) #ListOfP5s + P6()
     
                }

       现在P3节点会有一个P4节点、一个ListOfP5s节点和一个P6节点作为子节点。#Name结构表示一个后缀操作符,它的区域直接就是前面的扩展单元。

 

节点作用域和用户行为

    每个节点都有其作用域。这个作用域内的用户行为可以通过使用特殊标志符“jjtThis”来访问已被创建的节点。这个标志符被隐含的声明为节点的正确类型,这样此节点的一些字段、方法可以轻易的被访问。

一个作用域就是前面一个节点定义的扩展单位。这可能是一个括号表达式。当产生式符号被定义(或者是隐含定义的默认节点),它的作用域就是包括声明块在内的整个产生式的右侧。

你可以使用一个包括“jjtThis”扩展引用左侧的表达式。例如:

       ... ( jjtThis.my_foo = foo() ) #Baz ...

这里“jjtThis”引用了一个含有“my_foo”字段的“Baz”节点。foo() 产生式解析的结果是被赋给“my_foo”。

    节点作用域内用户的最终行为不同于所有的其他行为。当代码执行过程中,子节点已经被从堆栈中弹出并被添加给这个被压入堆栈的节点。这些孩子节点可以通过诸如这个节点的方法jjtGetChild().进行访问。除了最终用户行为之外的用户行为仅能访问堆栈中的子节点。此节点的方法对那些没有被赋给这个节点的孩子节点是无效的。

    一个条件计算结果为False的条件表达式条件节点既不能被得到以添加到堆栈中,其孩子节点也不能被添加。条件节点作用域内的最终用户行为可以通过调用方法nodeCreated()来决定此节点是否被创建。如果节点条件被满足、节点被创建、并且被压入堆栈的话结果返回True,否则返回False。

 

异常处理

       一个由节点作用域内扩展抛出却没有在节点作用域内捕获的异常将由JJTree自行处理。当这种情况发生时,那些再节点作用域内被压入堆栈的节点会被弹出堆栈并被抛弃。之后一个异常会被重新抛出。

       这样做的意图是使分析其可以进行错误校验并且使节点堆栈继续处于一个可知的状态。

       注意:一般JJTree 不能识别出此异常是否由节点作用于内用户行为抛出的。这样的异常有可能被不正确的处理。

 

节点范围钩子

如果NODE_SCOPE_HOOK选项被置为True,JJTree会在每个节点作用域的入口和出口处调用用户自定义的2个解析器方法。这些方法必须有下面的形式:

        void jjtreeOpenNodeScope(Node n)
        void jjtreeCloseNodeScope(Node n)

如果解析器是静态的,那么这些方法也必须被声明成静态的。他们都以当前节点作为被调用的参数。

       一种用途是这些功能将用来存储节点的首尾标记符以便于输入可以很容易的被重现。例如:

               void jjtreeOpenNodeScope(Node n)
                {
                     ((MySimpleNode)n).first_token = getToken(1);
                }

  
    
  
               void jjtreeCloseNodeScope(Node n)
               {
                     ((MySimpleNode)n).last_token = getToken(0);
                }

       基于SimpleNode的MySimpleNode类中有下面2个额外的字段:

               Token first_token, last_token;

       另外一个用途是用于将解析器对象自身存储于节点以便于状态可以被解析器提供的所有节点所共享。

节点的生存周期

一个节点的建立经历了一个很好被确定的序列步骤。下面是从节点自身的透视图观察的序列。

1、创建节点需要一个独特的整形参量。这个参数确定了节点的种类,这在单一模式中有其有用。JJTree自动生成了一个声明了有效常量的类 parserTreeConstants.java 。 常量的名字取决于JJT前缀加文件中节点名字的大写字符串。用字符“.”代替字符“_” 。为方便起见,在同一个文件中维护了一个被称为jjtNodeName[]的字符串数组,它遍历了节点的未修改名字的常量。

2、节点的方法jjtOpen()被调用

3、如果NODE_SCOPE_HOOK选项被设置为True,那么用户自定义的方openNodeScope()将被调用并且以此节点为参数。这个方法可以初始化节点的字段或者调用节点的方法。例如,他可以存储节点的首标记符。

4、如果一个节点被解析时抛出了一个未捕获的异常,节点将被抛弃。JJTree将不再对其进行引用。虽然用户自定义的节点作用域钩子closeNodeHook()将不再调用此节点作为参数,但节点不会被关闭。

5、另外,如果一个条件节点的条件计算值为False,节点将被抛弃。虽然用户自定义节点作用域钩子closeNodeHook()可能调用此节点作为参数,但节点不会被关闭。

6、另外,一个明确节点通过整形表达式制定的所有孩子节点或者在一个条件节点作用域内被压入堆栈的所有节点被赋给此节点。他们的添加次序并没有被确定。

7、节点的方法jjtClose()被调用。

8、节点被压入堆栈。

9、如果NODE_SCOPE_HOOK选项被设置为True,则用户自定义方法closenNodeScope()将被调用并且以此节点作为参数。

10、如果节点不是根节点,他将作为其他节点的字节点被添加并且它的jjtSetParent()方法被调用。

 

访问者支持

       JJTree为访问者提供了一些基本的设计模式。如果VISITOR选项被设置为True,JJTree将会在生成所有节点类时插入jjtAccept()方法,并且生成一个以此节点为参数而被实现的访问者接口。[public Object visit(具体Node类,Object)]

       访问者接口的名字由解析器名字加Visitor来构造。每当JJTree运行时接口会被生成,以便于他可以准确地表现解析其所使用的那些节点。如果实现类不能被新节点更新将会产生编译时错误。只是一个特性。

 

选项

       JJTree在命令行或者JavaCC选项声明中提供了下面这些选项:

       BUILD_NODE_FILES (default: true)

       为SimpleNode以及语法中使用的其它节点创建样本实现。

       MULTI (default: false)

       创建多模式解析树。此选项默认为False,生成一个单一模式解析树。

       NODE_DEFAULT_VOID (default: false)

       此选项设置为True时,不在使每个非包装产生式定义一个节点,取而代之为空。

       NODE_FACTORY (default: false)

       用下面的方式使用一个工厂方法创建一个节点:

       public static Node jjtCreate(int id)

NODE_PACKAGE (default: "")

    被放进生成节点类里的包。默认为解析器的包。

    NODE_PREFIX (default: "AST")

    在多模式中,前缀用来从节点标志符构造节点类名字。默认前缀为” AST”

    NODE_SCOPE_HOOK (default: false)

       在节点作用域入口和出口处插入调用用户自定义的解析方法。参见:节点作用域钩子。

       NODE_USES_PARSER (default: false)

       JJTree会使用一个选择的形式将解析对象传给构造函数。例如:

  public static Node MyNode.jjtCreate(MyParser p, int id);

        MyNode(MyParser p, int id);

       STATIC (default: true)

       为静态解析器生成代码。选项默认为True。这必须一致的通过等效的JavaCC选项被使用。选项的值发布于JavaCC的源码中。

       VISITOR (default: false)

       在节点类中插入jjtAccept()方法,为语法中使用的每个节点类型产生一个访问者实现。

VISITOR_EXCEPTION (default: "")

       如果这个选项被设置,它将使用jjtAccept()和visit()方法的形式。注意:这个选项将会在以后的某个JJTree版本中删除。如果不影响你请不要使用它。

       JJTREE_OUTPUT_DIRECTORY (default: use value of OUTPUT_DIRECTORY)

       默认情况下,在全局OUTPUT_DIRECTORY设置中指定JJTree生成的输出目录。明确的设置这个选项允许用户从树文件中分离解析器。

 

JJTree 状态

       JJTree通过解析器对象中的jjtree字段保持它的状态。你可以使用它的一些方法来操作节点堆栈。

      final class JJTreeState {
      /* Call this to reinitialize the node stack.  */
      void reset();

  
    
  
      /* Return the root node of the AST. */
      Node rootNode();

  
    
  
      /* Determine whether the current node was actually closed and
         pushed */
      boolean nodeCreated();

  
    
  
      /* Return the number of nodes currently pushed on the node
         stack in the current node scope. */
      int arity();

  
    
  
      /* Push a node on to the stack. */
      void pushNode(Node n);

  
    
  
      /* Return the node on the top of the stack, and remove it from the
         stack.  */
      Node popNode();

  
    
  
      /* Return the node currently on the top of the stack. */
      Node peekNode();
     }

 

节点对象

       所有的AST节点必须实现这个接口。它为构建节点间父子关系提供了基本的操作。

        public interface Node {
      /** This method is called after the node has been made the current
        node.  It indicates that child nodes can now be added to it. */
      public void jjtOpen();

  
    
  
      /** This method is called after all the child nodes have been
        added. */
      public void jjtClose();

  
    
  
      /** This pair of methods are used to inform the node of its
        parent. */
      public void jjtSetParent(Node n);
      public Node jjtGetParent();

  
    
  
      /** This method tells the node to add its argument to the node's
        list of children.  */
      public void jjtAddChild(Node n, int i);

  
    
  
      /** This method returns a child node.  The children are numbered
         from zero, left to right. */
      public Node jjtGetChild(int i);

  
    
  
      /** Return the number of children the node has. */
      int jjtGetNumChildren();
    }

       SimpleNode类实现了Node接口,如果他不存在,那么JJTree会自动生成。你可以把这个类作为你自己节点实现的一个模板或者超类,或者你可以修改它以适应需求。SimpleNode为递归清理的节点和它的字节点额外提供了一个根本操作。你可以像这样的行为使用:

        {
            ((SimpleNode)jjtree.rootNode()).dump(">");
        }

方法dump()的字符串参数被添加以暗示树的层级。

    如果VISITOR选项被设置,那么另一个有效的方法被生成:

        {
           public void childrenAccept(MyParserVisitor visitor);
        }

       它轮流遍历节点的子节点,要求他们接受访问者。这对preorder and postorder[前序和后续]遍历可能是有用的。

 

例子

       JJTree发布了一些单一模式的例子其中包含解析数学表达式的文法。更多详细信息参见examples/JJTreeExamples/README

       还有一个利用JJTree构建的解释单一语言的程序解释器。更多详细信息参见examples/Interpreter/README

       一个关于HTML 3.2的文法也被发布了。更多信息参见examples/HTMLGrammars/RobsHTML/README

    一个使用访问者支持的例子的信息请参见examples/VTransformer/README

------------如下是英文原文---------------------------

JavaCC [tm]: JJTree Reference Documentation

Introduction

JJTree is a preprocessor for JavaCC [tm] that inserts parse tree building actions at various places in the JavaCC source. The output of JJTree is run through JavaCC to create the parser. This document describes how to use JJTree, and how you can interface your parser to it.

By default JJTree generates code to construct parse tree nodes for each nonterminal in the language. This behavior can be modified so that some nonterminals do not have nodes generated, or so that a node is generated for a part of a production's expansion.

JJTree defines a Java interface Node that all parse tree nodes must implement. The interface provides methods for operations such as setting the parent of the node, and for adding children and retrieving them.

JJTree operates in one of two modes, simple and multi (for want of better terms). In simple mode each parse tree node is of concrete type SimpleNode; in multi mode the type of the parse tree node is derived from the name of the node. If you don't provide implementations for the node classes JJTree will generate sample implementations based on SimpleNode for you. You can then modify the implementations to suit.

Although JavaCC is a top-down parser, JJTree constructs the parse tree from the bottom up. To do this it uses a stack where it pushes nodes after they have been created. When it finds a parent for them, it pops the children from the stack and adds them to the parent, and finally pushes the new parent node itself. The stack is open, which means that you have access to it from within grammar actions: you can push, pop and otherwise manipulate its contents however you feel appropriate. See Node Scopes and User Actions below for more important information.

JJTree provides decorations for two basic varieties of nodes, and some syntactic shorthand to make their use convenient.

  1. A definite node is constructed with a specific number of children. That many nodes are popped from the stack and made the children of the new node, which is then pushed on the stack itself. You notate a definite node like this:

    #ADefiniteNode(INTEGER EXPRESSION)

    A definite node descriptor expression can be any integer expression, although literal integer constants are by far the most common expressions.

  2. A conditional node is constructed with all of the children that were pushed on the stack within its node scope if and only if its condition evaluates to true. If it evaluates to false, the node is not constructed, and all of the children remain on the node stack. You notate a conditional node like this:

    #ConditionalNode(BOOLEAN EXPRESSION)

    A conditional node descriptor expression can be any boolean expression. There are two common shorthands for conditional nodes:

    1. Indefinite nodes

      #IndefiniteNode is short for #IndefiniteNode(true)

    2. Greater-than nodes

      #GTNode(>1) is short for #GTNode(jjtree.arity() > 1)

    The indefinite node shorthand (1) can lead to ambiguities in the JJTree source when it is followed by a parenthesized expansion. In those cases the shorthand must be replaced by the full expression. For example:

    	  ( ... ) #N ( a() ) 

    is ambiguous; you have to use the explicit condition:

     ( ... ) #N(true) ( a()
    	  ) 

WARNING: node descriptor expression should not have side-effects. JJTree doesn't specify how many times the expression will be evaluated.

By default JJTree treats each nonterminal as an indefinite node and derives the name of the node from the name of its production. You can give it a different name with the following syntax:

    void P1() #MyNode : { ... } { ... }
	  

When the parser recognizes a P1 nonterminal it begins an indefinite node. It marks the stack, so that any parse tree nodes created and pushed on the stack by nonterminals in the expansion for P1 will be popped off and made children of the node MyNode.

If you want to suppress the creation of a node for a production you can use the following syntax:

    void P2() #void : { ... } { ... }
	  

Now any parse tree nodes pushed by nonterminals in the expansion of P2 will remain on the stack, to be popped and made children of a production further up the tree. You can make this the default behavior for non-decorated nodes by using the NODE_DEFAULT_VOID option.

    
    void P3() : {}
    {
        P4() ( P5() )+ P6()
    }
    

In this example, an indefinite node P3 is begun, marking the stack, and then a P4 node, one or more P5 nodes and a P6 node are parsed. Any nodes that they push are popped and made the children of P3. You can further customize the generated tree:

    void P3() : {}
    {
        P4() ( P5() )+ #ListOfP5s P6()
    }
    

Now the P3 node will have a P4 node, a ListOfP5s node and a P6 node as children. The #Name construct acts as a postfix operator, and its scope is the immediately preceding expansion unit.

Node Scopes and User Actions

Each node is associated with a node scope. User actions within this scope can access the node under construction by using the special identifier jjtThis to refer to the node. This identifier is implicitly declared to be of the correct type for the node, so any fields and methods that the node has can be easily accessed.

A scope is the expansion unit immediately preceding the node decoration. This can be a parenthesized expression. When the production signature is decorated (perhaps implicitly with the default node), the scope is the entire right hand side of the production including its declaration block.

You can also use an expression involving jjtThis on the left hand side of an expansion reference. For example:

    ... ( jjtThis.my_foo = foo() ) #Baz ...
	  

Here jjtThis refers to a Baz node, which has a field called my_foo. The result of parsing the production foo() is assigned to that my_foo.

The final user action in a node scope is different from all the others. When the code within it executes, the node's children have already been popped from the stack and added to the node, which has itself been pushed onto the stack. The children can now be accessed via the node's methods such as jjtGetChild().

User actions other than the final one can only access the children on the stack. They have not yet been added to the node, so they aren't available via the node's methods.

A conditional node that has a node descriptor expression that evaluates to false will not get added to the stack, nor have children added to it. The final user action within a conditional node scope can determine whether the node was created or not by calling the nodeCreated() method. This returns true if the node's condition was satisfied and the node was created and pushed on the node stack, and false otherwise.

Exception handling

An exception thrown by an expansion within a node scope that is not caught within the node scope is caught by JJTree itself. When this occurs, any nodes that have been pushed on to the node stack within the node scope are popped and thrown away. Then the exception is rethrown.

The intention is to make it possible for parsers to implement error recovery and continue with the node stack in a known state.

WARNING: JJTree currently cannot detect whether exceptions are thrown from user actions within a node scope. Such an exception will probably be handled incorrectly.

Node Scope Hooks

If the NODE_SCOPE_HOOK option is set to true, JJTree generates calls to two user-defined parser methods on the entry and exit of every node scope. The methods must have the following signatures:

    void jjtreeOpenNodeScope(Node n)
    void jjtreeCloseNodeScope(Node n)
    

If the parser is STATIC then these methods will have to be declared as static as well. They are both called with the current node as a parameter.

One use for these functions is to store the node's first and last tokens so that the input can be easily reproduced again. For example:

    void jjtreeOpenNodeScope(Node n)
    {
      ((MySimpleNode)n).first_token = getToken(1);
    }

    void jjtreeCloseNodeScope(Node n)
    {
      ((MySimpleNode)n).last_token = getToken(0);
    }
    

where MySimpleNode is based on SimpleNode and has the following additional fields:

    Token first_token, last_token;
    

Another use might be to store the parser object itself in the node so that state that should be shared by all nodes produced by that parser can be provided. For example, the parser might maintain a symbol table.

The Life Cycle of a Node

A node goes through a well determined sequence of steps as it is built. This is that sequence viewed from the perspective of the node itself:

  1. the node's constructor is called with a unique integer parameter. This parameter identifies the kind of node and is especially useful in simple mode. JJTree automatically generates a file called parserTreeConstants.java that declares valid constants. The names of constants are derived by prepending JJT to the uppercase names of nodes, with dot symbols (".") replaced by underscore symbols ("_"). For convenience, an array of Strings called jjtNodeName[] that maps the constants to the unmodified names of nodes is maintained in the same file.
  2. the node's jjtOpen() method is called.
  3. if the option NODE_SCOPE_HOOK is set, the user-defined parser method openNodeScope() is called and passed the node as its parameter. This method can initialize fields in the node or call its methods. For example, it might store the node's first token in the node.
  4. if an unhandled exception is thrown while the node is being parsed then the node is abandoned. JJTree will never refer to it again. It will not be closed, and the user-defined node scope hook closeNodeHook() will not be called with it as a parameter.
  5. otherwise, if the node is conditional and its conditional expression evaluates to false then the node is abandoned. It will not be closed, although the user-defined node scope hook closeNodeHook() might be called with it as a parameter.
  6. otherwise, all of the children of the node as specified by the integer expression of a definite node, or all the nodes that were pushed on the stack within a conditional node scope are added to the node. The order they are added is not specified.
  7. the node's jjtClose() method is called.
  8. the node is pushed on the stack.
  9. if the option NODE_SCOPE_HOOK is set, the user-defined parser method closenNodeScope() is called and passed the node as its parameter.
  10. if the node is not the root node, it is added as a child of another node and its jjtSetParent() method is called.

Visitor Support

JJTree provides some basic support for the visitor design pattern. If the VISITOR option is set to true JJTree will insert an jjtAccept() method into all of the node classes it generates, and also generate a visitor interface that can be implemented and passed to the nodes to accept.

The name of the visitor interface is constructed by appending Visitor to the name of the parser. The interface is regenerated every time that JJTree is run, so that it accurately represents the set of nodes used by the parser. This will cause compile time errors if the implementation class has not been updated for the new nodes. This is a feature.

Options

JJTree supports the following options on the command line and in the JavaCC options statement:

BUILD_NODE_FILES (default: true)
Generate sample implementations for SimpleNode and any other nodes used in the grammar.
MULTI (default: false)
Generate a multi mode parse tree. The default for this is false, generating a simple mode parse tree.
NODE_DEFAULT_VOID (default: false)
Instead of making each non-decorated production an indefinite node, make it void instead.
NODE_FACTORY (default: false)
Use a factory method with following signature to construct nodes:
public static Node jjtCreate(int id)
NODE_PACKAGE (default: "")
The package to generate the node classes into. The default for this is the parser package.
NODE_PREFIX (default: "AST")
The prefix used to construct node class names from node identifiers in multi mode. The default for this is AST.
NODE_SCOPE_HOOK (default: false)
Insert calls to user-defined parser methods on entry and exit of every node scope. See Node Scope Hooks above.
NODE_USES_PARSER (default: false)
JJTree will use an alternate form of the node construction routines where it passes the parser object in. For example,
 public static Node MyNode.jjtCreate(MyParser p, int id);
	  MyNode(MyParser p, int id); 
STATIC (default: true)
Generate code for a static parser. The default for this is true. This must be used consistently with the equivalent JavaCC options. The value of this option is emitted in the JavaCC source.
VISITOR (default: false)
Insert a jjtAccept() method in the node classes, and generate a visitor implementation with an entry for every node type used in the grammar.
VISITOR_EXCEPTION (default: "")
If this option is set, it is used in the signature of the generated jjtAccept() methods and the visit() methods. Note: this option will be removed in a later version of JJTree. Don't use it if that bothers you.
JJTREE_OUTPUT_DIRECTORY (default: use value of OUTPUT_DIRECTORY)
By default, JJTree generates its output in the directory specified in the global OUTPUT_DIRECTORY setting. Explicitly setting this option allows the user to separate the parser from the tree files.

JJTree state

JJTree keeps its state in a parser class field called jjtree. You can use methods in this member to manipulate the node stack.

    final class JJTreeState {
      /* Call this to reinitialize the node stack.  */
      void reset();

      /* Return the root node of the AST. */
      Node rootNode();

      /* Determine whether the current node was actually closed and
	 pushed */
      boolean nodeCreated();

      /* Return the number of nodes currently pushed on the node
         stack in the current node scope. */
      int arity();

      /* Push a node on to the stack. */
      void pushNode(Node n);

      /* Return the node on the top of the stack, and remove it from the
	 stack.  */
      Node popNode();

      /* Return the node currently on the top of the stack. */
      Node peekNode();
    }
    

Node Objects

    /* All AST nodes must implement this interface.  It provides basic
       machinery for constructing the parent and child relationships
       between nodes. */

    public interface Node {
      /** This method is called after the node has been made the current
	node.  It indicates that child nodes can now be added to it. */
      public void jjtOpen();

      /** This method is called after all the child nodes have been
	added. */
      public void jjtClose();

      /** This pair of methods are used to inform the node of its
	parent. */
      public void jjtSetParent(Node n);
      public Node jjtGetParent();

      /** This method tells the node to add its argument to the node's
	list of children.  */
      public void jjtAddChild(Node n, int i);

      /** This method returns a child node.  The children are numbered
	 from zero, left to right. */
      public Node jjtGetChild(int i);

      /** Return the number of children the node has. */
      int jjtGetNumChildren();
    }
    

The class SimpleNode implements the Node interface, and is automatically generated by JJTree if it doesn't already exist. You can use this class as a template or superclass for your node implementations, or you can modify it to suit. SimpleNode additionally provides a rudimentary mechanism for recursively dumping the node and its children. You might use this is in action like this:

    
    {
        ((SimpleNode)jjtree.rootNode()).dump(">");
    }

    

The String parameter to dump() is used as padding to indicate the tree hierarchy.

Another utility method is generated if the VISITOR options is set:

    {
        public void childrenAccept(MyParserVisitor visitor);
    }
    

This walks over the node's children in turn, asking them to accept the visitor. This can be useful when implementing preorder and postorder traversals.

Examples

JJTree is distributed with some simple examples containing a grammar that parses arithmetic expressions. See the file examples/JJTreeExamples/README for further details.

There is also an interpreter for a simple language that uses JJTree to build the program representation. See the file examples/Interpreter/README for more information.

A grammar for HTML 3.2 is also included in the distribution. See examples/HTMLGrammars/RobsHTML/README to find out more.

Information about an example using the visitor support is in examples/VTransformer/README.

 

本人英文水平太差,就当学习了,希望大家支持。

 

       

javacc-docs:JavaCC文档工作库 严重缺少和急需的文档的工作资料库。 我原本打算使用Markdown,但后来经过一番调查,决定(至少暂时地,我们会看到...) 将是一个更好的选择。 目的是编写循序渐进的教程和更详细的参考文档。 这很可能最终将最终形成一本书。 立即下载

相关推荐

全网最全医学图像数据汇总

本仓库汇总了多个领域的医学图像开源数据集,涵盖CT、MRI、超声、内窥镜、病理、多模态、PET、OCT以及皮肤镜等多种成像技术。每个数据集均详细列出了任务类型、部位、格式、数量及下载链接,旨在为医学图像研究者提供便捷的资源导航。⚠️:这些数据集仅适用于学术研究用途。

2401_83173765的博客 3323

jjTree 简介

 和YACC等一样,JavaCC不直接支持分析树或抽象语法树(AST)的生成,如果要完成这些功能,用户需要自己编写相应的代码。幸运的是,JavaCC有一个扩充支持分析树或抽象语法树的生成,这就是jjTree。  实际上,jjTree可以看成是JavaCC的预处理程序。它的输入文件的后缀是jjt。经它处理之后的jj文件就包含了生成分析树的能力。jjTree采用压栈出栈的方法生成分析树。当它碰到一个非...

zhoujinhuang 1384

西门子与汇川伺服脉冲连接(速度模式)

如何使用西门子PLC与汇川伺服进行脉冲连接

2301_77347966的博客 1万+

javacc 教程6 jjtree

我们知道JavaCC是一种编译器开发工具,主要用于解析输入文本并生成与其语法结构相对应的语法树。JavaCC生成的语法树是一种较低级别的抽象,需要开发人员自行定义和实现对其的处理和操作。而JJTree是JavaCC的一个扩展,提供了一种更高级别的抽象,允许开发人员更方便地处理和操作语法树。JJTree生成的语法树节点包含属性和方法,可以更方便地遍历和操作语法树。因此,相对于JavaCC,JJTree可以更方便地构建和处理语法树,尤其是对于复杂的语法结构和语法树节点操作需求。

gambool的博客 1506

基于RYU+Mininet的SDN校园网络实战:从零搭建虚拟化实验环境

本文详细介绍了如何利用RYU控制器和Mininet搭建SDN校园网络实验环境,从Ubuntu虚拟机准备到网络拓扑设计,再到关键网络功能实现。通过实战案例展示SDN在校园网络中的应用优势,包括简化配置、提升效率等,为网络工程师和学生提供了一套完整的虚拟化实验方案。

weixin_30872499的博客 465

解决RK3566 USB OTG模式识别问题:从原理图分析到otg_mode文件操作全解析

本文深入解析RK3566 USB OTG模式识别问题,从硬件原理图设计到Linux内核的otg_mode文件操作,提供全面的调试方案。针对USB_OTG0_ID引脚电路设计、电源管理关联及内核驱动交互等关键点,给出实战调试技巧和常见问题解决方案,帮助开发者高效解决USB OTG模式切换难题。

eee77的博客 868

jjTree解析

这篇文章可以算是这篇小型桌面计算器的实现(javacc) 的续。   可以这么说,使用javacc作分析器生成器,如果没有用到jjTree,那么就是对语义分析 的过程理解不够深入。如果用到了jjTree而且用好了,那么对编译原理,BNF等的理解才算是比较到位了。 jjTree中最重要的概念是Node接口,所有的非终结符都可以规约 为一个节点。这个节点一般来讲是实现了Node接口的节点类 其中主要...

横行青海夜带刀 515

【ZZULIOJ】郑州轻工业大学oj题解整合

写在前面 关于ZZULIOJ题解 学习编程,想要巩固无非就是刷题或者做项目,然后因为我大一开始是刷郑轻的题目,就想着自己整合一下自己写的题目。既是复习也是笔记吧。 题解(内有题目描述) 1000 整数a+b 1001 植树问题 1002 简单多项式求值 1003 两个整数的四则运算 1004 三位数的数位分离 1005 整数幂 1006 求等差数列的和 1007 鸡兔同笼 1008 美元和人民币 1009 求平均分 1010 求圆的周长和面积 1011 圆柱体表面积 1012 求绝对值 1013 求两点间

蒟蒻乱写的博客 29万+

jjtree学习文档

闲来无聊,翻译jjtree帮助自己学习,也顺便方便大家原文档来源:https://javacc.org/jjtree;(如果觉得使用网页翻译可以看得懂的话可以自行跳过。翻译水平有限,不求雅,可以更快理解就行。)这里也附上一个链接,写得挺详细的:https://blog.csdn.net/charlesyy/article/details/1035413JavaCC™: JJTree Referen...

WhereIsMyChair的博客 1014

视觉提示学习(Prompt Learning):CV领域的GPT时刻?

视觉提示学习(Prompt Learning):CV领域的GPT时刻? ,人工智能,计算机视觉,大模型,AI,在人工智能技术不断演进的今天,计算机视觉(Computer Vision,CV)作为人工智能领域的重要分支,始终致力于让机器 “看懂” 世界。从早期基于传统特征工程的图像识别方法,到深度学习时代卷积神经网络(Convolutional Neural Networks,CNN)的崛起,CV 领域经历了多次重大变革。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 4万+

Multisim14.0仿真(五十九)基于74LS161的24进制计数器应用设计

它具有同步预置、异步清零功能,支持并行数据加载,并可在计数使能控制下进行二进制计数。:低电平时,在下一个时钟上升沿加载输入数据(A, B, C, D)。(多个 74LS161D 串联构成 8 位、12 位等更长计数器)。低电平时,下一个时钟上升沿加载输入数据(A, B, C, D)。:低电平有效,立即清零计数器(不受时钟控制)。均为高电平时,每个时钟上升沿计数。:两者均为高电平时,计数器才能递增。:立即清零计数器(不受时钟控制)。(通过预置初始值控制计数范围)。(计数范围:0 ~ 15,即。

corlin6688的博客 4772

jjTree和javaCC学习笔记

jjTree和javaCC学习笔记javacc是类似lex/yacc的parser生成器,可以把一段文本转换为抽象语法树(AST)。一般来说,用户首先要写一个jjtree文件(如eg2.jjt),然后执行jjtree eg2.jjt编译得到eg2.jj,再执行javacc eg2

月亮的技术博客 7952

ardupilot BMI088加速度陀螺仪学习

本节主要记录ardupilot 中BMI088传感器的学习过程,欢迎批评指正。BMI088是一款高性能6轴惯性传感器,由16位数字三轴±24g加速度计和16位数字三轴±2000°/ s陀螺仪组成。 BMI088允许高精度测量方向和沿三个正交轴的运动检测。BMI088具有高振动鲁棒性和3 x 4.5 x0.95mm³的小尺寸,是用于无人机和机器人应用等恶劣环境的高性能IMU中的独特产品。 BMI088专门设计用于有效抑制由于PCB上的共振或整个系统的结构而可能发生的振动。 除了高振动稳定性外,BMI088的出

陌城烟雨 2060

静态分析工具PMD使用说明

原博客地址:http://blog.csdn.net/ml5271169588/article/details/6975690                                    静态分析工具PMD使用说明 目录 静态分析工具PMD使用说明... 1 目录... 2 1.           编写目的...

javaee1234的博客 2571

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

javacc jjtree 写法 以及 jj写法 基本语法 以及应用

/***********************************************************/ >我使用的测试jjt,jj文件来自于javacc5.0版本 >dir_hier/javacc-5.0/javacc-5.0/examples/ JJTreeExamples

zyb243380456的专栏 1万+

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

JavaCC 实战和jjtree

最近项目 SQL 解析模块用到 Apache Calcite 了,由于需要自己定义一些语法,因此花了一段时间研究了其中的 JavaCC,本文全当是笔记免得以后忘记。 Sql 解析的第一步往往是将一串 SQL 字符串进行词法和语法解析。 所谓词法分析,就是将一行行的字符串按照实现定好的格式分割成一个个单词字符 Token, 比如sql SELECT 1+1 FROM tb WHERE;经过词法分析后就变成了单词SELECT, 1, +, 1, FROM, tb, WHERE。 而语法分析内,就是分析..

a1058926697的博客 1388

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

JavaCC/JJTree入门

JavaCC 介绍   JJTree JJTree为了让JJ文件具有生成分析树的能力。   实践 下载 Eclipse JavaCC plugin(http://www.easyeclipse.org/site/plugins/javacc.html ) 在公司win7下载的easy-javacc-1.5.7.exe         一个示例程序 options {...

保持学习、思考和实践 858

JavaCC、解析树和 XQuery 语法,第 2 部分

JavaCC、解析树和 XQuery 语法,第 2 部分 使用 JJTree 构建和遍历定制解析树

石头记 1434

java jj_javacc jjtree 写法 以及 jj写法 基本语法 以及应用

/***********************************************************/>我使用的测试jjt,jj文件来自于javacc5.0版本>dir_hier/javacc-5.0/javacc-5.0/examples/JJTreeExamplesSimpleExamples/**********************************...

weixin_33270404的博客 1429

代码质量工具总结

代码质量 代码质量检测工具 PMD FindBugs checkstyle SonarLint/SonarQube alibaba coding guiding 仅仅有质量检测工具还不够,还需要代码审查工具,加以人工审查辅助 代码审查工具简介 Review Board Gerrit GitLab Changes 强硬手段触发不规范代码拒绝提交或者构建发布失败 借助于 Git hook 的 VCS 服务端实现代码 merge

lonelymanontheway的博客 1万+

理解calcite 里的Java CC

JAVA CC 在calcite 的应用

csdn问鼎 586

JAVACC使用总结(一):基础入门

JAVACC使用总结(一):基础入门 安装及环境设置 JAVACC版的Hello word

qq_15089775的博客 1万+

JAVACC 入门(转载)

JAVACC 入门(转载) JavaFlashIDEA算法Unix    读了JavaCC自带文档中的SimpleExamples之后,有一点心得,于是总结一下,以备遗忘。   JavaCC的输入文档是一个词法和语法的规范文件,其中也包括一些动作的描述,它的后缀应该是jj。   简而言之,一个jj文档由下面几个部分构成: l         Options{}部分:这个部分对产

qq_20125305的专栏 1143

抽象语法树AST的全面解析(二)

JCTree类(com.sun.tools.javac.tree.JCTree)的简要分析 上一篇文章讲解了抽象语法树的来源和获取时机,接下来要分析一下抽象语法树的内部结构。 抽象语法树由JCTree的内部类(如JCCompilationUnit,JCClassDecl,JCMethodDecl等)作为语法节点构成。我们可以通过调用JCTree的accept()方法来访问抽象语法树的所有语法节点。 public abstract void accept(JCTree.Visitor var1);

qq_23107097的博客 1096

Java注解(五):JCTree相关知识学习

从图片讲起java编译过程中AST变化过程 安卓AOP之AST:抽象语法树 什么是JSR269 Javac黑客指南 JCTree的两个分类和重要子类的介绍 转载:抽象语法树AST的全面解析(二) 获取JCTree的工具类JavacTrees JCTree和TreeMaker的关系 ......

lucyLee的博客 3605
上一篇: 如何获得免费网络空间
下一篇: 病毒的苦恼与解决
CharlesYY
博客等级 码龄21年 7粉丝 54原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值