Introduction to Compiler Construction in a Java World


Click the following link to download the latest release of j-- up-to-date with bug fixes (recommended):

Click the following link to download the original release of j-- published with the book in December 2012:


Setting Up j-- for Command-Line Execution

  • Unpack into some folder. We will refer to this folder as $j. Configure the environment variable PATH to include $j/j--/bin.

  • Install J2SE 7 or later and configure the environment variable PATH to include the path to the Java binaries. For example, on Windows, PATH might include C:\Program Files\Java\jdk1.7.0\bin.

  • Install Ant 1.8.2 or later, set the environment variable ANT_HOME to point to the folder where it is installed, and configure environment variable PATH to include $ANT_HOME/bin.

You are now ready to work with j-- compiler. To compile and test the compiler using Ant, execute the appropriate Ant target as follows:

$j/j--> ant <target>

If no target is specified, the default (runCompilerTests) target is executed. To obtain a list of available Ant targets, execute the following command:

$j/j--> ant help

The j-- compiler can be executed directly on the command line using the script $j/j--/bin/j--. For example $j/j--/tests/pass/ can be compiled using j– as follows:

$j/j--> j-- tests/pass/

The class file HelloWorld.class produced under $j/j--/pass/ can be run as follows:

$j/j--> java pass.HelloWorld

The full command-line syntax for the j-- script is as follows:

Usage: j-- <options> <source file>
where possible options include:
  -t Only tokenize input and print tokens to STDOUT
  -p Only parse input and print AST to STDOUT
  -pa Only parse and pre-analyze input and print AST to STDOUT
  -a Only parse, pre-analyze, and analyze input and print AST to STDOUT
  -s <naive|linear|graph> Generate SPIM code
  -r <num> Max. physical registers (1-18) available for allocation; default = 8
  -d <dir> Specify where to place output files; default = .

Setting Up j-- in Eclipse

  • Install J2SE 7 or later and set the environment variable JAVA_HOME to point to the installation folder. For example, on Windows, JAVA_HOME might be set to C:\Program Files\Java\jdk1.7.0.

  • Setup an Eclipse project for j-- as follows:

    • Unpack into a temporary folder, say /tmp.

    • In Eclipse, click the File → New → Project … menu to bring up the New Project dialog box. Select Java Project and click Next. In the New Java Project dialog box, type “j--” for Project Name, make sure JRE (Java Run-time Environment) used is 1.7 or later, and click Finish. This creates an empty Java project called “j--” in the current workspace.

    • In the Project Explorer pane on the left, select the “j--” project. Click File → Import … menu to bring up the Import dialog box. Under General, select File System as the Import Source, and click Next. Choose “/tmp/j--” as the From directory, select “j--” below, and click Finish. Answer Yes to All to the question on the Question pop-up menu. This imports the j-- files into the “j--” project. Once the import is complete, Eclipse will automatically build the project. The automatic build feature can be turned off, if necessary, by clicking the Project → Build Automatically menu.

To build the j-- project manually, select the project in the Project Explorer window and click the Project → Build Project menu. This runs the default (runCompilerTests) target in the Ant file $j/j--/build.xml. To run a different target, edit the following line in the Ant file:

<project default="runCompilerTests">

and change the value of the default attribute to the desired target.

In order to run and debug the compiler within Eclipse, a Launch Configuration has to be created, which can be done as follows:

  • Click the Run → Run … menu to bring up the Run dialog box. Select Java Application, and click the New Launch Configuration button.

  • Give a suitable name for the configuration. Select “j--” for Project and type “jminusminus.Main” for Main class.

  • In the (x) = Arguments tab, type appropriate values for Program arguments; these are the same as the arguments for the $j/j--/bin/j-- script described. For example, type “-t tests/pass/” to tokenize the j-- test program using the hand-written scanner.

  • Click Apply and Close.

  • You can have as many configurations as you like. To run or debug a particular configuration, click Run → Run … or Run → Debug … menu, select the configuration, and click Run or Debug. The output (STDOUT and STDERR) messages from the compiler are redirected to the Console pane.


Each software made available here is free; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

The software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.