PageRenderTime 43ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/javacc-5.0/www/doc/jjtreeintro.html

https://gitlab.com/essere.lab.public/qualitas.class-corpus
HTML | 501 lines | 347 code | 123 blank | 31 comment | 0 complexity | bd112ea6d24ceb2a3bcb530b6ca2fffa MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <!--
  4. Copyright (c) 2006, Sun Microsystems, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the Sun Microsystems, Inc. nor the names of its
  14. contributors may be used to endorse or promote products derived from
  15. this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  26. THE POSSIBILITY OF SUCH DAMAGE.
  27. -->
  28. <head>
  29. <title>JavaCC: JJTree Introduction</title>
  30. <!-- Changed by: Michael Van De Vanter, 14-Jan-2003 -->
  31. </head>
  32. <body bgcolor="#FFFFFF" >
  33. <h1>JavaCC [tm]: JJTree Introduction</h1>
  34. <pre>
  35. JJTree is a preprocessor for JavaCC [tm] that inserts parse tree building actions
  36. at various places in the JavaCC source. The output of JJTree is run through
  37. JavaCC to create the parser. This document describes how to use JJTree, and
  38. how you can interface your parser to it.
  39. By default, JJTree generates code to construct parse tree nodes for each
  40. nonterminal in the language. This behavior can be modified so that some
  41. nonterminals do not have nodes generated, or so that a node is generated for a
  42. part of a production's expansion.
  43. JJTree defines a Java interface Node that all parse tree nodes must
  44. implement. The interface provides methods for operations such as setting the
  45. parent of the node, and for adding children and retrieving them.
  46. JJTree operates in one of two modes, simple and multi (for want of better
  47. terms). In simple mode, each parse tree node is of concrete type SimpleNode; in
  48. multi mode, the type of the parse tree node is derived from the name of the
  49. node. If you don't provide implementations for the node classes JJTree will
  50. generate sample implementations based on SimpleNode for you. You can then
  51. modify the implementations to suit.
  52. Although JavaCC is a top-down parser, JJTree constructs the parse tree from
  53. the bottom up. To do this it uses a stack where it pushes nodes after they
  54. have been created. When it finds a parent for them, it pops the children from
  55. the stack and adds them to the parent, and finally pushes the new parent node
  56. itself. The stack is open, which means that you have access to it from within
  57. grammar actions: you can push, pop and otherwise manipulate its contents
  58. however you feel appropriate. See Node Scopes and User Actions below for more
  59. important information.
  60. JJTree provides decorations for two basic varieties of nodes, and some
  61. syntactic shorthand to make their use convenient.
  62. 1.
  63. A definite node is constructed with a specific number of
  64. children. That many nodes are popped from the stack and made the
  65. children of the new node, which is then pushed on the stack
  66. itself. You notate a definite node like this:
  67. #ADefiniteNode(INTEGER EXPRESSION)
  68. A definite node descriptor expression can be any integer expression,
  69. although literal integer constants are by far the most common
  70. expressions.
  71. 2.
  72. A conditional node is constructed with all of the children that were
  73. pushed on the stack within its node scope if and only if its condition
  74. evaluates to true. If it evaluates to false, the node is not
  75. constructed, and all of the children remain on the node stack. You
  76. notate a conditional node like this:
  77. #ConditionalNode(BOOLEAN EXPRESSION)
  78. A conditional node descriptor expression can be any boolean
  79. expression. There are two common shorthands for conditional nodes:
  80. 1)
  81. Indefinite nodes
  82. #IndefiniteNode is short for #IndefiniteNode(true)
  83. 2)
  84. Greater-than nodes
  85. #GTNode(>1) is short for #GTNode(jjtree.arity() > 1)
  86. The indefinite node shorthand (1) can lead to ambiguities in the
  87. JJTree source when it is followed by a parenthesized expansion. In
  88. those cases the shorthand must be replaced by the full expression. For
  89. example:
  90. ( ... ) #N ( a() )
  91. is ambiguous; you have to use the explicit condition:
  92. ( ... ) #N(true) ( a() )
  93. WARNING: node descriptor expression should not have side-effects. JJTree
  94. doesn't specify how many times the expression will be evaluated.
  95. By default JJTree treats each nonterminal as an indefinite node and derives
  96. the name of the node from the name of its production. You can give it a
  97. different name with the following syntax:
  98. void P1() #MyNode : { ... } { ... }
  99. When the parser recognizes a P1 nonterminal it begins an indefinite node. It
  100. marks the stack, so that any parse tree nodes created and pushed on the stack
  101. by nonterminals in the expansion for P1 will be popped off and made children
  102. of the node MyNode.
  103. If you want to suppress the creation of a node for a production, you can use
  104. the following syntax:
  105. void P2() #void : { ... } { ... }
  106. Now any parse tree nodes pushed by nonterminals in the expansion of P2 will
  107. remain on the stack, to be popped and made children of a production further up
  108. the tree. You can make this the default behavior for non-decorated nodes by
  109. using the NODE_DEFAULT_VOID option.
  110. void P3() : {}
  111. {
  112. P4() ( P5() )+ P6()
  113. }
  114. In this example, an indefinite node P3 is begun, marking the stack, and then a
  115. P4 node, one or more P5 nodes and a P6 node are parsed. Any nodes that they
  116. push are popped and made the children of P3. You can further customize the
  117. generated tree:
  118. void P3() : {}
  119. {
  120. P4() ( P5() )+ #ListOfP5s P6()
  121. }
  122. Now the P3 node will have a P4 node, a ListOfP5s node and a P6 node as
  123. children. The #Name construct acts as a postfix operator, and its scope is the
  124. immediately preceding expansion unit.
  125. Node Scopes and User Actions
  126. Each node is associated with a node scope. User actions within this scope can
  127. access the node under construction by using the special identifier jjtThis to
  128. refer to the node. This identifier is implicitly declared to be of the correct
  129. type for the node, so any fields and methods that the node has can be easily
  130. accessed.
  131. A scope is the expansion unit immediately preceding the node decoration. This
  132. can be a parenthesized expression. When the production signature is decorated
  133. (perhaps implicitly with the default node), the scope is the entire right hand
  134. side of the production including its declaration block.
  135. You can also use an expression involving jjtThis on the left hand side of an
  136. expansion reference. For example:
  137. ... ( jjtThis.my_foo = foo() ) #Baz ...
  138. Here jjtThis refers to a Baz node, which has a field called my_foo. The result
  139. of parsing the production foo() is assigned to that my_foo.
  140. The final user action in a node scope is different from all the others. When
  141. the code within it executes, the node's children have already been popped from
  142. the stack and added to the node, which has itself been pushed onto the
  143. stack. The children can now be accessed via the node's methods such as
  144. jjtGetChild().
  145. User actions other than the final one can only access the children on the
  146. stack. They have not yet been added to the node, so they aren't available via
  147. the node's methods.
  148. A conditional node that has a node descriptor expression that evaluates to
  149. false will not get added to the stack, nor have children added to it. The
  150. final user action within a conditional node scope can determine whether the
  151. node was created or not by calling the nodeCreated() method. This returns true
  152. if the node's condition was satisfied and the node was created and pushed on
  153. the node stack, and false otherwise.
  154. Exception handling
  155. An exception thrown by an expansion within a node scope that is not caught
  156. within the node scope is caught by JJTree itself. When this occurs, any nodes
  157. that have been pushed on to the node stack within the node scope are popped
  158. and thrown away. Then the exception is rethrown.
  159. The intention is to make it possible for parsers to implement error recovery
  160. and continue with the node stack in a known state.
  161. WARNING: JJTree currently cannot detect whether exceptions are thrown from
  162. user actions within a node scope. Such an exception will probably be handled
  163. incorrectly.
  164. Node Scope Hooks
  165. If the NODE_SCOPE_HOOK option is set to true, JJTree generates calls to two
  166. user-defined parser methods on the entry and exit of every node scope. The
  167. methods must have the following signatures:
  168. void jjtreeOpenNodeScope(Node n)
  169. void jjtreeCloseNodeScope(Node n)
  170. If the parser is STATIC then these methods will have to be declared as static
  171. as well. They are both called with the current node as a parameter.
  172. One use for these functions is to store the node's first and last tokens so
  173. that the input can be easily reproduced again. For example:
  174. void jjtreeOpenNodeScope(Node n)
  175. {
  176. ((MySimpleNode)n).first_token = getToken(1);
  177. }
  178. void jjtreeCloseNodeScope(Node n)
  179. {
  180. ((MySimpleNode)n).last_token = getToken(0);
  181. }
  182. where MySimpleNode is based on SimpleNode and has the following additional
  183. fields:
  184. Token first_token, last_token;
  185. Another use might be to store the parser object itself in the node so that
  186. state that should be shared by all nodes produced by that parser can be
  187. provided. For example, the parser might maintain a symbol table.
  188. The Life Cycle of a Node
  189. A node goes through a well determined sequence of steps as it is built. The
  190. following
  191. is that sequence viewed from the perspective of the node itself:
  192. 1. the node's constructor is called with a unique integer parameter. This
  193. parameter identifies the kind of node and is especially useful in
  194. simple mode. JJTree automatically generates a file called
  195. parserTreeConstants.java and declares Java constants for the node
  196. identifiers. It also declares an array of Strings called jjtNodeName[]
  197. which maps the identifier integers to the names of the nodes.
  198. 2. the node's jjtOpen() method is called.
  199. 3. if the option NODE_SCOPE_HOOK is set, the user-defined parser method
  200. openNodeScope() is called and passed the node as its parameter. This
  201. method can initialize fields in the node or call its methods. For
  202. example, it might store the node's first token in the node.
  203. 4. if an unhandled exception is thrown while the node is being parsed
  204. then the node is abandoned. JJTree will never refer to it again. It
  205. will not be closed, and the user-defined node scope hook
  206. closeNodeHook() will not be called with it as a parameter.
  207. 5. otherwise, if the node is conditional and its conditional expression
  208. evaluates to false then the node is abandoned. It will not be closed,
  209. although the user-defined node scope hook closeNodeHook() might be
  210. called with it as a parameter.
  211. 6. otherwise, all of the children of the node as specified by the integer
  212. expression of a definite node, or all the nodes that were pushed on
  213. the stack within a conditional node scope are added to the node. The
  214. order they are added is not specified.
  215. 7. the node's jjtClose() method is called.
  216. 8. the node is pushed on the stack.
  217. 9. if the option NODE_SCOPE_HOOK is set, the user-defined parser method
  218. closenNodeScope() is called and passed the node as its parameter.
  219. 10. if the node is not the root node, it is added as a child of another
  220. node and its jjtSetParent() method is called.
  221. Visitor Support
  222. JJTree provides some basic support for the visitor design pattern. If the
  223. VISITOR option is set to true, JJTree will insert an jjtAccept() method into
  224. all of the node classes it generates, and also generate a visitor interface
  225. that can be implemented and passed to the nodes to accept.
  226. The name of the visitor interface is constructed by appending Visitor to the
  227. name of the parser. The interface is regenerated every time that JJTree is
  228. run, so that it accurately represents the set of nodes used by the
  229. parser. This will cause compile time errors if the implementation class has
  230. not been updated for the new nodes. This is a feature.
  231. Options
  232. JJTree 0.3pre4 supports the following options on the command line and in the
  233. JavaCC options statement:
  234. BUILD_NODE_FILES (default: true)
  235. Generate sample implementations for SimpleNode and any other nodes used
  236. in the grammar.
  237. MULTI (default: false)
  238. Generate a multi mode parse tree. The default for this is false,
  239. generating a simple mode parse tree.
  240. NODE_DEFAULT_VOID (default: false)
  241. Instead of making each non-decorated production an indefinite node, make
  242. it void instead.
  243. NODE_CLASS (default: "")
  244. If set defines the name of a user-supplied class that will extend
  245. SimpleNode. Any tree nodes created will then be subclasses of NODE_CLASS.
  246. NODE_FACTORY (default: "")
  247. Specify a class containing a factory method with following signature
  248. to construct nodes:
  249. public static Node jjtCreate(int id)
  250. For backwards compatibility, the value "false" may also be specified,
  251. meaning that SimpleNode will be used as the factory class.
  252. NODE_PACKAGE (default: "")
  253. The package to generate the node classes into. The default for this is
  254. the parser package.
  255. NODE_EXTENDS (default: "") (DEPRECATED)
  256. The superclass for the SimpleNode class. By providing a custom
  257. superclass you may be able to avoid the need to edit the generated
  258. SimpleNode.java. See the examples/Interpreter for an example usage.
  259. NODE_PREFIX (default: "AST")
  260. The prefix used to construct node class names from node identifiers in
  261. multi mode. The default for this is AST.
  262. NODE_SCOPE_HOOK (default: false)
  263. Insert calls to user-defined parser methods on entry and exit of every
  264. node scope. See Node Scope Hooks above.
  265. NODE_USES_PARSER (default: false)
  266. JJTree will use an alternate form of the node construction routines
  267. where it passes the parser object in. For example,
  268. public static Node MyNode.jjtCreate(MyParser p, int id);
  269. MyNode(MyParser p, int id);
  270. TRACK_TOKENS (default: false)
  271. Insert jjtGetFirstToken(), jjtSetFirstToken(), jjtGetLastToken() and
  272. jjtSetLastToken() methods in SimpleNode. The firstToken is automatically
  273. set up on entry to a node scope; the endToken is automatically set
  274. up on exit from a node scope.
  275. STATIC (default: true)
  276. Generate code for a static parser. The default for this is true. This
  277. must be used consistently with the equivalent JavaCC options. The value
  278. of this option is emitted in the JavaCC source.
  279. VISITOR (default: false)
  280. Insert a jjtAccept() method in the node classes, and generate a visitor
  281. implementation with an entry for every node type used in the grammar.
  282. VISITOR_DATA_TYPE (default: "Object")
  283. If this option is set, it is used in the signature of the
  284. generated jjtAccept() methods and the visit() methods as the type of the
  285. "data" argument.
  286. VISITOR_RETURN_TYPE (default: "Object")
  287. If this option is set, it is used in the signature of the generated
  288. jjtAccept() methods and the visit() methods as the return type of the
  289. method.
  290. VISITOR_EXCEPTION (default: "")
  291. If this option is set, it is used in the signature of the generated
  292. jjtAccept() methods and the visit() methods.
  293. JJTree state
  294. JJTree keeps its state in a parser class field called jjtree. You can use
  295. methods in this member to manipulate the node stack.
  296. final class JJTreeState {
  297. /* Call this to reinitialize the node stack. */
  298. void reset();
  299. /* Return the root node of the AST. */
  300. Node rootNode();
  301. /* Determine whether the current node was actually closed and
  302. pushed */
  303. boolean nodeCreated();
  304. /* Return the number of nodes currently pushed on the node
  305. stack in the current node scope. */
  306. int arity();
  307. /* Push a node on to the stack. */
  308. void pushNode(Node n);
  309. /* Return the node on the top of the stack, and remove it from the
  310. stack. */
  311. Node popNode();
  312. /* Return the node currently on the top of the stack. */
  313. Node peekNode();
  314. }
  315. Node Objects
  316. /* All AST nodes must implement this interface. It provides basic
  317. machinery for constructing the parent and child relationships
  318. between nodes. */
  319. public interface Node {
  320. /** This method is called after the node has been made the current
  321. node. It indicates that child nodes can now be added to it. */
  322. public void jjtOpen();
  323. /** This method is called after all the child nodes have been
  324. added. */
  325. public void jjtClose();
  326. /** This pair of methods are used to inform the node of its
  327. parent. */
  328. public void jjtSetParent(Node n);
  329. public Node jjtGetParent();
  330. /** This method tells the node to add its argument to the node's
  331. list of children. */
  332. public void jjtAddChild(Node n, int i);
  333. /** This method returns a child node. The children are numbered
  334. from zero, left to right. */
  335. public Node jjtGetChild(int i);
  336. /** Return the number of children the node has. */
  337. int jjtGetNumChildren();
  338. }
  339. The class SimpleNode implements the Node interface, and is automatically
  340. generated by JJTree if it doesn't already exist. You can use this class as a
  341. template or superclass for your node implementations, or you can modify it to
  342. suit. SimpleNode additionally provides a rudimentary mechanism for recursively
  343. dumping the node and its children. You might use this is in action like this:
  344. {
  345. ((SimpleNode)jjtree.rootNode()).dump(">");
  346. }
  347. The String parameter to dump() is used as padding to indicate the tree
  348. hierarchy.
  349. Another utility method is generated if the VISITOR options is set:
  350. {
  351. public void childrenAccept(MyParserVisitor visitor);
  352. }
  353. This walks over the node's children in turn, asking them to accept the
  354. visitor. This can be useful when implementing preorder and postorder
  355. traversals.
  356. More Documentation
  357. Jocelyn Paine has contributed a very nice introduction to JJTree where he
  358. describes how he has used it to develop an extension to HTML for interactive
  359. web pages: http://users.ox.ac.uk/~popx/jjtree.html
  360. Examples
  361. JJTree 0.3pre3 is distributed with some simple examples containing a grammar
  362. that parses arithmetic expressions. See the file
  363. examples/JJTreeExamples/README for further details.
  364. There is also an interpreter for a simple language that uses JJTree to build
  365. the program representation. See the file examples/Interpreter/README for more
  366. information.
  367. A grammar for HTML 3.2 is also included in the distribution. See
  368. examples/HTMLGrammars/RobsHTML/README to find out more.
  369. Information about an example using the visitor support is in
  370. examples/VTransformer/README.
  371. </pre>
  372. </body>
  373. </html>