CS 496 - Assignment 3

Due: Thursday, April 3rd by 11:59 PM

Updated 4/1: extended due date, added Hints


In this assignment, you will use CUP to implement a parser for the SPL language.

Getting Started

Use your code for Assignment 2 as a starting point for this assignment.

You will need to make a few changes to your code:

Add the source file BetterSymbol.java to the edu.ycp.cs496.spl.fe package.

Change the definitions of the makeSymbol methods in Lexer.jflex so that they look like this:

private Symbol makeSymbol(int type) {
return new BetterSymbol(type, yyline, yycolumn, yytext());

private Symbol makeSymbol(int type, Object value) {
return new BetterSymbol(type, yyline, yycolumn, value, yytext());

Add the following code to Parser.cup, just below the import statements at the top of the file:

parser code {:
public void syntax_error(Symbol cur_token) {

StringBuffer buf = new StringBuffer();
buf.append("Syntax error at line ");
buf.append(cur_token.left + 1);
buf.append(", character ");

if (cur_token instanceof BetterSymbol) {
buf.append(" near token \"");

report_error(buf.toString(), null);

These changes improve the reporting of syntax errors (when your parser fails to successfully parse an input file.)

Also, replace the existing version of Main.java with the following updated version: Main.java.  This updated version prompts you for the name of the input file to read (e.g., test/t2.spl), so you don't have to enter command line arguments using the Run... dialog.

Your Task

Your task is add productions to Parser.cup so that the parser accepts any legal SPL program.

You should use the following test programs to test your parser:


You should copy these files into the test subdirectory of your project.

You should test your parser by running Main.java by right-clicking on it and choosing Run As->Java Application.  You can just type the name of the input file you want to test.  Here is what a run might look like (user input in bold red):

Enter the name of a file (e.g., "test/t2.spl")
Reading input from test/t7.spl
Parsing was successful

Note: the version of t2.spl distributed with Assignment 2 had two syntax errors.  The version included in the above zip file fixes these errors.

You may need to add some new lexical rules to Lexer.jflex in order to recognize keywords such as if, else, and return, and types such as int, boolean, string, and void.


The overall structure of an SPL source file is a list of declarations.  A declaration is either a variable declaration or a function declaration.

A block is a code region beginning with the begin keyword and ending with the end keyword.  A block consists of a list of 0 or more variable declarations, followed by 0 or more statements.

A statement is one of the following

an expression statement: which is an expression followed by a semicolon

an if or if/else statement:

if ( expression ) block


if ( expression ) block else block

a return statement: which is the return keyword, followed by an expression, followed by a semicolon

The syntax of expressions is very similar to the expression grammar you developed in Assignment 1.

There are 4 kinds of primary expressions in SPL.  Primary expressions have the highest precedence level.

literals (string and integer)

variable references

function calls

explicitly parenthesized expressions


Put Lexer.jflex and Parser.cup into a zip file, and upload the zip file to the Marmoset server as Project 3: