Feeds

JavaCC: Don't talk back

Implementing a parser-analyser

Secure remote control for conventional and virtual desktops

A list of grammar productions

The final section of the .jj file defines the context-free grammar that is a set of grammar productions (rules) that define what the legal syntax for the input data should be. In the case of a parser to analyse a programming language this would contain the grammar rules for that language. In our case we will define a very simple set of rules that define a very simple syntax. The syntax will allow two integers to be added or subtracted. Thus the set of productions for this grammar might be:

expr := element + element
|  element – element
number := digit
digit := [0 – 9]
EOL = “\n”

We will define this grammar in a JavaCC .jj file. The resultant parser will check the input has a legal format as defined by the grammar file and will then print out the result of the addition or subtraction.

The following is the list of grammar productions defined for our simple example in JavaCC syntax:

void calc():
{ 
    double x;
}
{
   x=expr() <EOL> {System.out.println(x); }   
|  <EOF> {System.exit(0); }
}

double expr():
{
   double a;
   double b;
}
{
   a = element() (
   "+" b=element() {a += b; }
|  "-" b=element() {a -= b;}
   )
   { return a; }
}


double element():
{
   Token t;
}
{
   t=<NUMBER> { System.out.println("t: " + t) ; return Double.parseDouble(t.toString()); }
}

This states that:

A calc element is comprised of

  • an expression followed by an End Of Line; or
  • an End Of File marker.

If the expression is found then the value returned is printed out while if an end of file marker is found then the program terminates. Note that in this way call backs can be made to any arbitrary Java code. It is via this mechanism that the parser can be linked into your own code.

Note that the format used to define the grammar is a translation of BNF (Backus-Naur-Form) notation used with the JavaCC compiler.

The remaining grammar productions define what comprises an expression and an element. In particular an expression is defined to be either:

  • an element,
  • followed by either a “+” and another element, or
  • a “-“ and another element.

Note in our case we also specify that the two elements should be added or subtracted and the result returned from this production.

Finally an element is defined as being any type of NUMBER. Note that the result of something being a number is that we return that number as a double value.

Generating the Parser

We are now in a position to generate our Java implementation parser class. This is done using the JavaCC program. For example:

JavaCC -NOSTATIC -OUTPUT_DIRECTORY=source SimpleCalculator.jj

The options included on the command line to the JavaCC program specify:

  • -NOSTATIC – which generates a parser with instance methods (the default is to generate a static class with static methods).
  • -OUTPUT_DIRECTORY – which tells the JavaCC compiler where to put the generated classes (the default is the current directory)
  • SimpleCalculator.jj – which is the file containing the specification of the JavaCC grammar.

With the javacc.jar file on the classpath, the resulting output generated is:

Java Compiler Compiler Version 4.0 (Parser Generator)
(type "JavaCC" with no arguments for help)
Reading from file SimpleCalculator.jj . . .
File "TokenMgrError.java" does not exist.  Will create one.
File "ParseException.java" does not exist.  Will create one.
File "Token.java" does not exist.  Will create one.
File "SimpleCharStream.java" does not exist.  Will create one.
Parser generated with 0 errors and 1 warnings.

This generates 7 classes as listed below:

ParseException.java
SimpleCalculator.java
SimpleCalculatorConstants.java
SimpleCalculatorTokenManager.java
SimpleCharStream.java
Token.java
TokenMgrError.java

As a combination these classes define the SimpleCalculator parser. If you are interested, the primary class to look at is the SimplerCalculator.java class, as it is this one which is both the entry point and the primary implementation of the parser.

To execute this parser we first need a simple file to process. In our case the file is called test.dat and contains the following:

2 + 3

4 – 6

We can compile the classes for the classes generated by JavaCC (again with the javacc.jar on the classpath). The parser can then be run by issuing the following:

java SimpleCalculator

The result of running the parser is:

Reading: C:\experis\projects\javacc\test.dat
t: 2
t: 3
5.0
t: 4
t: 6
-2.0

Summary

JavaCC is a very powerful tool that can be used to create parsers for a wide range of data formats, including RTF, Visual Basic, Java itself, HTML, MHEG-5, C, C++, Excel files, numeric formula, email headers etc. It can be easily integrated into your own code as well as being used in stand-alone fashion as illustrated here.

Complete Listing of SimpleCalculator.jj

options {
  LOOKAHEAD=2;
}

PARSER_BEGIN(SimpleCalculator)
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) {
         sc.calc();
      }
   }
}

PARSER_END(SimpleCalculator)

SKIP:
{
   " "
|   "\r"
}

TOKEN:
{
   <NUMBER:(<DIGIT>)>
|  <DIGIT:["0"-"9"]>
|  <EOL: "\n" >

}   

void calc():
{ 
    double x;
}
{
   x=expr() <EOL> {System.out.println(x); }   
|  <EOF> {System.exit(0); }
}

double expr():
{
   double a;
   double b;
}
{
   a = element() (
   "+" b=element() {a += b; }
|  "-" b=element() {a -= b;}
   )
   { return a; }
}


double element():
{
   Token t;
}
{
   t=<NUMBER> { System.out.println("t: " + t) ; return Double.parseDouble(t.toString()); }
}

Top 5 reasons to deploy VMware with Tegile

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
Netscape plugins about to stop working in Chrome for Mac
Google kills off 32-bit Chrome, only on Mac
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
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?
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.