⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jjtreeintro.html

📁 This a JavaCC documentation.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<HTML><!--Copyright 漏 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,California 95054, U.S.A. All rights reserved.  Sun Microsystems, Inc. hasintellectual property rights relating to technology embodied in the productthat is described in this document. In particular, and without limitation,these intellectual property rights may include one or more of the U.S.patents listed at http://www.sun.com/patents and one or more additionalpatents or pending patent applications in the U.S. and in other countries.U.S. Government Rights - Commercial software. Government users are subjectto the Sun Microsystems, Inc. standard license agreement and applicableprovisions of the FAR and its supplements.  Use is subject to license terms.Sun,  Sun Microsystems,  the Sun logo and  Java are trademarks or registeredtrademarks of Sun Microsystems, Inc. in the U.S. and other countries.  Thisproduct is covered and controlled by U.S. Export Control laws and may besubject to the export or import laws in other countries.  Nuclear, missile,chemical biological weapons or nuclear maritime end uses or end users, whetherdirect or indirect, are strictly prohibited.  Export or reexport to countriessubject to U.S. embargo or to entities identified on U.S. export exclusionlists, including, but not limited to, the denied persons and speciallydesignated nationals lists is strictly prohibited.--><HEAD> <title>JavaCC: JJTree Introduction</title><!-- Changed by: Michael Van De Vanter, 14-Jan-2003 --></HEAD><BODY bgcolor="#FFFFFF" ><H1>JavaCC [tm]: JJTree Introduction</H1><pre>JJTree is a preprocessor for JavaCC [tm] that inserts parse tree building actionsat various places in the JavaCC source. The output of JJTree is run throughJavaCC to create the parser. This document describes how to use JJTree, andhow you can interface your parser to it.By default, JJTree generates code to construct parse tree nodes for eachnonterminal in the language. This behavior can be modified so that somenonterminals do not have nodes generated, or so that a node is generated for apart of a production's expansion.JJTree defines a Java interface Node that all parse tree nodes mustimplement. The interface provides methods for operations such as setting theparent of the node, and for adding children and retrieving them.JJTree operates in one of two modes, simple and multi (for want of betterterms). In simple mode, each parse tree node is of concrete type SimpleNode; inmulti mode, the type of the parse tree node is derived from the name of thenode. If you don't provide implementations for the node classes JJTree willgenerate sample implementations based on SimpleNode for you. You can thenmodify the implementations to suit.Although JavaCC is a top-down parser, JJTree constructs the parse tree fromthe bottom up. To do this it uses a stack where it pushes nodes after theyhave been created. When it finds a parent for them, it pops the children fromthe stack and adds them to the parent, and finally pushes the new parent nodeitself. The stack is open, which means that you have access to it from withingrammar actions: you can push, pop and otherwise manipulate its contentshowever you feel appropriate. See Node Scopes and User Actions below for moreimportant information.JJTree provides decorations for two basic varieties of nodes, and somesyntactic 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. JJTreedoesn't specify how many times the expression will be evaluated.By default JJTree treats each nonterminal as an indefinite node and derivesthe name of the node from the name of its production. You can give it adifferent name with the following syntax:    void P1() #MyNode : { ... } { ... }When the parser recognizes a P1 nonterminal it begins an indefinite node. Itmarks the stack, so that any parse tree nodes created and pushed on the stackby nonterminals in the expansion for P1 will be popped off and made childrenof the node MyNode.If you want to suppress the creation of a node for a production, you can usethe following syntax:    void P2() #void : { ... } { ... }Now any parse tree nodes pushed by nonterminals in the expansion of P2 willremain on the stack, to be popped and made children of a production further upthe tree. You can make this the default behavior for non-decorated nodes byusing the NODE_DEFAULT_VOID option.    void P3() : {}    {        P4() ( P5() )+ P6()    }In this example, an indefinite node P3 is begun, marking the stack, and then aP4 node, one or more P5 nodes and a P6 node are parsed. Any nodes that theypush are popped and made the children of P3. You can further customize thegenerated tree:    void P3() : {}    {        P4() ( P5() )+ #ListOfP5s P6()    }Now the P3 node will have a P4 node, a ListOfP5s node and a P6 node aschildren. The #Name construct acts as a postfix operator, and its scope is theimmediately preceding expansion unit.Node Scopes and User ActionsEach node is associated with a node scope. User actions within this scope canaccess the node under construction by using the special identifier jjtThis torefer to the node. This identifier is implicitly declared to be of the correcttype for the node, so any fields and methods that the node has can be easilyaccessed.A scope is the expansion unit immediately preceding the node decoration. Thiscan be a parenthesized expression. When the production signature is decorated(perhaps implicitly with the default node), the scope is the entire right handside of the production including its declaration block.You can also use an expression involving jjtThis on the left hand side of anexpansion reference. For example:    ... ( jjtThis.my_foo = foo() ) #Baz ...Here jjtThis refers to a Baz node, which has a field called my_foo. The resultof parsing the production foo() is assigned to that my_foo.The final user action in a node scope is different from all the others. Whenthe code within it executes, the node's children have already been popped fromthe stack and added to the node, which has itself been pushed onto thestack. 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 thestack. They have not yet been added to the node, so they aren't available viathe node's methods.A conditional node that has a node descriptor expression that evaluates tofalse will not get added to the stack, nor have children added to it. Thefinal user action within a conditional node scope can determine whether thenode was created or not by calling the nodeCreated() method. This returns trueif the node's condition was satisfied and the node was created and pushed onthe node stack, and false otherwise.Exception handlingAn exception thrown by an expansion within a node scope that is not caughtwithin the node scope is caught by JJTree itself. When this occurs, any nodesthat have been pushed on to the node stack within the node scope are poppedand thrown away. Then the exception is rethrown.The intention is to make it possible for parsers to implement error recoveryand continue with the node stack in a known state.WARNING: JJTree currently cannot detect whether exceptions are thrown fromuser actions within a node scope. Such an exception will probably be handledincorrectly.Node Scope HooksIf the NODE_SCOPE_HOOK option is set to true, JJTree generates calls to twouser-defined parser methods on the entry and exit of every node scope. Themethods 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 staticas 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 sothat the input can be easily reproduced again. For example:    void jjtreeOpenNodeScope(Node n)    {      ((MySimpleNode)n).first_token = getToken(1);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -