diff -r 53def1fbf472 -r d36363d648e3 coursework/cw02.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/coursework/cw02.tex Sun Nov 03 00:41:24 2013 +0000 @@ -0,0 +1,143 @@ +\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 26 November at 16:00. You are asked to implement +a tokeniser for the WHILE language, an evaluator for boolean and arithmetic expressions and +a WHILE program for printing prime numbers. + +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 pdf.\bigskip + + +\subsection*{Question 1 (marked with 1\%)} + +Implement a tokeniser for the WHILE language. (1) Keywords in this language +are + +\begin{center} +\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{center} + +\noindent +(2) Operators are + +\begin{center} +\texttt{+}, \texttt{-}, \texttt{*}, \texttt{\%}, \texttt{==}, \texttt{!=}, \texttt{>}, \texttt{<}, \texttt{:=} +\end{center} + +\noindent +(3) Strings are enclosed into \texttt{"\ldots"}, (4) you have parentheses \texttt{(}, \texttt{\{}, \texttt{)}, and \texttt{\}}, +(5) there are semicolons \texttt{;}, (6) whitespaces are either \texttt{" "} or \texttt{$\backslash$n}, +(7) comments either start with $\backslash\,\backslash$ and run to the end of the corresponding line +(\texttt{$\backslash$n}), comments can also been given by looking for $\slash\texttt{*}$ as the +beginning marker and $\texttt{*}\slash{}$\smallskip as the end marker. + +\noindent +(8) Identifiers are letters followed by underscores \texttt{\_}, letters +or digits. (9) There are also numbers, like \texttt{0}, \text{1}, \ldots.\medskip + +Once you have implemented all regular expressions for (1) - (9), then +give the token sequence for the Fibonacci program shown below. + +\subsection*{Question 2 (marked with 1\%)} + +Implement parser combinators and an evaluate function for arithmetic and boolean +expressions. Arithmetic operations should include $+$, $-$, $*$, $\%$ (quotient). Boolean +operations should include $==$ (equal), $!=$ (unequal), $<$, $>$. + +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). As a guidance have a look at the Fibonacci program +and three nested loops program shown below. + + +\begin{center} +\mbox{\lstinputlisting[language=while]{../progs/fib.while}} +\end{center} + +\begin{center} +\mbox{\lstinputlisting[language=while]{../progs/loops.while}} +\end{center} + + +\end{document} + +%%% Local Variables: +%%% mode: latex +%%% TeX-master: t +%%% End: