How to use SableCC

SableCC is a full compiler compiler. It is a Java-based tool that performs the same job as flex/bison. There are two versions of SableCC that you can use, SableCC 2 and SableCC 3. However, since SableCC 3 accepts SableCC 2 grammars, you will actually use the same Java tool for both.

The difference between them is that SableCC 3 has special rules for CST->AST transformations and an AST productions section. If you want SableCC 3 to behave like SableCC 2, just omit the transformations and the AST section.

1. Making sure prerequisites are installed

Before you can use these instructions, you should first follow the instructions on using Subversion in this class and setting up your environment for JOOS programs. This will ensure that Java 1.5 is on your PATH and that you have a local copy of SableCC 3.2.

2. Setting up your environment

We need to set up some additional environment variables:


Assuming you have checked out the public_html directory from the class SVN repository to $HOME/cs520/public_html, add the following:

      setenv SABLECCDIR $HOME/cs520/public_html/sablecc/sablecc-3.2


      export SABLECCDIR=$HOME/cs520/public_html/sablecc/sablecc-3.2  

to your .cshrc or .bash_profile for tcsh or bash respectively.


Now we would the sablecc script on our PATH for easy execution. Add the following:
      setenv PATH $SABLECCDIR/bin:$PATH


      export PATH=$SABLECCDIR/bin:$PATH

to your .cshrc or .bash_profile for tcsh or bash respectively.

3. Using SableCC

Now that SableCC is installed, you can input your grammar:

      $ sablecc grammar.txt

This will generate many directories. You should not add them to the Subversion repository since it is expected that you will write a Makefile or ant task that can call SableCC to generate them for you. This also means that you should not be modifying the generated code. If you want to see a Makefile that you can work from then see the SableCC 2 and SableCC 3 Tiny expression language examples.

4. Examples

There are many examples of SableCC in use that you can learn from:

5. Documentation

The documentation for SableCC 2 is excellent. The documentation of the SableCC 3 AST extensions is somewhat lacking in comparison, but the information you need is generally available.

If you have questions that your instructor or TA are not able to answer, you can also subscribe to the SableCC mailing list or email Etienne Gagnon directly. Please be sure to send a copy of any questions or responses to your instructor or TA.

Maintained by Eric Bodden. [HOME]