| 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 |
|
| 5 |
Copyright (c) 2006, Sun Microsystems, Inc. |
| 6 |
All rights reserved. |
| 7 |
|
| 8 |
Redistribution and use in source and binary forms, with or without |
| 9 |
modification, are permitted provided that the following conditions are met: |
| 10 |
|
| 11 |
* Redistributions of source code must retain the above copyright notice, |
| 12 |
this list of conditions and the following disclaimer. |
| 13 |
* Redistributions in binary form must reproduce the above copyright |
| 14 |
notice, this list of conditions and the following disclaimer in the |
| 15 |
documentation and/or other materials provided with the distribution. |
| 16 |
* Neither the name of the Sun Microsystems, Inc. nor the names of its |
| 17 |
contributors may be used to endorse or promote products derived from |
| 18 |
this software without specific prior written permission. |
| 19 |
|
| 20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 30 |
THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
|
| 32 |
--> |
| 33 |
<head> |
| 34 |
<title>JavaCC: JJTree README</title> |
| 35 |
<!-- Changed by: Michael Van De Vanter, 14-Jan-2003 --> |
| 36 |
</head> |
| 37 |
<body bgcolor="#FFFFFF" > |
| 38 |
|
| 39 |
<h1>JavaCC [tm]: README for JJTreeExamples</h1> |
| 40 |
|
| 41 |
<pre> |
| 42 |
JJTreeExamples |
| 43 |
|
| 44 |
This directory contains some simple JJTree input files intended to |
| 45 |
illustrate some of the basic ideas. All of them are based on an |
| 46 |
grammar to recognize arithmetic expressions built out of identifiers |
| 47 |
and constants. |
| 48 |
|
| 49 |
eg1.jjt |
| 50 |
|
| 51 |
This example is just the JavaCC [tm] grammar, with a little extra code in |
| 52 |
the parser's main method to call the dump method on the generated |
| 53 |
tree. It illustrates how the default behavior of JJTree will produce |
| 54 |
a tree of non-terminals. |
| 55 |
|
| 56 |
eg2.jjt |
| 57 |
|
| 58 |
This example is the same grammar as eg1.jjt with modifications to |
| 59 |
customize the generated tree. It illustrates how unnecessary |
| 60 |
intermediate nodes can be suppressed, and how actions in the grammar |
| 61 |
can attach extra information to the nodes. |
| 62 |
|
| 63 |
eg3.jjt |
| 64 |
|
| 65 |
This example is a modification of eg2.jjt with the NODE_DEFAULT_VOID |
| 66 |
option set. This instructs JJTree to treat all undecorated |
| 67 |
non-terminals as if they were decorated as #void. The default JJTree |
| 68 |
behavior is to treat such non-terminals as if they were decorated |
| 69 |
with the name of the non-terminal. |
| 70 |
|
| 71 |
eg4.jjt |
| 72 |
|
| 73 |
This is a modification of eg3.jjt with the VISITOR option set. This |
| 74 |
instructs JJTree to insert a jjtAccept() method into all nodes it |
| 75 |
generates, and to produce a visitor class. The visitor is used to |
| 76 |
dump the tree. |
| 77 |
|
| 78 |
|
| 79 |
Here are some instructions on how to run the examples and the output |
| 80 |
you can expect to see. |
| 81 |
|
| 82 |
eg1.jjt |
| 83 |
------- |
| 84 |
|
| 85 |
The only bit of JJTree-specific code is an action in the start |
| 86 |
production that dumps the constructed parse tree when the parse is |
| 87 |
complete. It uses JJTree simple mode. |
| 88 |
|
| 89 |
The input file is eg1.jjt. |
| 90 |
|
| 91 |
trane% jjtree eg1.jjt |
| 92 |
<<< Version and copyright info>>> |
| 93 |
(type "jjtree" with no arguments for help) |
| 94 |
Reading from file eg1.jjt . . . |
| 95 |
Annotated grammar generated successfully in eg1.jj |
| 96 |
trane% |
| 97 |
|
| 98 |
JJTree has now generated the JavaCC parser source, as well as Java |
| 99 |
source for the parse tree node building classes. Running JavaCC in |
| 100 |
the normal way generates the remaining Java code. |
| 101 |
|
| 102 |
trane% javacc eg1.jj |
| 103 |
<<< Version and copyright info>>> |
| 104 |
(type "javacc" with no arguments for help) |
| 105 |
Reading from file eg1.jj . . . |
| 106 |
File "TokenMgrError.java" does not exist. Will create one. |
| 107 |
File "ParseException.java" does not exist. Will create one. |
| 108 |
File "Token.java" does not exist. Will create one. |
| 109 |
File "ASCII_CharStream.java" does not exist. Will create one. |
| 110 |
Parser generated successfully. |
| 111 |
trane% |
| 112 |
|
| 113 |
Compile and run the Java program as usual. The expression is read from the |
| 114 |
standard input (you type in "(a + b) * (c + 1);"): |
| 115 |
|
| 116 |
trane% javac eg1.java |
| 117 |
trane% java eg1 |
| 118 |
Reading from standard input... |
| 119 |
(a + b) * (c + 1); |
| 120 |
Start |
| 121 |
Expression |
| 122 |
AdditiveExpression |
| 123 |
MultiplicativeExpression |
| 124 |
UnaryExpression |
| 125 |
Expression |
| 126 |
AdditiveExpression |
| 127 |
MultiplicativeExpression |
| 128 |
UnaryExpression |
| 129 |
Identifier |
| 130 |
MultiplicativeExpression |
| 131 |
UnaryExpression |
| 132 |
Identifier |
| 133 |
UnaryExpression |
| 134 |
Expression |
| 135 |
AdditiveExpression |
| 136 |
MultiplicativeExpression |
| 137 |
UnaryExpression |
| 138 |
Identifier |
| 139 |
MultiplicativeExpression |
| 140 |
UnaryExpression |
| 141 |
Integer |
| 142 |
Thank you. |
| 143 |
trane% |
| 144 |
|
| 145 |
eg2.jjt |
| 146 |
------- |
| 147 |
|
| 148 |
This is a modification of the first example to illustrate how the |
| 149 |
parse tree can be customized: |
| 150 |
|
| 151 |
trane% jjtree eg2.jjt |
| 152 |
<<< Version and copyright info>>> |
| 153 |
(type "jjtree" with no arguments for help) |
| 154 |
Reading from file eg2.jjt . . . |
| 155 |
File "Node.java" does not exist. Will create one. |
| 156 |
File "SimpleNode.java" does not exist. Will create one. |
| 157 |
File "ASTStart.java" does not exist. Will create one. |
| 158 |
File "ASTAdd.java" does not exist. Will create one. |
| 159 |
File "ASTMult.java" does not exist. Will create one. |
| 160 |
File "ASTInteger.java" does not exist. Will create one. |
| 161 |
Annotated grammar generated successfully in eg2.jj |
| 162 |
trane% |
| 163 |
trane% javacc eg2.jj |
| 164 |
<<< Version and copyright info>>> |
| 165 |
(type "javacc" with no arguments for help) |
| 166 |
Reading from file eg2.jj . . . |
| 167 |
File "TokenMgrError.java" does not exist. Will create one. |
| 168 |
File "ParseException.java" does not exist. Will create one. |
| 169 |
File "Token.java" does not exist. Will create one. |
| 170 |
File "ASCII_CharStream.java" does not exist. Will create one. |
| 171 |
Parser generated successfully. |
| 172 |
trane% |
| 173 |
trane% javac eg2.java |
| 174 |
trane% java eg2 |
| 175 |
Reading from standard input... |
| 176 |
(a + b) * (c + 1); |
| 177 |
Start |
| 178 |
Mult |
| 179 |
Add |
| 180 |
Identifier: a |
| 181 |
Identifier: b |
| 182 |
Add |
| 183 |
Identifier: c |
| 184 |
Integer |
| 185 |
Thank you. |
| 186 |
trane% |
| 187 |
|
| 188 |
Look at eg2.jjt to see how node annotations can be used to restructure |
| 189 |
the parse tree, and at ASTMyID.java to see how you can write your own |
| 190 |
node classes that maintain more information from the input stream. |
| 191 |
|
| 192 |
eg3.jjt |
| 193 |
------- |
| 194 |
|
| 195 |
This example can be run in the same manner as you ran eg2.jjt. |
| 196 |
|
| 197 |
eg4.jjt |
| 198 |
------- |
| 199 |
|
| 200 |
This example again can be run in the same manner as you ran eg2.jjt. |
| 201 |
One thing to take care in this case is that you must run jjtree on |
| 202 |
a clean directory (that does not contain previously generated files). |
| 203 |
For example, the file SimpleNode.java is different when the option |
| 204 |
VISITOR is set to true. |
| 205 |
</pre> |
| 206 |
</body> |
| 207 |
</html> |