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
|
/* 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.
*/
JJTreeExamples
This directory contains some simple JJTree input files intended to
illustrate some of the basic ideas. All of them are based on a
grammar to recognize arithmetic expressions built out of identifiers
and constants.
eg1.jjt
This example is just the JavaCC grammar, with a little extra code in
the parser's main method to call the dump method on the generated
tree. It illustrates how the default behavior of JJTree will produce
a tree of non-terminals.
eg2.jjt
This example is the same grammar as eg1.jjt with modifications to
customize the generated tree. It illustrates how unnecessary
intermediate nodes can be suppressed and how actions in the grammar
can attach extra information to the nodes.
eg3.jjt
This example is a modification of eg2.jjt with the NODE_DEFAULT_VOID
option set. This instructs JJTree to treat all undecorated
non-terminals as if they were decorated as #void. The default JJTree
behavior is to treat such non-terminals as if they were decorated
with the name of the non-terminal.
eg4.jjt
This is a modification of eg3.jjt with the VISITOR option set. This
instructs JJTree to insert a jjtAccept() method into all nodes it
generates, and to produce a visitor class. The visitor is used to
dump the tree.
Here are some instructions on how to run the examples using the Ant build scripts,
and the output you can expect to see.
eg1.jjt
-------
The only bit of JJTree-specific code is an action in the start
production that dumps the constructed parse tree when the parse is
complete. It uses JJTree simple mode.
The input file is eg1.jjt.
$ ant -lib ../../../lib eg1
Buildfile: Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\build.xml
eg1:
[delete] Deleting directory Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1
[mkdir] Created dir: Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1
[copy] Copying 1 file to Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1
[copy] Copying 1 file to Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1
[jjtree] Java Compiler Compiler Version 6.2_1 (Tree Builder)
[jjtree] (type "jjtree" with no arguments for help)
[jjtree] Reading from file Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1.jjt . . .
[jjtree] opt:c++
[jjtree] File "Node.h" does not exist. Will create one.
[jjtree] File "SimpleNode.h" does not exist. Will create one.
[jjtree] File "SimpleNode.cc" does not exist. Will create one.
[jjtree] File "ParserTree.h" does not exist. Will create one.
[jjtree] File "ParserTreeConstants.h" does not exist. Will create one.
[jjtree] File "JJTParserState.h" does not exist. Will create one.
[jjtree] File "JJTParserState.cc" does not exist. Will create one.
[jjtree] Annotated grammar generated successfully in Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\eg1.jj
[javacc] Java Compiler Compiler Version 6.2_1 (Parser Generator)
[javacc] (type "javacc" with no arguments for help)
[javacc] Reading from file Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\eg1.jj . . .
[javacc] File "JavaCC.h" does not exist. Will create one.
[javacc] File "CharStream.h" does not exist. Will create one.
[javacc] File "CharStream.cc" does not exist. Will create one.
[javacc] File "Token.h" does not exist. Will create one.
[javacc] File "Token.cc" does not exist. Will create one.
[javacc] File "TokenManager.h" does not exist. Will create one.
[javacc] File "TokenMgrError.h" does not exist. Will create one.
[javacc] File "TokenMgrError.cc" does not exist. Will create one.
[javacc] File "ParseException.h" does not exist. Will create one.
[javacc] File "ParseException.cc" does not exist. Will create one.
[javacc] File "Token.h" is being rebuilt.
[javacc] File "Token.cc" is being rebuilt.
[javacc] File "ErrorHandler.h" does not exist. Will create one.
[javacc] Parser generated successfully.
[cpptasks:cc] 9 total files to be compiled.
[cpptasks:cc] In file included from Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\SimpleNode.cc:4:0:
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\SimpleNode.h: In constructor EG1::SimpleNode::SimpleNode(EG1::Parser*, int):
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\SimpleNode.h:20:14: attention : EG1::SimpleNode::parser will be initialized after [-Wreorder]
[cpptasks:cc] Parser * parser;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\SimpleNode.h:19:22: attention : int EG1::SimpleNode::id [-Wreorder]
[cpptasks:cc] int id;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\SimpleNode.cc:14:1: attention : lorsqu'initialis ici [-Wreorder]
[cpptasks:cc] SimpleNode::SimpleNode(Parser *parser, int id) :
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\ParserTokenManager.cc: In member function EG1::Token* EG1::ParserTokenManager::jjFillToken():
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\ParserTokenManager.cc:361:28: attention : endColumn may be used uninitialized in this function [-Wmaybe-uninitialized]
[cpptasks:cc] t->endColumn = endColumn;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\ParserTokenManager.cc:360:32: attention : beginColumn may be used uninitialized in this function [-Wmaybe-uninitialized]
[cpptasks:cc] t->beginColumn = beginColumn;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\ParserTokenManager.cc:359:24: attention : endLine may be used uninitialized in this function [-Wmaybe-uninitialized]
[cpptasks:cc] t->endLine = endLine;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg1\ParserTokenManager.cc:358:28: attention : beginLine may be used uninitialized in this function [-Wmaybe-uninitialized]
[cpptasks:cc] t->beginLine = beginLine;
[cpptasks:cc] ^
[cpptasks:cc] Starting link
[exec] Reading from standard input...
[exec] Start
[exec] Expression
[exec] AdditiveExpression
[exec] MultiplicativeExpression
[exec] UnaryExpression
[exec] Expression
[exec] AdditiveExpression
[exec] MultiplicativeExpression
[exec] UnaryExpression
[exec] Integer
[exec] MultiplicativeExpression
[exec] UnaryExpression
[exec] Integer
[exec] UnaryExpression
[exec] Expression
[exec] AdditiveExpression
[exec] MultiplicativeExpression
[exec] UnaryExpression
[exec] Identifier
[exec] MultiplicativeExpression
[exec] UnaryExpression
[exec] Identifier
[exec] thank you.
BUILD SUCCESSFUL
Total time: 4 seconds
eg2.jjt
-------
This is a modification of the first example to illustrate how the
parse tree can be customized:
$ ant -lib ../../../lib eg2
Buildfile: Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\build.xml
eg2:
[delete] Deleting directory Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2
[mkdir] Created dir: Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2
[copy] Copying 1 file to Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2
[copy] Copying 1 file to Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2
[jjtree] Java Compiler Compiler Version 6.2_1 (Tree Builder)
[jjtree] (type "jjtree" with no arguments for help)
[jjtree] Reading from file Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2.jjt . . .
[jjtree] opt:c++
[jjtree] File "Node.h" does not exist. Will create one.
[jjtree] File "SimpleNode.h" does not exist. Will create one.
[jjtree] File "SimpleNode.cc" does not exist. Will create one.
[jjtree] File "ASTUnaryExpression.h" does not exist. Will create one.
[jjtree] File "ASTAdd.h" does not exist. Will create one.
[jjtree] File "ASTStart.h" does not exist. Will create one.
[jjtree] File "ASTMyID.h" does not exist. Will create one.
[jjtree] File "ASTInteger.h" does not exist. Will create one.
[jjtree] File "ASTMult.h" does not exist. Will create one.
[jjtree] File "ASTUnaryExpression.cc" does not exist. Will create one.
[jjtree] File "ASTAdd.cc" does not exist. Will create one.
[jjtree] File "ASTStart.cc" does not exist. Will create one.
[jjtree] File "ASTMyID.cc" does not exist. Will create one.
[jjtree] File "ASTInteger.cc" does not exist. Will create one.
[jjtree] File "ASTMult.cc" does not exist. Will create one.
[jjtree] File "ParserTree.h" does not exist. Will create one.
[jjtree] File "ParserTreeConstants.h" does not exist. Will create one.
[jjtree] File "JJTParserState.h" does not exist. Will create one.
[jjtree] File "JJTParserState.cc" does not exist. Will create one.
[jjtree] Annotated grammar generated successfully in Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\eg2.jj
[javacc] Java Compiler Compiler Version 6.2_1 (Parser Generator)
[javacc] (type "javacc" with no arguments for help)
[javacc] Reading from file Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\eg2.jj . . .
[javacc] File "JavaCC.h" does not exist. Will create one.
[javacc] File "CharStream.h" does not exist. Will create one.
[javacc] File "CharStream.cc" does not exist. Will create one.
[javacc] File "Token.h" does not exist. Will create one.
[javacc] File "Token.cc" does not exist. Will create one.
[javacc] File "TokenManager.h" does not exist. Will create one.
[javacc] File "TokenMgrError.h" does not exist. Will create one.
[javacc] File "TokenMgrError.cc" does not exist. Will create one.
[javacc] File "ParseException.h" does not exist. Will create one.
[javacc] File "ParseException.cc" does not exist. Will create one.
[javacc] File "Token.h" is being rebuilt.
[javacc] File "Token.cc" is being rebuilt.
[javacc] File "ErrorHandler.h" does not exist. Will create one.
[javacc] Parser generated successfully.
[copy] Copying 1 file to Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2
[copy] Copying 1 file to Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2
[cpptasks:cc] 15 total files to be compiled.
[cpptasks:cc] In file included from Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\SimpleNode.cc:4:0:
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\SimpleNode.h: In constructor EG2::SimpleNode::SimpleNode(EG2::Parser*, int):
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\SimpleNode.h:20:14: attention : EG2::SimpleNode::parser will be initialized after [-Wreorder]
[cpptasks:cc] Parser * parser;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\SimpleNode.h:19:22: attention : int EG2::SimpleNode::id [-Wreorder]
[cpptasks:cc] int id;
[cpptasks:cc] ^
[cpptasks:cc] Z:\dev\llvm\x64\javacc_version_593\examples\JJTreeExamples\cpp\eg2\SimpleNode.cc:14:1: attention : lorsqu'initialis ici [-Wreorder]
[cpptasks:cc] SimpleNode::SimpleNode(Parser *parser, int id) :
[cpptasks:cc] ^
[cpptasks:cc] Starting link
[exec] Reading from standard input...
[exec] Start
[exec] Add
[exec] UnaryExpression
[exec] Integer
[exec] Mult
[exec] UnaryExpression
[exec] Integer
[exec] UnaryExpression
[exec] Add
[exec] UnaryExpression
[exec] Identifier: a
[exec] UnaryExpression
[exec] Identifier: b
[exec] thank you.
BUILD SUCCESSFUL
Total time: 5 seconds
Look at eg2.jjt to see how node annotations can be used to restructure
the parse tree, and at ASTMyID.java to see how you can write your own
node classes that maintain more information from the input stream.
eg3.jjt
-------
This example can be run in the same manner as you ran eg2.jjt.
eg4.jjt
-------
This example again can be run in the same manner as you ran eg2.jjt.
|