| 1 |
/* Copyright (c) 2006, Sun Microsystems, Inc. |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* * Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* * Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* * Neither the name of the Sun Microsystems, Inc. nor the names of its |
| 13 |
* contributors may be used to endorse or promote products derived from |
| 14 |
* this software without specific prior written permission. |
| 15 |
* |
| 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 |
|
| 29 |
JJTreeExamples |
| 30 |
|
| 31 |
This directory contains some simple JJTree input files intended to |
| 32 |
illustrate some of the basic ideas. All of them are based on a |
| 33 |
grammar to recognize arithmetic expressions built out of identifiers |
| 34 |
and constants. |
| 35 |
|
| 36 |
eg1.jjt |
| 37 |
|
| 38 |
This example is just the JavaCC grammar, with a little extra code in |
| 39 |
the parser's main method to call the dump method on the generated |
| 40 |
tree. It illustrates how the default behavior of JJTree will produce |
| 41 |
a tree of non-terminals. |
| 42 |
|
| 43 |
eg2.jjt |
| 44 |
|
| 45 |
This example is the same grammar as eg1.jjt with modifications to |
| 46 |
customize the generated tree. It illustrates how unnecessary |
| 47 |
intermediate nodes can be suppressed and how actions in the grammar |
| 48 |
can attach extra information to the nodes. |
| 49 |
|
| 50 |
eg3.jjt |
| 51 |
|
| 52 |
This example is a modification of eg2.jjt with the NODE_DEFAULT_VOID |
| 53 |
option set. This instructs JJTree to treat all undecorated |
| 54 |
non-terminals as if they were decorated as #void. The default JJTree |
| 55 |
behavior is to treat such non-terminals as if they were decorated |
| 56 |
with the name of the non-terminal. |
| 57 |
|
| 58 |
eg4.jjt |
| 59 |
|
| 60 |
This is a modification of eg3.jjt with the VISITOR option set. This |
| 61 |
instructs JJTree to insert a jjtAccept() method into all nodes it |
| 62 |
generates, and to produce a visitor class. The visitor is used to |
| 63 |
dump the tree. |
| 64 |
|
| 65 |
Here are some instructions on how to run the examples using the Ant build scripts, |
| 66 |
and the output you can expect to see. |
| 67 |
|
| 68 |
eg1.jjt |
| 69 |
------- |
| 70 |
|
| 71 |
The only bit of JJTree-specific code is an action in the start |
| 72 |
production that dumps the constructed parse tree when the parse is |
| 73 |
complete. It uses JJTree simple mode. |
| 74 |
|
| 75 |
The input file is eg1.jjt. |
| 76 |
|
| 77 |
$ ant eg1 |
| 78 |
Buildfile: build.xml |
| 79 |
|
| 80 |
eg1: |
| 81 |
[mkdir] Created dir: /home/tom/javacc/javacc/examples/JJTreeExamples/eg1 |
| 82 |
[copy] Copying 1 file to /home/tom/javacc/javacc/examples/JJTreeExamples/eg1 |
| 83 |
[jjtree] Java Compiler Compiler Version 3.2 (Tree Builder) |
| 84 |
[jjtree] (type "jjtree" with no arguments for help) |
| 85 |
[jjtree] Reading from file /home/tom/javacc/javacc/examples/JJTreeExamples/eg1.jjt . . . |
| 86 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg1/Node.java" does not exist. Will create one. |
| 87 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg1/SimpleNode.java" does not exist. Will create one. |
| 88 |
[jjtree] Annotated grammar generated successfully in /home/tom/javacc/javacc/examples/JJTreeExamples/eg1/eg1.jj |
| 89 |
[javacc] Java Compiler Compiler Version 3.2 (Parser Generator) |
| 90 |
[javacc] (type "javacc" with no arguments for help) |
| 91 |
[javacc] Reading from file /home/tom/javacc/javacc/examples/JJTreeExamples/eg1/eg1.jj . . . |
| 92 |
[javacc] File "TokenMgrError.java" does not exist. Will create one. |
| 93 |
[javacc] File "ParseException.java" does not exist. Will create one. |
| 94 |
[javacc] File "Token.java" does not exist. Will create one. |
| 95 |
[javacc] File "SimpleCharStream.java" does not exist. Will create one. |
| 96 |
[javacc] Parser generated successfully. |
| 97 |
[javac] Compiling 11 source files to /home/tom/javacc/javacc/examples/JJTreeExamples/eg1 |
| 98 |
[echo] ******* |
| 99 |
[echo] ******* Now cd into the eg1 directory and run 'java eg1' ****** |
| 100 |
[echo] ******* |
| 101 |
|
| 102 |
BUILD SUCCESSFUL |
| 103 |
Total time: 3 seconds |
| 104 |
[tom@hal JJTreeExamples]$ cd eg1 |
| 105 |
[tom@hal eg1]$ java eg1 |
| 106 |
Reading from standard input... |
| 107 |
(a + b) * (c + 1); |
| 108 |
Start |
| 109 |
Expression |
| 110 |
AdditiveExpression |
| 111 |
MultiplicativeExpression |
| 112 |
UnaryExpression |
| 113 |
Expression |
| 114 |
AdditiveExpression |
| 115 |
MultiplicativeExpression |
| 116 |
UnaryExpression |
| 117 |
Identifier |
| 118 |
MultiplicativeExpression |
| 119 |
UnaryExpression |
| 120 |
Identifier |
| 121 |
UnaryExpression |
| 122 |
Expression |
| 123 |
AdditiveExpression |
| 124 |
MultiplicativeExpression |
| 125 |
UnaryExpression |
| 126 |
Identifier |
| 127 |
MultiplicativeExpression |
| 128 |
UnaryExpression |
| 129 |
Integer |
| 130 |
Thank you. |
| 131 |
|
| 132 |
eg2.jjt |
| 133 |
------- |
| 134 |
|
| 135 |
This is a modification of the first example to illustrate how the |
| 136 |
parse tree can be customized: |
| 137 |
|
| 138 |
$ ant eg2 |
| 139 |
Buildfile: build.xml |
| 140 |
|
| 141 |
eg2: |
| 142 |
[mkdir] Created dir: /home/tom/javacc/javacc/examples/JJTreeExamples/eg2 |
| 143 |
[copy] Copying 1 file to /home/tom/javacc/javacc/examples/JJTreeExamples/eg2 |
| 144 |
[copy] Copying 1 file to /home/tom/javacc/javacc/examples/JJTreeExamples/eg2 |
| 145 |
[jjtree] Java Compiler Compiler Version 3.2 (Tree Builder) |
| 146 |
[jjtree] (type "jjtree" with no arguments for help) |
| 147 |
[jjtree] Reading from file /home/tom/javacc/javacc/examples/JJTreeExamples/eg2.jjt . . . |
| 148 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg2/Node.java" does not exist. Will create one. |
| 149 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg2/SimpleNode.java" does not exist. Will create one. |
| 150 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg2/ASTStart.java" does not exist. Will create one. |
| 151 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg2/ASTAdd.java" does not exist. Will create one. |
| 152 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg2/ASTMult.java" does not exist. Will create one. |
| 153 |
[jjtree] File "/home/tom/javacc/javacc/examples/JJTreeExamples/eg2/ASTInteger.java" does not exist. Will create one. |
| 154 |
[jjtree] Annotated grammar generated successfully in /home/tom/javacc/javacc/examples/JJTreeExamples/eg2/eg2.jj |
| 155 |
[javacc] Java Compiler Compiler Version 3.2 (Parser Generator) |
| 156 |
[javacc] (type "javacc" with no arguments for help) |
| 157 |
[javacc] Reading from file /home/tom/javacc/javacc/examples/JJTreeExamples/eg2/eg2.jj . . . |
| 158 |
[javacc] File "TokenMgrError.java" does not exist. Will create one. |
| 159 |
[javacc] File "ParseException.java" does not exist. Will create one. |
| 160 |
[javacc] File "Token.java" does not exist. Will create one. |
| 161 |
[javacc] File "SimpleCharStream.java" does not exist. Will create one. |
| 162 |
[javacc] Parser generated successfully. |
| 163 |
[javac] Compiling 16 source files to /home/tom/javacc/javacc/examples/JJTreeExamples/eg2 |
| 164 |
[echo] ******* |
| 165 |
[echo] ******* Now cd into the eg2 directory and run 'java eg2' ****** |
| 166 |
[echo] ******* |
| 167 |
|
| 168 |
BUILD SUCCESSFUL |
| 169 |
Total time: 3 seconds |
| 170 |
[tom@hal JJTreeExamples]$ cd eg2 |
| 171 |
[tom@hal eg2]$ java eg2 |
| 172 |
Reading from standard input... |
| 173 |
(a + b) * (c + 1); |
| 174 |
Start |
| 175 |
Mult |
| 176 |
Add |
| 177 |
Identifier: a |
| 178 |
Identifier: b |
| 179 |
Add |
| 180 |
Identifier: c |
| 181 |
Integer |
| 182 |
Thank you. |
| 183 |
|
| 184 |
Look at eg2.jjt to see how node annotations can be used to restructure |
| 185 |
the parse tree, and at ASTMyID.java to see how you can write your own |
| 186 |
node classes that maintain more information from the input stream. |
| 187 |
|
| 188 |
eg3.jjt |
| 189 |
------- |
| 190 |
|
| 191 |
This example can be run in the same manner as you ran eg2.jjt. |
| 192 |
|
| 193 |
eg4.jjt |
| 194 |
------- |
| 195 |
|
| 196 |
This example again can be run in the same manner as you ran eg2.jjt. |