Assignment Getting Started
In this assignment you will install the software you need for the lab assignments of this course. Also, you will learn the basics of the Tiger language. This language is used in many of the examples and assignments during this course. Furthermore, you will learn how to use some of the tools of the Tiger compiler.
After this session, you should have a comfortable working environment, so that you can immediately start with the real work in the next lab assignment.
Configuring a User-Friendly Shell (aka using
bash instead of
(This section is optional. Please ignore if your are perfectly comfortable using your current shell)
Most of the time, we will be working in a terminal. Therefore, it is important that the shell you are using is user-friendly. For example, it is quite useful if pressing the
key actually deletes a character. For many users,
is easier to use then the default shell on our Linux machines:
(Korn Shell). You can find out what shell you are using by typing:
You can also recognize the shell by its prompt: the usual prompt of
is a plain
. Bash usually shows the current user, directory, followed by a
. You can change the shell to bash by just entering the command
. If you want to change your shell to bash forever, then add the line
the end of the file
. From now, we will assume that you are using bash.
If you Bash prompt is not
, then you can configure bash to use this prompt by adding the line
Preparing for Using Nix
The software you will use in this course is deployed using Nix
. Nix is already installed on the Linux machines. You can easily install Nix at home if you want to work on the assignments on your own computer.
First, you have to prepare your account for using Nix. Please add the following line to
(create the file if it does not exist.
stands for your home directory, aka
. You can edit
with any plain text editor you want (emacs, vi, gedit, ...)
Now, start a new terminal, and start the bash shell (the default shell is
, which is not very user-friendly). Now you should be able to invoke the program
, since the line you have just added to
adds the Nix installation
directory to your path. Try if the tool indeed can be found using:
$ nix-env --help
This should print some information on how to use
, which you can ignore for now. You are now ready to use Nix to install the required software.
Subscribe to the
The software you need during this course is available in a so called Nix channel
. You can use this Nix channel to install all the software safely and quickly and it is easy to update your installation if new software is available.
First, you have to subscribe yourself to this channel:
$ nix-channel --add http://catamaran.labs.cs.uu.nl/dist/stratego/channels/pt-software-stable
Next, update the channels you are subscribed to:
$ nix-channel --update
You can now have a look at the software that is available in this channel:
$ nix-env -qas
This will show a list of packages, all called
. You can now install this software:
$ nix-env -i pt-software
After downloading some files, all the software for this course will be available. You can try this using one or more of the following commands:
$ sglr --help
$ strc --help
$ stratego-shell --help
$ run-tiger --help
(part of the sdf2-bundle) and
(part of Stratego/XT) are in fact already installed in
. Please check if your
is configured correctly by verifying the version of
$ strc --version
This should print 0.14 (not
0.11, which is the version of
For more information about using Nix and Nix channels, please consult the Nix manual
, also available from the Nix
Update Before Every Session
Before starting on your assignments, you should always check if we have updated or added software in the Nix channel. This is done using the following commands:
$ nix-channel --update
$ nix-env -u pt-software
Make GNU Emacs User Friendly
(This section is optional. Please ignore if you already have a favourite editor for Linux)
For some students this will be their first experience with a GNU/Linux system. If you are looking for a good editor, then GNU Emacs is a good choice, but of course you can use whatever editor you want. Unfortunately, GNU Emacs has some rough edges in its default configuration. In this section we will explain how to configure GNU Emacs to make it more user friendly
First, many people are used to
for selection. The CUA mode configures Emacs to use these key bindings. Download the CUA Mode
) to a directory, for example
Next, edit the file
to activate the CUA mode. Add the following lines the end of this file:
(add-to-list 'load-path "~/myemacs")
Now you can start GNU Emacs:
You can specify files that you want to open on the command-line, and you open files via the menu. Check if your
works and cry for help if it does not.
Next, you might want to change the default font of GNU Emacs. You can do that by saving this file
to your home directory.
Learn the Tiger Language
The Tiger language is a small, imperative languages designed by Andrew Appel for his series Modern Compiler Implementation in Java/C/ML
. We will use the Tiger language in this course to study program transformations.
The Tiger Language Reference is part of the book of Andrew Appel, but fortunately some online documents also introduce the language:
Running Tiger Programs
To learn the Tiger language, read one of these manual to learn the basics of the Tiger language. You can experiment with your own Tiger programs by executing them using the tool
, which is an interpreter for Tiger, implemented in Stratego. You can write your Tiger programs in a file, or just specify them on the command-line.
$ run-tiger -i hello-world.tig
$ echo 'print("Hello world")' | run-tiger
$ echo 'printint(1 + 2)' | run-tiger
Try to implement some Tiger programs with control-flow, functions, and variables. An example of the factorial program:
let function fact(n : int) : int =
if n < 1 then 1 else (n * fact(n - 1))
Using Components of the Tiger Compiler
In this part of the assignment you will use some of components of the Tiger compiler to compose real program transformations.
Parsing and Pretty-Printing
The Tiger compiler provides a Tiger parser and a Tiger pretty-printer. The
tool parses a Tiger program and constructs an abstract syntax tree. The details of this tree are not important for now. The
tool pretty-prints a Tiger abstract syntax tree back to an ordinary Tiger program.
$ parse-tiger -i test.tig | pp-tiger
Again, you can also enter some Tiger code at the command-line:
$ echo "1 + 2" | parse-tiger | pp-tiger
Desugaring and Ensugaring
removes some syntax sugar from a Tiger program. Try to find out what this tool does by applying it in the following pipeline:
$ parse-tiger -i ... | Tiger-Desugar | pp-tiger
re-adds the syntactic sugar. Try this by adding it to the pipline:
$ parse-tiger -i ... | Tiger-Desugar | Tiger-Ensugar | pp-tiger
implements a type checker for Tiger. It should be applied after
and the result can pretty-printed with
. Try to some valid and invalid Tiger programs and observe how this tool reports the errors.
is an optimization that inlines function bodies at the call-site. Again, this tool should be applied after Tiger-Desugar. Try to apply this tool to a program with functions. In which cases are functions not inlined?
does not actually remove the function definitions after inlining. The tool
achieves this. Apply it after
to cleanup the code.
You don't actually have to submit anything. If you have done this assignment well, then you will benefit from this while working on the next lab assignments.