2018/2/2 19:07:34当前位置推荐好文程序员浏览文章




JJTree is a preprocessor for JavaCC™ 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, how you can interface your parser to it.

JJtree 是JavaCC的一个预处理器,它能在JavaCC源文件的许多位置插入解析树构建动作。JJTree的输出是通过运行JavaCC来创建解析器。本文描述如何使用JJTree以及如何将你的解析操作连接到它。

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, for adding children retrieving them.

JJTree定义了一个java Interface Node,所有的解析树节点都实现该接口。该接口提供某些方法供操作,例如:设定节点parent或添加孩子节点,或检索节点。

JJTree operates in one of two modes, simple 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.

JJTree有两种操作模式,simple 和 multi(想要获得更好的操作)。在simple模式下每个解析树节点是一个具体的类型SimpleNode;在multi模式下解析树节点的类型来自于相应节点的名字。如果你不提供node类的实现,JJTree会为你生成简单的实现,该实现基于SimpleNode。然后你可以通过修改实现来适应你得需求。

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 adds them to the parent, 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 otherwise manipulate its contents however you feel appropriate. SeeNode Scopes User Actionsbelow for more important information.

虽然JavaCC是一个自顶向下的解析器,但JJTree是自底向上来构造解析树的。它使用stack数据结构来存储已经创建的节点。当发现节点的父节点时,它从stack中pop出孩子节点并添加到parent下,并最终push新的parent节点到stack中。stack是开放的,你可以push,pop或其他你认为合适的操作stack的内容。查看Node Scopes User Actions章节获悉更多实现信息。

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


A definite node is constructed with a specific number of children. That many nodes are popped from the stack 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.

1、 一个definite node是由特定数量的孩子构成的。有许多节点从stack pop出来组成新节点的孩子节点,然后将自己push到stack中,你可以像这样指定一个definite node:


一个definite node描述表达式可以使多个integer 表达式,尽管文字整型常量是迄今为止最常见的表达式。

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


2、一个conditional node由所有在其作用域下的已经push到stack的孩子节点组成,当且仅当条件判定为true时。如果条件判定为false,则node不会被构造,并且所有孩子节点保留在stack中。你可以像下面这样指定一个conditional node:


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

Indefinite nodes#IndefiniteNodeis short for#IndefiniteNode(true)

Greater-than nodes#GTNode(>1)is short for#GTNode(jjtree.arity() > 1)

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

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

is ambiguous; you have to use the explicit condition:

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


一个conditional 节点描述表达式可以是很多个boolean 表达式。有两种一般的简写:

1、Indefinite nodes

#IndefiniteNode是#IndefiniteNode(true) 的简写

2、Greater-than nodes

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

indefinite node的简写表达式可能导致歧义,当它后边跟随跟随的是一个括号表达式。在这种情形下必须替换为完整表达式,例如:

( … ) #N ( a() ) 是有歧义的,你可以使用清晰的条件:

( … ) #N(true) ( a() )

WARNING: node descriptor expressions 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 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 : { ... } { ... }

默认情况下,JJTree把每个非终结符当成indefinite node,并且节点名字来源于名字对应的产生式。你可以使用如下语法指定一个不同的名字:

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

When the parser recognizes aP1nonterminal it begins an indefinite node. It marks the stack, so that any parse tree nodes created pushed on the stack by nonterminals in the expansion forP1will be popped off made children of the nodeMyNode.

当解析器识别P1这个非终结符时,它将初始一个indefinite node。并为它构建stack,因此在P1非终结符下创建的任何解析树节点会push到stack中,并且pop出来构建MyNode 节点的孩子节点。

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

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


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

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


可以将这个设定为默认行为,当节点未被装饰时,通过设定 NODE_DEFAULT_VOID选项。

    void P3() : {}


        P4() ( P5() )+ P6()


In this example, an indefinite nodeP3is begun, marking the stack, then aP4node, one or moreP5nodes aP6node are parsed. Any nodes that they push are popped made the children ofP3. You can further customize the generated tree:

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

在这个例子中,indefinite node P3 是开始,构建stack,然后是P4节点,一个或多个P5节点和一个P6节点被解析。


    void P3() : {}


        P4() ( P5() )+ #ListOfP5s P6()


Now theP3node will have aP4node, aListOfP5snode aP6node as children. The#Nameconstruct acts as a postfix operator, its scope is the immediately preceding expansion unit.

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


Node Scopes 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 identifierjjtThisto refer to the node. This identifier is implicitly d to be of the correct type for the node, so any fields 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 hside of the production including its declaration block.


You can also use an expression involvingjjtThison the left hside of an expansion reference. For example:

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


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

HerejjtThisrefers to aBaznode, which has a field calledmy_foo. The result of parsing the productionfoo()is assigned to thatmy_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 added to the node, which has itself been pushed onto the stack. The children can now be accessed via the node’s methods such asjjtGetChild().


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 thenodeCreated()method. This returns true if the node’s condition was satisfied the node was created pushed on the node stack, false otherwise.

一个conditional node的描述表达式被计算为false时,不会被push到stack,并且没有孩子节点加入到它。最后的用户动作在conditional node作用域里可以通过nodeCreated()方法判断节点是否被创建。返回true时表示节点创建并push到stack,false则没有。

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 thrown away. Then the exception is rethrown.

The intention is to make it possible for parsers to implement error recovery 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 theNODE_SCOPE_HOOKoption is set to true, JJTree generates calls to two user-defined parser methods on the entry exit of every node scope. The methods must have the following signatures:

    void jjtreeOpenNodeScope(Node n)

    void jjtreeCloseNodeScope(Node n)


void jjtreeOpenNodeScope(Node n)

void jjtreeCloseNodeScope(Node n)

If the parser isSTATICthen these methods will have to be d as static as well. They are both called with the current node as a parameter.

One 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.

    void jjtreeOpenNodeScope(Node n)




    void jjtreeCloseNodeScope(Node n)



WheregetSymbolTable()is a user-defined method to return a symbol table structure for the node.



void jjtreeOpenNodeScope(Node n) { ((SimpleNode)n).jjtSetValue(getSymbolTable()); }

void jjtreeCloseNodeScope(Node n) { }

Tracking Tokens 跟踪token

It is often useful to keep track of each node’s first last token so that input can be easily reproduced again. By setting theTRACK_TOKENSoption the generatedSimpleNodeclass will contain 4 extra methods:

      public Token jjtGetFirstToken()

      public void jjtSetFirstToken(Token token)

      public Token jjtGetLastToken()

      public void jjtSetLastToken(Token token)

The first last token for each node will be set up automatically when the parser is run.


public Token jjtGetFirstToken()

public void jjtSetFirstToken(Token token)

public Token jjtGetLastToken()

public void jjtSetLastToken(Token token)

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:


the node’s constructor is called with a unique integer parameter. This parameter identifies the kind of node is especially useful in simple mode. JJTree automatically generates a file calledparserTreeConstants.java that s 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 ofStrings calledjjtNodeName[]that maps the constants to the unmodified names of nodes is maintained in the same file.

1、 节点的构造函数调用会以一个唯一的整数为参数。这个参数标识节点类型,这在simple模式下尤其有用。JJTree自动生成一个i称作 parserTreeConstants.java的文件定义有效的常量。常量名是JJT+节点名大写的方式生成,使用.替换所有_。为简便,在同一个文件中,一个String类型的jjtNodeName[]数组映射所有不可修改的节点名常量。

2.the node’sjjtOpen()method is called.


3.if the optionNODE_SCOPE_HOOKis set, the user-defined parser methodopenNodeScope()is called 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, the user-defined node scope hookcloseNodeHook()will not be called with it as a parameter.


5.otherwise, if the node is conditional its conditional expression evaluates to false then the node is abandoned. It will not be closed, although the user-defined node scope hookcloseNodeHook()might be called with it as a parameter.

5、另外,如果是conditional 节点,并且条件表达式计算得false,则该节点被遗弃。它不回被关闭,虽然用户定义的节点钩子函数closeNodeHook()方法会被调用。

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’sjjtClose()method is called.

8.the node is pushed on the stack.

9.if the optionNODE_SCOPE_HOOKis set, the user-defined parser methodcloseNodeScope()is called passed the node as its parameter.

10.if the node is not the root node, it is added as a child of another node itsjjtSetParent()method is called.





Visitor Support 访问支持

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


The name of the visitor interface is constructed by appendingVisitorto 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.



JJTree supports the following options on the commline in the JavaCC options statement:


Generate sample implementations for SimpleNode any other nodes used in the grammar.


Generate a multi mode parse tree. The default for this is false, generating a simple mode parse tree.


Instead of making each non-decorated production an indefinite node, make it void instead.


If set defines the name of a user-supplied class that will extendSimpleNode. Any tree nodes created will then be subclasses of NODE_CLASS.


Specify a class containing a factory method with following signature to construct nodes:

public static Node jjtCreate(int id)

For backwards compatibility, the valuefalsemay also be specified, meaning thatSimpleNodewill be used as the factory class.


The package to generate the node classes into. The default for this is the parser package.


The superclass for the SimpleNode class. By providing a custom superclass you may be able to avoid the need to edit the generated SimpleNode.java. See the examples/Interpreter for an example usage.


The prefix used to construct node class names from node identifiers in multi mode. The default for this is AST.


calls to user-defined parser methods on entry exit of every node scope. SeeNode Scope Hooks above.


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


Insert jjtGetFirstToken(), jjtSetFirstToken(), getLastToken(), and jjtSetLastToken() methods in SimpleNode. The FirstToken is automatically set up on entry to a node scope; the LastToken is automatically set up on exit from a node scope.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)a jjtAccept() method in the node classes, generate a visitor implementation with an entry for every node type used in the grammar.VISITOR_DATA_TYPE (default: "Object")If this option is set, it is used in the signature of the generated jjtAccept() methods the visit() methods as the type of the data argument.VISITOR_RETURN_TYPE (default: "Object")If this option is set, it is used in the signature of the generated jjtAccept() methods the visit() methods as the return type of the method.VISITOR_EXCEPTION (default: "")If this option is set, it is used in the signature of the generated jjtAccept() methods the visit() methods.JJTREE_OUTPUT_DIRECTORY (default: use value of OUTPUT_DIRECTORY)By default, JJTree generates its output in the directory specified in the global OUTPUT_DIRECTORYsetting. 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 calledjjtree. 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, 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 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 nodes

  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 classSimpleNodeimplements theNodeinterface, 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.SimpleNodeadditionally provides a rudimentary mechanism for recursively dumping the node its children. You might use this is in action like this:




TheStringparameter todump()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 postorder traversals.


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

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

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