\documentclass{article}
\usepackage{charter}
\usepackage{hyperref}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{xcolor}
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
\begin{document}
\definecolor{javared}{rgb}{0.6,0,0} % for strings
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
\lstdefinelanguage{scala}{
morekeywords={abstract,case,catch,class,def,%
do,else,extends,false,final,finally,%
for,if,implicit,import,match,mixin,%
new,null,object,override,package,%
private,protected,requires,return,sealed,%
super,this,throw,trait,true,try,
type,val,var,while,with,yield},
otherkeywords={=>,<-,<\%,<:,>:,\#,@},
sensitive=true,
morecomment=[l]{//},
morecomment=[n]{/*}{*/},
morestring=[b]",
morestring=[b]',
morestring=[b]"""
}
\lstdefinelanguage{while}{
morekeywords={while, if, then. else, read, write},
otherkeywords={=>,<-,<\%,<:,>:,\#,@},
sensitive=true,
morecomment=[l]{//},
morecomment=[n]{/*}{*/},
morestring=[b]",
morestring=[b]',
morestring=[b]"""
}
\lstset{language=Scala,
basicstyle=\ttfamily,
keywordstyle=\color{javapurple}\bfseries,
stringstyle=\color{javagreen},
commentstyle=\color{javagreen},
morecomment=[s][\color{javadocblue}]{/**}{*/},
numbers=left,
numberstyle=\tiny\color{black},
stepnumber=1,
numbersep=10pt,
tabsize=2,
showspaces=false,
showstringspaces=false}
\section*{Coursework 2}
This coursework is worth 3\% and is due on 27 November at 16:00. You are asked to
\begin{enumerate}
\item implement a tokeniser for the WHILE language,
\item implement a parser and an evaluator for boolean and arithmetic expressions, and
\item write a WHILE program for printing out prime numbers.
\end{enumerate}
\noindent
You need to submit a document containing the answers for the questions
below. You can do the implementation in any programming language you like, but you need
to submit the source code with which you answered the questions. However, the coursework
will \emph{only} be judged according to the answers. You can submit your answers
in a txt-file or as pdf.\bigskip
\subsection*{Question 1 (marked with 1\%)}
Implement a tokeniser for the WHILE language. You first need to design the appropriate
regular expressions for the following nine syntactic entities:
\begin{enumerate}
\item keywords are
\begin{quote}
\texttt{while}, \texttt{if}, \texttt{then}, \texttt{else}, \texttt{do}, \texttt{for}, \texttt{to}, \texttt{true}, \texttt{false}
\texttt{andalso}, \texttt{orelse}, \texttt{read}, \texttt{write}
\end{quote}
\item operators are
\begin{quote}
\texttt{+}, \texttt{-}, \texttt{*}, \texttt{\%}, \texttt{==}, \texttt{!=}, \texttt{>}, \texttt{<}, \texttt{:=}
\end{quote}
\item strings are enclosed by \texttt{"\ldots"}
\item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}}
\item there are semicolons \texttt{;}
\item whitespaces are either \texttt{" "} or \texttt{$\backslash$n}
\item comments either start with $\backslash\,\backslash$ and run to the end of the corresponding line
(indicated by \texttt{$\backslash$n}), or they can also run over several lines but then need to be enclosed by
$\slash\texttt{*}$ as the beginning marker and $\texttt{*}\slash{}$\smallskip as the end marker
\item identifiers are letters followed by underscores \texttt{\_\!\_}, letters
or digits
\item numbers are \texttt{0}, \text{1}, \ldots
\end{enumerate}
\noindent
Once you have implemented all regular expressions for 1 - 9, then
give the token sequence for the Fibonacci program shown below in Fig.~\ref{fib}.
\subsection*{Question 2 (marked with 1\%)}
Implement parser combinators and an evaluation function for arithmetic and boolean
expressions. Arithmetic operations should include \texttt{+}, \texttt{-}, \texttt{*} and
\texttt{\%} (quotient). Boolean
operations should include \texttt{==} (equal), \texttt{!=} (unequal), \texttt{<} and
\texttt{>}.
Using the parser and evaluation function, calculate the values for
\begin{itemize}
\item \texttt{17 < 3 * 3 * 3}
\item \texttt{(29 - 20) * 3}
\item \texttt{79 - 20 * 3}
\item \texttt{2 * 2 != 12 \% 3}
\end{itemize}
\subsection*{Question 3 (marked with 1\%)}
Write a program in the WHILE programming language that prints out all prime numbers between
0 and a fixed number (say 100). Take the grammar of this language from the lectures. As another
guidance have a look at the Fibonacci program
and ``three-nested-loops'' program shown below. For example, printing a variable \texttt{x} in the
WHILE language can be done by using the command \mbox{\texttt{write x}}.
\begin{figure}[h]
\begin{center}
\mbox{\lstinputlisting[language=while]{../progs/fib.while}}
\end{center}
\caption{Fibonacci program in the WHILE language.\label{fib}}
\end{figure}
\begin{figure}[h]
\begin{center}
\mbox{\lstinputlisting[language=while]{../progs/loops.while}}
\end{center}
\caption{The three-nested-loops program in the WHILE language. Usually used for timing measurements.\label{loop}}
\end{figure}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: