/usr/share/doc/javacc/doc/JJTree.html is in javacc-doc 5.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Copyright (c) 2006, Sun Microsystems, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Sun Microsystems, Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
-->
<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 expressions 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 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.</p>
<pre>
void jjtreeOpenNodeScope(Node n)
{
((SimpleNode)n).jjtSetValue(getSymbolTable());
}
void jjtreeCloseNodeScope(Node n)
{
}
</pre>
<p>Where <code>getSymbolTable()</code> is a user-defined method to
return a symbol table structure for the node.</p>
<h3><a name="hooks">Tracking Tokens</a></h3>
<p>It is often useful to keep track of each node's first and last
token so that input can be easily reproduced again. By
setting the <code>TRACK_TOKENS</code> option the generated
<code>SimpleNode</code> class will contain 4 extra methods:</p>
<pre>
public Token jjtGetFirstToken()
public void jjtSetFirstToken(Token token)
public Token jjtGetLastToken()
public void jjtSetLastToken(Token token)
</pre>
<p>The first and last token for each node will be set
up automatically when the parser is run. </p>
<h3>The Life Cycle of a Node</h3>
<p>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:</p>
<ol>
<li>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 <i>parser</i>TreeConstants.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 <code>String</code>s
called <code>jjtNodeName[]</code> that maps the constants to the
unmodified names of nodes is maintained in the same file.</li>
<li>the node's <code>jjtOpen()</code> method is called.</li>
<li>if the option <code>NODE_SCOPE_HOOK</code> is set, the
user-defined parser method <code>openNodeScope()</code> 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.</li>
<li>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 <code>closeNodeHook()</code> will not be called
with it as a parameter.</li>
<li>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
<code>closeNodeHook()</code> might be called with it as a
parameter.</li>
<li>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.</li>
<li>the node's <code>jjtClose()</code> method is called.</li>
<li>the node is pushed on the stack.</li>
<li>if the option <code>NODE_SCOPE_HOOK</code> is set, the
user-defined parser method <code>closeNodeScope()</code> is
called and passed the node as its parameter.</li>
<li>if the node is not the root node, it is added as a child of
another node and its <code>jjtSetParent()</code> method is
called.</li>
</ol>
<h3>Visitor Support</h3>
<p>JJTree provides some basic support for the visitor design
pattern. If the <code>VISITOR</code> option is set to true
JJTree will insert an <code>jjtAccept()</code> 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.</p>
<p>The name of the visitor interface is constructed by appending
<code>Visitor</code> 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.</p>
<h3>Options</h3>
<p>JJTree supports the following options on the command
line and in the JavaCC options statement:</p>
<dl>
<dt><code>BUILD_NODE_FILES</code> (default:
<code>true</code>)</dt>
<dd>Generate sample implementations for SimpleNode and any other
nodes used in the grammar.</dd>
<dt><code>MULTI</code> (default: <code>false</code>)</dt>
<dd> Generate a multi mode parse tree. The default for this is
false, generating a simple mode parse tree.</dd>
<dt><code>NODE_DEFAULT_VOID</code> (default:
<code>false</code>)</dt>
<dd>Instead of making each non-decorated production an
indefinite node, make it void instead.</dd>
<dt><code>NODE_CLASS</code> (default: <code>""</code>)</dt>
<dd>If set defines the name of a user-supplied class that will
extend <code>SimpleNode</code>. Any tree nodes created will
then be subclasses of NODE_CLASS.</dd>
<dt><code>NODE_FACTORY</code> (default: <code>""</code>)</dt>
<dd>Specify a class containing a factory method with following
signature to construct nodes:
<br /><code>public static Node jjtCreate(int id)</code>
<br />For backwards compatibility, the value <code>false</code>
may also be specified, meaning that <code>SimpleNode</code>
will be used as the factory class.
</dd>
<dt><code>NODE_PACKAGE</code> (default: <code>""</code>)</dt>
<dd> The package to generate the node classes into. The default
for this is the parser package.</dd>
<dt><code>NODE_EXTENDS</code> (default: <code>""</code>) <em>Deprecated</em></dt>
<dd>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.</dd>
<dt><code>NODE_PREFIX</code> (default: <code>"AST"</code>)</dt>
<dd>The prefix used to construct node class names from node
identifiers in multi mode. The default for this is AST.</dd>
<dt><code>NODE_SCOPE_HOOK</code> (default:
<code>false</code>)</dt>
<dd>Insert calls to user-defined parser methods on entry and
exit of every node scope. See <a href="#hooks">Node Scope
Hooks above</a>.</dd>
<dt><code>NODE_USES_PARSER</code> (default:
<code>false</code>)</dt>
<dd>JJTree will use an alternate form of the node construction
routines where it passes the parser object in. For example,
<pre> public static Node MyNode.jjtCreate(MyParser p, int id);
MyNode(MyParser p, int id); </pre>
</dd>
<dt><code>TRACK_TOKENS</code> (default:
<code>false</code)</dt>
<dd>Insert <code>jjtGetFirstToken(), jjtSetFirstToken(), getLastToken(),
</code> and <code>jjtSetLastToken()</code> methods in SimpleNode. The
<code>FirstToken</code> is automatically set up on entry to a node
scope; the <code>LastToken</code> is automatically set up on exit
from a node scope.
</dd>
<dt><code>STATIC</code> (default: <code>true</code>)</dt>
<dd>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.</dd>
<dt><code>VISITOR</code> (default: <code>false</code>)</dt>
<dd>Insert a <code>jjtAccept()</code> method in the node
classes, and generate a visitor implementation with an entry
for every node type used in the grammar.</dd>
<dt><code>VISITOR_DATA_TYPE</code> (default:
<code>"Object"</code>)</dt>
<dd>If this option is set, it is used in the signature of the
generated <code>jjtAccept()</code> methods and the visit()
methods as the type of the <code>data</code>
argument.
</dd>
<dt><code>VISITOR_RETURN_TYPE</code> (default:
<code>"Object"</code>)</dt>
<dd>If this option is set, it is used in the signature of the
generated <code>jjtAccept()</code> methods and the visit()
methods as the return type of the method.
</dd>
<dt><code>VISITOR_EXCEPTION</code> (default:
<code>""</code>)</dt>
<dd>If this option is set, it is used in the signature of the
generated <code>jjtAccept()</code> methods and the visit()
methods.</dd>
<dt><code>JJTREE_OUTPUT_DIRECTORY</code> (default:
use value of <code>OUTPUT_DIRECTORY</code>)</dt>
<dd>By default, JJTree generates its output in the directory
specified in the global <code>OUTPUT_DIRECTORY</code> setting.
Explicitly setting this option allows the user to separate the
parser from the tree files.</dd>
</dl>
<h3>JJTree state</h3>
<p>JJTree keeps its state in a parser class field called
<code>jjtree</code>. You can use methods in this member to
manipulate the node stack.</p>
<pre>
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();
}
</pre>
<h3>Node Objects</h3>
<pre>
/* 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();
}
</pre>
<p>The class <code>SimpleNode</code> implements the
<code>Node</code> 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. <code>SimpleNode</code> additionally
provides a rudimentary mechanism for recursively dumping the
node and its children. You might use this is in action like
this:</p>
<pre>
{
((SimpleNode)jjtree.rootNode()).dump(">");
}
</pre>
<p>The <code>String</code> parameter to <code>dump()</code> is
used as padding to indicate the tree hierarchy.</p>
<p>Another utility method is generated if the VISITOR options is set:</p>
<pre>
{
public void childrenAccept(MyParserVisitor visitor);
}
</pre>
<p>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.</p>
<h3>Examples</h3>
<p>JJTree is distributed with some simple examples
containing a grammar that parses arithmetic expressions. See
the file <code>examples/JJTreeExamples/README</code> for further
details.</p>
<p>There is also an interpreter for a simple language that uses
JJTree to build the program representation. See the file
<code>examples/Interpreter/README</code> for more
information.</p>
<p>Information about an example using the visitor support is in
<code>examples/VTransformer/README</code>.</p>
</body>
</html>
|