JavaCC: Don't talk back

Implementing a parser-analyser

Top 5 reasons to deploy VMware with Tegile

In many cases, with the advent of XML, if data must be exchanged, or information read, a simple solution is to mark that document up using XML and then parse it using an XML parser.

However, in some situations the documents to be processed may not be in XML format. This could be because of legacy systems, external constraints or because they adhere to other standards. In such situations you still need to parse the documents to ensure that they conform to whatever standard is expected, to analyse the contents of such documents to determine the information they provide and to take some action based on that information. To do this you may implement your own parser-analyser or you could choose to look at an existing parser generator tool. Probably the best stabled parser generator tool is JavaCC (Java Compiler Compiler).

Java CC is a parser generator in the best tradition of tools such as YACC (Yet Another Compiler Compiler). JavaCC started out life as Jack and was developed by Sun Microsystems. It was then passed to a company called Metamata and renamed JavaCC. It is now an open source project and is very widely used.

JavaCC offers the Java developer a tool that processes a grammar specification and produces a set of Java classes that can read and analyse input that matches that grammar. JavaCC also provides additional tools that can be used with the main JavaCC tool such as the JJTree tree building tool for displaying grammars. The grammar to be analysed is defined in a BNF-like notation that is quick to learn and allows Java to be embedded within it (to allow callbacks to your own Java code).

Obtaining JavaCC

You can download JavaCC from the official JavaCC website. From here you can download the latest version of the JavaCC classes (or indeed the source if you so wish). What you get when you download JavaCC Binary distribution is:

  • The javacc.jar
  • Some command line tools (such as JJTree)
  • Javadoc Documentation
  • And, a number of examples

You can also find sample JavaCC Grammars here.

A useful textbook is Aho, R. Sethi, and J.D. Ullman, Compilers: Principles, Techniques and Tools, Prentice Hall, 1986. You can buy it at Cash 'n' Carrion.

A Sample Grammar

A JavaCC grammar file can be divided into a number of parts. These parts are:

  • An optional list of options
  • Java compilation unit
  • The lexical specification
  • A list of grammar productions

Each of these is described in a little more detail below:

A List of Options

The developer can influence the way in which the grammar is processed using these options (which may also be specified from the command line if the JavaCC command is being used). In the following example, we declare the LOOKAHEAD option that specifies number of tokens to look ahead before making a decision at a choice point during parsing:

options {

Other options include STATIC (which indicates whether a static only set of methods should be produced), DEBUG_PARSER (which can be used to include debugging information in the generated parser and IGNORE_CASE (which indicates whether the grammar should be case sensitive or not).

Java Compilation Unit

The Java compilation unit is enclosed between PARSER_BEGIN(name) and PARSER_END(name) . The name that follows PARSER_BEGIN and PARSER_END must be the same and this identifies the name of the generated parser. This allows you to provide definitions to be used with the generated Java parser class. For example, in the following code snippet we import the java.io package and define a main method. This allows the resulting SimpleCalculator parser class to be used stand alone. The main method reads a data file called “test.dat” which is then parsed by the generated SimpleCalculator parser class.

import java.io.*;

public class SimpleCalculator {
   public static void main(String [] args) throws Exception {
      File file = new File("test.dat");
      System.out.println("Reading: " + file.getAbsolutePath());
      FileReader reader = new FileReader("test.dat") ;
      SimpleCalculator sc = new SimpleCalculator(reader);
      while (true) {


The Lexical Specification

Next we have our lexical specification. This defines the tokens to be recognised when parsing data input. For example:

   " "
|   "\r"

|  <DIGIT:["0"-"9"]>
|  <EOL: "\n" >


This specifies that white space and carriage returns should be skipped. It then defines the set of tokens to be understood by the parser. In this case it means that NUMBERs are defined as being digits in the range 0 to 9. Finally the EOL (or End Of Line) is defined to be a Newline (“/n”).

Remote control for virtualized desktops

More from The Register

next story
PEAK APPLE: iOS 8 is least popular Cupertino mobile OS in all of HUMAN HISTORY
'Nerd release' finally staggers past 50 per cent adoption
Microsoft to bake Skype into IE, without plugins
Redmond thinks the Object Real-Time Communications API for WebRTC is ready to roll
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Mozilla: Spidermonkey ATE Apple's JavaScriptCore, THRASHED Google V8
Moz man claims the win on rivals' own benchmarks
Yes, Virginia, there IS a W3C HTML5 standard – as of now, that is
You asked for it! You begged for it! Then you gave up! And now it's HERE!
FTDI yanks chip-bricking driver from Windows Update, vows to fight on
Next driver to battle fake chips with 'non-invasive' methods
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Ubuntu 14.10 tries pulling a Steve Ballmer on cloudy offerings
Oi, Windows, centOS and openSUSE – behave, we're all friends here
prev story


Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
Security and trust: The backbone of doing business over the internet
Explores the current state of website security and the contributions Symantec is making to help organizations protect critical data and build trust with customers.