📄 jjtree.html
字号:
<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 Reference</title><!-- Changed by: Michael Van De Vanter, 14-Jan-2003 --></HEAD><BODY bgcolor="#FFFFFF"><H1>JavaCC [tm]: JJTree Reference Documentation</H1> <h3>Introduction</h3> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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 <a href="#scopes">Node Scopes and User Actions</a> below for more important information.</p> <p>JJTree provides decorations for two basic varieties of nodes, and some syntactic shorthand to make their use convenient.</p> <ol> <li> <p>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:</p> <p><code>#ADefiniteNode(INTEGER EXPRESSION)</code></p> <p>A definite node descriptor expression can be any integer expression, although literal integer constants are by far the most common expressions.</p> </li> <li> <p>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:</p> <p><code>#ConditionalNode(BOOLEAN EXPRESSION)</code></p> <p>A conditional node descriptor expression can be any boolean expression. There are two common shorthands for conditional nodes:</p> <ol> <li> <p>Indefinite nodes</p> <p><code>#IndefiniteNode</code> is short for <code>#IndefiniteNode(true)</code></p> </li> <li> <p>Greater-than nodes</p> <p><code>#GTNode(>1)</code> is short for <code>#GTNode(jjtree.arity() > 1)</code></p> </li> </ol> <p>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:</p> <pre> ( ... ) #N ( a() ) </pre> <p>is ambiguous; you have to use the explicit condition:</p> <pre> ( ... ) #N(true) ( a() ) </pre> </li> </ol> <p>WARNING: node descriptor expression should not have side-effects. JJTree doesn't specify how many times the expression will be evaluated.</p> <p>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:</p> <pre> void P1() #MyNode : { ... } { ... } </pre> <p>When the parser recognizes a <code>P1</code> 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 <code>P1</code> will be popped off and made children of the node <code>MyNode</code>.</p> <p>If you want to suppress the creation of a node for a production you can use the following syntax:</p> <pre> void P2() #void : { ... } { ... } </pre> <p>Now any parse tree nodes pushed by nonterminals in the expansion of <code>P2</code> 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 <code>NODE_DEFAULT_VOID</code> option.</p> <pre> void P3() : {} { P4() ( P5() )+ P6() } </pre> <p>In this example, an indefinite node <code>P3</code> is begun, marking the stack, and then a <code>P4</code> node, one or more <code>P5</code> nodes and a <code>P6</code> node are parsed. Any nodes that they push are popped and made the children of <code>P3</code>. You can further customize the generated tree:</p> <pre> void P3() : {} { P4() ( P5() )+ #ListOfP5s P6() } </pre> <p>Now the <code>P3</code> node will have a <code>P4</code> node, a <code>ListOfP5s</code> node and a <code>P6</code> node as children. The <code>#Name</code> construct acts as a postfix operator, and its scope is the immediately preceding expansion unit.</p> <h3><a name="scopes">Node Scopes and User Actions</a></h3> <p>Each node is associated with a node scope. User actions within this scope can access the node under construction by using the special identifier <code>jjtThis</code> 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.</p> <p>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.</p> <p>You can also use an expression involving <code>jjtThis</code> on the left hand side of an expansion reference. For example:</p> <pre> ... ( jjtThis.my_foo = foo() ) #Baz ... </pre> <p>Here <code>jjtThis</code> refers to a <code>Baz</code> node, which has a field called <code>my_foo</code>. The result of parsing the production <code>foo()</code> is assigned to that <code>my_foo</code>.</p> <p>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 <code>jjtGetChild()</code>.</p> <p>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.</p> <p>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 <code>nodeCreated()</code> 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.</p> <h3>Exception handling</h3> <p>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.</p> <p>The intention is to make it possible for parsers to implement error recovery and continue with the node stack in a known state.</p> <p>WARNING: JJTree currently cannot detect whether exceptions are thrown from user actions within a node scope. Such an exception will probably be handled incorrectly.</p> <h3><a name="hooks">Node Scope Hooks</a></h3> <p>If the <code>NODE_SCOPE_HOOK</code> 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:</p> <pre> void jjtreeOpenNodeScope(Node n) void jjtreeCloseNodeScope(Node n) </pre> <p>If the parser is <code>STATIC</code> then these methods will have to be declared as static as well. They are both called with the current node as a parameter.</p> <p>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:</p> <pre> void jjtreeOpenNodeScope(Node n) { ((MySimpleNode)n).first_token = getToken(1); } void jjtreeCloseNodeScope(Node n) { ((MySimpleNode)n).last_token = getToken(0); } </pre> <p>where <code>MySimpleNode</code> is based on <code>SimpleNode</code> and has the following additional fields:</p> <pre> Token first_token, last_token; </pre> <p>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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -