author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 01 Dec 2013 10:08:53 +0000 | |
changeset 216 | f5ec7c597c5b |
parent 200 | 7415871b1ef5 |
child 275 | 618c7640cf66 |
permissions | -rw-r--r-- |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{hyperref} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{amssymb} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
\usepackage{amsmath} |
216
f5ec7c597c5b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
200
diff
changeset
|
5 |
\usepackage{../langs} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
\begin{document} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
\section*{Coursework 2} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
12 |
{\bf UPDATE:} There was a typo in Q1 about the regular expressions for comments: |
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
13 |
they should, of course, start with $\slash\slash$, as in C for example, not with |
199
89cafeb66f40
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
198
diff
changeset
|
14 |
$\backslash\backslash$. (Thanks to Bryan who pointed out this error.)\bigskip\bigskip |
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
15 |
|
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
16 |
\noindent |
200
7415871b1ef5
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
199
diff
changeset
|
17 |
This coursework is worth 3\% and is due on 29 November at 16:00. You are asked to |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
19 |
\begin{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
20 |
\item implement a tokeniser for the WHILE language, |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
21 |
\item implement a parser and an evaluator for boolean and arithmetic expressions, and |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
22 |
\item write a WHILE program for printing out prime numbers. |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
23 |
\end{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
24 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
25 |
\noindent |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
You need to submit a document containing the answers for the questions |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
below. You can do the implementation in any programming language you like, but you need |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
to submit the source code with which you answered the questions. However, the coursework |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
will \emph{only} be judged according to the answers. You can submit your answers |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
30 |
in a txt-file or as pdf.\bigskip |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
\subsection*{Question 1 (marked with 1\%)} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
35 |
Implement a tokeniser for the WHILE language. You first need to design the appropriate |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
36 |
regular expressions for the following nine syntactic entities: |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
38 |
\begin{enumerate} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
39 |
\item keywords are |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
40 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
41 |
\begin{quote} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
\texttt{while}, \texttt{if}, \texttt{then}, \texttt{else}, \texttt{do}, \texttt{for}, \texttt{to}, \texttt{true}, \texttt{false} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
\texttt{andalso}, \texttt{orelse}, \texttt{read}, \texttt{write} |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
44 |
\end{quote} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
45 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
46 |
\item operators are |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
47 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
48 |
\begin{quote} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
49 |
\texttt{+}, \texttt{-}, \texttt{*}, \texttt{\%}, \texttt{==}, \texttt{!=}, \texttt{>}, \texttt{<}, \texttt{:=} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
50 |
\end{quote} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
51 |
|
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
52 |
\item strings are enclosed by \texttt{"\ldots"} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
53 |
\item parentheses are \texttt{(}, \texttt{\{}, \texttt{)} and \texttt{\}} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
54 |
\item there are semicolons \texttt{;} |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
55 |
\item whitespaces are either \texttt{" "} or \texttt{$\backslash$n} |
198
f54972b0f641
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
182
diff
changeset
|
56 |
\item comments either start with $\slash\,\slash$ and run to the end of the corresponding line |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
57 |
(indicated by \texttt{$\backslash$n}), or they can also run over several lines but then need to be enclosed by |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
58 |
$\slash\texttt{*}$ as the beginning marker and $\texttt{*}\slash{}$\smallskip as the end marker |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
59 |
\item identifiers are letters followed by underscores \texttt{\_\!\_}, letters |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
60 |
or digits |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
61 |
\item numbers are \texttt{0}, \text{1}, \ldots |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
62 |
\end{enumerate} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
\noindent |
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
65 |
Once you have implemented all regular expressions for 1 - 9, then |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
66 |
give the token sequence for the Fibonacci program shown below in Fig.~\ref{fib}. |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
\subsection*{Question 2 (marked with 1\%)} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
|
180
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
70 |
Implement parser combinators and an evaluation function for arithmetic and boolean |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
71 |
expressions. Arithmetic operations should include \texttt{+}, \texttt{-}, \texttt{*} and |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
72 |
\texttt{\%} (quotient). Boolean |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
73 |
operations should include \texttt{==} (equal), \texttt{!=} (unequal), \texttt{<} and |
50e8dcd95ae3
added cw
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
179
diff
changeset
|
74 |
\texttt{>}. |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
Using the parser and evaluation function, calculate the values for |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
\begin{itemize} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
\item \texttt{17 < 3 * 3 * 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
\item \texttt{(29 - 20) * 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
\item \texttt{79 - 20 * 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
\item \texttt{2 * 2 != 12 \% 3} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
\end{itemize} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
\subsection*{Question 3 (marked with 1\%)} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
Write a program in the WHILE programming language that prints out all prime numbers between |
182
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
88 |
0 and a fixed number (say 100). A partial grammar of the WHILE language is given below. |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
89 |
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
90 |
\begin{center} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
91 |
\begin{tabular}{@{}lcl@{}} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
92 |
\textit{Stmt} & $\rightarrow$ & $\texttt{skip}$\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
93 |
& $|$ & \textit{Id}\;\texttt{:=}\;\textit{AExp}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
94 |
& $|$ & \texttt{if}\; \textit{BExp} \;\texttt{then}\; \textit{Block} \;\texttt{else}\; \textit{Block}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
95 |
& $|$ & \texttt{while}\; \textit{BExp} \;\texttt{do}\; \textit{Block}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
96 |
& $|$ & \texttt{read}\;\textit{Id}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
97 |
& $|$ & \texttt{write}\;\textit{Id}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
98 |
& $|$ & \texttt{write}\;\textit{String}\medskip\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
99 |
\textit{Stmts} & $\rightarrow$ & \textit{Stmt} \;\texttt{;}\; \textit{Stmts}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
100 |
& $|$ & \textit{Stmt}\medskip\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
101 |
\textit{Block} & $\rightarrow$ & \texttt{\{}\,\textit{Stmts}\,\texttt{\}}\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
102 |
& $|$ & \textit{Stmt}\medskip\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
103 |
\textit{AExp} & $\rightarrow$ & \ldots\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
104 |
\textit{BExp} & $\rightarrow$ & \ldots\\ |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
105 |
\end{tabular} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
106 |
\end{center} |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
107 |
|
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
108 |
\noindent |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
109 |
As another guidance for your program have a look at the Fibonacci program |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
110 |
and ``three-nested-loops'' program shown below in Figures \ref{fib} and \ref{loop}. |
9ce2414e470e
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
181
diff
changeset
|
111 |
|
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
113 |
\begin{figure}[h] |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
\begin{center} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
\mbox{\lstinputlisting[language=while]{../progs/fib.while}} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
\end{center} |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
117 |
\caption{Fibonacci program in the WHILE language.\label{fib}} |
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
118 |
\end{figure} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
|
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
120 |
\begin{figure}[h] |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
\begin{center} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
\mbox{\lstinputlisting[language=while]{../progs/loops.while}} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
\end{center} |
181
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
124 |
\caption{The three-nested-loops program in the WHILE language. Usually used for timing measurements.\label{loop}} |
1f98d215df71
added material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
180
diff
changeset
|
125 |
\end{figure} |
178
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
\end{document} |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
|
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
%%% Local Variables: |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
%%% mode: latex |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
%%% TeX-master: t |
d36363d648e3
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
%%% End: |