Individual Practical (07-08): JavaCC Parser Generator
Introduction
Parser generators are highly useful tools for automatically
generating parsers from annotated grammar specifications. A
classic example is
yacc and the associated lexer
lex
from the C world. JavaCC is one of the most popular parser generators
used for Java.
Accessing JavaCC
JavaCC V4.0 is already installed on DICE machines. If you need to
install it on your own machine, visit the
JavaCC home page,
click on
Documents and Files on the
Project tools menu
on the left hand side.
Download release 4.0, either
javacc-4.0.tar.gz or
javacc-4.0.zip, unpack, and visit the
doc/index.html
file for further installation instructions and documentation.
Documentation
The root page for JavaCC online documentation is
here.
The
FAQ and
Documentation links from this page are
probably the most useful to you.
Start with having a read through the draft
tutorial by Theo Norvell, also the maintainer of the JavaCC
FAQ linked to from the documentation page.
You might also find it useful to look through and try running the
examples from the examples/SimpleExamples/ directory of
the release. See the README file in this directory for
instructions.
There is no need to read about the JJTree class for building syntax trees.
Example Relevant to Part 2
I have put together an example
parser generator for simple arithmetic expressions. The generated
parser parses expression strings into syntax trees built using a
provided tree class. You should easily be able to adapt this
example to create a parser for the Part 2 Boolean query
expressions.
Download the tar and gzipped file
javacc-arith.tgz to your
pt2 directory and unpack with the command
tar xzvf javacc-arith.tgz
The file unpacks to 3 files in the
src/example/arith/
sub-directory:
- Tree.java defines a simple tree class for building the
abstract syntax trees output by the parser.
- Kind.java defines an enumerated type for the kinds of tree
nodes.
- ArithParser.jj is a JavaCC input file which is processed
by the javacc tool to generate an ArithParser java class,
along with auxiliary classes needed by the parser. The start of this
file gives details on how to generate, compile and run the parser, and
includes the output generated by example runs.
Paul Jackson,
Paul.Jackson (@) ed.ac.uk
Last modified: Sat Nov 17 23:00:27 GMT 2007