\documentclass[11pt]{article}
\usepackage{fullpage}
\usepackage{epsfig}


\newcommand{\postscript}[2]
 {\setlength{\epsfxsize}{#2\hsize}
  \centerline{\epsfbox{#1}}}

\begin{document}

\centerline{CSCI 2400 -- Models of Computation}
\vspace{0.5cm}
\centerline{\Large Project 2: Yacc}
%\centerline{Interpreter for Snail}
%\vspace{0.5cm}

\section{Overview}
In this assignment you will use the parser generator {\bf yacc}
to construct an {\em interpreter}
for a language called {\em Snail}
containing the set of statements listed below.
The interpreter executes the statements in a Snail program,
one after the other in the order which they appear in the Snail program.

In Section \ref{Snail} we describe the
various statements.
In Section \ref{grammar} we give the grammar
of the {\em Simple} language.
In Section \ref{yacc} we describe what your
yacc code will do.
In Section \ref{handin} are instructions to hand-in
your assignment.

\section{The Snail Programming Language}
\label{Snail}
{\em Snail} is a very simple programming language.
The body of a {\em Snail} program consists of a sequence
of statements.
There are three kinds of statements:
{\em assign},
{\em print},
and
{\em if}.
A basic component for all kinds of statements
is the {\em expression}.
The expression and the statements are described
below.

\begin{itemize}
\item
{\bf expression}

An expression is any mathematical expression
made from identifiers, integers, parenthesis,
the arithmetic operators
\begin{verbatim}
+   -   *   /
\end{verbatim}
and the comparison operators
\begin{verbatim}
<  >  <=  >=  ==  !=
\end{verbatim}
For example, this is a valid expression:
\begin{verbatim}
10 + 20 * (10 < 3)
\end{verbatim}

The {\em value} of an expression is obtained by
executing all the arithmetic operations in the expression.
The result of a comparison operation is 1
if the comparison result is true, and 0 otherwise.
For example, the above expression has value 10
(since, $10 < 3 = 0$).

The value of an identifier is the last value
assigned to it in an assign statement.
An identifier which hasn't been assigned a value before
cannot be used inside an expression
and in this case you should report an error message.

\item
{\bf assign statement}

The assign statement has the form:
\begin{verbatim}
identifier = expression;
\end{verbatim}
For example, this is a valid assign statement:
\begin{verbatim}
var1 = 20 - 3*2;
\end{verbatim}
In the assign statement the identifier gets
the value of the expression.
As an example, in the above assign statement
the new value of variable {\tt var1} is 14.

\item
{\bf print statement}

The print statement print messages
on the screen.
The print statement has one of following forms:
\begin{verbatim}
print ``string'';     //prints the string
print newline;        //prints a newline character
print expression;     //prints the expression value
\end{verbatim}
For example, the execution of the following statements
\begin{verbatim}
print ``The value of 10*5 is '';
print 10*5;
\end{verbatim}
produces the output:
\begin{verbatim}
The value of 10*5 is 50
\end{verbatim}


\item
{\bf if statement}

An if statement has two forms:
\begin{verbatim}
if expression then         //if-then statement
  statement
  statement
  ...                      //more statements
endif


if expression then         //if-then-else statement
  statement
  ...                      //more statements
else
  statement
  ...                      //more statements
endif
\end{verbatim}
The ``if-then'' statement means that
if the expression value is not 0 then
the sequence of statements
between {\tt then} and {\tt endif}
will be executed.
The ``if-then-else'' statement
means that if the expression value
is not 0 then the statements
between {\tt then} and {\tt else} will
be executed,
and otherwise,
if the expression value is 0,
the statements between {\tt else} and {\tt endif}
will be executed.
For example, the following is a valid if statement:
\begin{verbatim}
if (x < 10) then
  print ``x is smaller than ten'';
  x = x - y + 20;
else
  x = 10* y;
endif
\end{verbatim}
\end{itemize}

\vspace{0.5cm}
\noindent
A Snail program is a sequence of
statements and has the following general form:
\begin{verbatim}
statement
statement
  ...
statement
\end{verbatim}
We can have comments in a Snail program
right after ``//'' (as in a C++ program).
An simple example Snail program is the following:
\begin{verbatim}
x = 10;
y = 20;
print ``the value of x is ''; print x; print newline;
print ``the value of y is ''; print y; print newline;
print ``the sum of x and y is ''; print x + y; print newline;

if x < y then                             //test x < y
  print ``x is smaller than y'';          // x is smaller
else
  print ``x is bigger or equal than y''   // x is not smaller
endif
\end{verbatim}
The output of the program is:
\begin{verbatim}
the value of x is 10
the value of y is 20
the sum of x and y is 30;
x is smaller than y;
\end{verbatim}

\section{Snail Grammar}
\label{grammar}
All the Snail programs can be described
by the context-free grammar given below.
The start variable is {\tt program},
the grammar variables are in small letters,
and the terminals in capital letters.
Notice that
this grammar is ambiguous in the {\tt expr} variable;
however,
all the ambiguities can be removed using the
precedence rules of yacc.

%\begin{figure}
\begin{verbatim}
program -> stmt_list

stmt_list -> stmt_list stmt
          |  stmt

stmt -> assign_stmt
     |  print_stmt
     |  if_stmt

assign_stmt -> ID = expr ;

print_stmt -> PRINT expr ;
           |  PRINT STRING ;
           |  PRINT NEWLINE ;

if_stmt -> IF expr THEN stmt_list ENDIF
        |  IF expr THEN stmt_list ELSE stmt_list ENDIF

expr -> ( expr )
     |  expr + expr
     |  expr - expr
     |  expr * expr
     |  expr / expr
     |  expr < expr
     |  expr > expr
     |  expr <= expr
     |  expr >= expr
     |  expr == expr
     |  expr != expr
     |  - expr
     |  INT
     |  ID
\end{verbatim}
%\caption{The snail grammar}
%\label{fig:grammar}
%\end{figure}

\section{Yacc Code}
\label{yacc}
You will write a yacc code which
implements the interpreter for
Snail programs.
The main part of your yacc code
will consist of the snail grammar
augmented with actions.

\begin{itemize}
\item
{\bf expression}

Whenever you find an expression,
you need to evaluate the expression.
This can be done in a bottom up fashion with actions such as:
\begin{verbatim}
expr : expr '+' expr {$$ = $1 + $3;}
\end{verbatim}

\item
{\bf assign statement}

The value of an {\tt ID} (identifier) node
is stored in the symbol table.
For an {\tt assign} node
you need to update the value of the
variable to be equal to the {\tt expr}
of the right hand side of the assign
statement.

\item
{\bf print statement}

For a {\tt print} node
you just print on the output
the contents of the {\tt STRING}
stripped from quotes, or the value of the {\tt expr},
or a newline character.

\item
{\bf if statement}

For the if statement you first evaluate the expr.
If the value of expr is true (not zero),
then you execute the
if part of the statement, otherwise you
execute the else part of the statement.
You need to be careful not to execute the part of the
if statement you don't want to execute.
In the following
example,
when expression is true then only statement1 will
be executed,
while when expression is false then only statement2 will be executed.
\begin{verbatim}
if expression then
  statement1
else
  statement2
endif
\end{verbatim}
In order to achieve this,
you need to have a stack
which will control whether or not to execute
particular statements.
We push true (1) for the part of the if which will be executed;
for the other part we push false (0).
The stack is needed in order to handle nested if statements.
The code to handle this is as follows:
\begin{verbatim}
if_stmt: IF expr
         THEN {top()==1 ? push($2!=0); : push(0);} stmt_list {pop();}
         ELSE {top()==1 ? push($2==0); : push(0);} stmt_list {pop();} ENDIF
\end{verbatim}
If the top of the stack contains true then statements
can be executed.
For example, the actions of the assignment statement:
\begin{verbatim}
assign_stmt -> ID = expr ; {if (top() == 1) then {execute the assignment}}
\end{verbatim}
In order to handle statements and expressions in the main body,
outside from if statements, you need to push true at the beginning
of the execution of the interpreter.
\end{itemize}

If you find a syntax error in the input program
then report an error message with the
line number where you found the error and abort the
program.

Your yacc program will use parts of the
the lexical analyzer you built
in the first project
and for this you need to modify appropriately
the lex code.

\section{Hand-In}
\label{handin}
You should hand-in in paper your lex and yacc code.
Also you should hand-in the output of your interpreter
for Snail test programs which are given
in the course web page.

In the course web page you can also find
example yacc programs
(together with lex programs) that will help
you to get started with your project.

\end{document}
