Course Homepage

slang-format Exercise

Overview

In this exercise you will implement an automatic code formatter, called slang-format, for our class’s own custom programming language called Slang (short for Simple Language). Most of the code for the slang-format executable will be given to you, and all you will need to do is hand-copy the remaining code from a given screenshot. The code you will be copying defines structures representing the nodes of the Slang abstract syntax tree (AST). The purpose of this exercise is to give you some hands-on experience working with an AST, and to introduce you to the codebase and data structures you will be working with for the remaining projects in this course.

Setup

Run the following commands to download the starter code for this project, and to set up your git repository for project submission:

mkdir -p ~/cop3402fal2026/
wget https://www.cs.ucf.edu/~pappas/teaching/cop3402/fal2026/files/slang-format-starter-code.tar
tar -xvf slang-format-starter-code.tar -C ~/cop3402fal2026/
cd ~/cop3402fal2026/slang-format
git init
git add .
git commit
# Enter a commit message in the editor that pops up
git remote add submission gitolite3@eustis3.eecs.ucf.edu:cop3402/$USER/slang-format
git push --set-upstream submission master

Starter code structure

The starter code has the following structure:

.
├── .gitignore          # .gitignore file telling git to ignore binary outputs.
├── grammar.ebnf        # File defining Slang's grammar.
├── include             # Header files.
│   ├── cli.h           # Header for the compiler's command line interface.
│   ├── globals.h       # Header declaring global variables.
│   ├── lex.h           # Header for the Slang compiler's lexer.
│   └── util.h          # Header declaring various utility functions.
├── Makefile            # The project's Makefile.
├── src                 # C files.
│   ├── cli.c           # Definition of Slang's command-line interface.
│   ├── globals.c       # Definition of Slang's global variables.
│   ├── lex.c           # Definition of Slang's lexer.
│   ├── parse.c         # Definition of Slang's parser.
│   ├── slang-format.c  # Entry point for the Slang auto-formatter.
│   └── util.c          # Defintion of utility functions.
└── test.sg             # Simple Slang file for testing slang-format with.

All these files must be added, committed, and pushed with git. Do not add to source control any generated or binary files (e.g., .o files, or the bin/slang-format binary itself).

In addition to the above files, you will need to create, stage, commit, and push a single file: include/parse.h. The next section explains what to put in this file.

include/parse.h

Hand-type by yourself the code for the file include/parse.h shown in the image below. Receiving a copy of the text of this file, using an LLM to write it for you, or copying it in any other way besides personally typing it out yourself is unauthorized assistance. Feel free to zoom in or download the image to obtain a closer look at it.

include/parse.h

Building your project

To build your project, run make:

make

Running bin/slang-format

You can pass Slang code directly to bin/slang-format via the command line, or via a file. Via the command line:

cat <<EOF | ./bin/slang-format
begin main(a, b, c)
locals d, e, f
a = 1
return 0
end
EOF

And via a file:

./bin/slang-format test.sg

For example, if you run the following command:

cat <<EOF | ./bin/slang-format
begin main(x, y)
x = 1 + 2
y = 3
if x = y goto done
done:
return 0
end
EOF

Then you should receive the following output:

begin main(x, y)
    x = 1 + 2
    y = 3
    if x = y goto done
done:
    return 0
end

Submitting your project

See the hello project for instructions on using git to submit your project. You must push all the starter code files, in addition to include/parse.h. Do not add to source control any generated or binary files (e.g., .o files, or the bin/slang-format binary itself).

Self-check

You can test that you copied the image file correctly by running make test:

make test

If you code works correctly, then the output of make test should end with the formatted version of the code in test.sg:

begin main(a, b, c)
    locals d, e, f
    a = 1
    return 0
end

Note that the grading server will not test your code against this test case, but instead a secret, different test case. As long as you correctly copied the code in include/parse.h, your code will pass this secret test case as well.

You should also verify that the above test still passes in a fresh clone of your repo. See the hello project for instructions on cloning a project from the grading server.

Grading schema

Criterion Points
The git repo exists 1
The repo contains the file include/parse.h 1
The program builds 1
The program passes the secret test case 1
TOTAL 4