author | Christian Urban <urbanc@in.tum.de> |
Sat, 16 Dec 2017 23:53:28 +0000 | |
changeset 166 | 780c40aaad27 |
parent 163 | 84917f2e16cd |
child 191 | f78b18c4c886 |
permissions | -rw-r--r-- |
6 | 1 |
\documentclass{article} |
62 | 2 |
\usepackage{../style} |
78 | 3 |
\usepackage{../langs} |
166 | 4 |
\usepackage{disclaimer} |
153 | 5 |
\usepackage{tikz} |
6 |
\usepackage{pgf} |
|
7 |
\usepackage{pgfplots} |
|
154 | 8 |
\usepackage{stackengine} |
166 | 9 |
%% \usepackage{accents} |
154 | 10 |
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}} |
153 | 11 |
|
12 |
\begin{filecontents}{re-python2.data} |
|
13 |
1 0.033 |
|
14 |
5 0.036 |
|
15 |
10 0.034 |
|
16 |
15 0.036 |
|
17 |
18 0.059 |
|
166 | 18 |
19 0.084 |
153 | 19 |
20 0.141 |
20 |
21 0.248 |
|
21 |
22 0.485 |
|
22 |
23 0.878 |
|
23 |
24 1.71 |
|
24 |
25 3.40 |
|
25 |
26 7.08 |
|
26 |
27 14.12 |
|
27 |
28 26.69 |
|
28 |
\end{filecontents} |
|
29 |
||
30 |
\begin{filecontents}{re-java.data} |
|
31 |
5 0.00298 |
|
32 |
10 0.00418 |
|
33 |
15 0.00996 |
|
34 |
16 0.01710 |
|
35 |
17 0.03492 |
|
36 |
18 0.03303 |
|
37 |
19 0.05084 |
|
38 |
20 0.10177 |
|
39 |
21 0.19960 |
|
40 |
22 0.41159 |
|
41 |
23 0.82234 |
|
42 |
24 1.70251 |
|
43 |
25 3.36112 |
|
44 |
26 6.63998 |
|
45 |
27 13.35120 |
|
46 |
28 29.81185 |
|
47 |
\end{filecontents} |
|
48 |
||
163 | 49 |
\begin{filecontents}{re-java9.data} |
50 |
1000 0.01410 |
|
51 |
2000 0.04882 |
|
52 |
3000 0.10609 |
|
53 |
4000 0.17456 |
|
54 |
5000 0.27530 |
|
55 |
6000 0.41116 |
|
56 |
7000 0.53741 |
|
57 |
8000 0.70261 |
|
58 |
9000 0.93981 |
|
59 |
10000 0.97419 |
|
60 |
11000 1.28697 |
|
61 |
12000 1.51387 |
|
62 |
14000 2.07079 |
|
63 |
16000 2.69846 |
|
64 |
20000 4.41823 |
|
65 |
24000 6.46077 |
|
66 |
26000 7.64373 |
|
67 |
30000 9.99446 |
|
68 |
34000 12.966885 |
|
69 |
38000 16.281621 |
|
70 |
42000 19.180228 |
|
71 |
46000 21.984721 |
|
72 |
50000 26.950203 |
|
73 |
60000 43.0327746 |
|
74 |
\end{filecontents} |
|
75 |
||
6 | 76 |
|
77 |
\begin{document} |
|
78 |
||
154 | 79 |
|
80 |
\section*{Coursework 8 (Regular Expressions and Brainf***)} |
|
6 | 81 |
|
79 | 82 |
This coursework is worth 10\%. It is about regular expressions, |
153 | 83 |
pattern matching and an interpreter. The first part is due on 30 |
152 | 84 |
November at 11pm; the second, more advanced part, is due on 21 |
153 | 85 |
December at 11pm. In the first part, you are asked to implement a |
86 |
regular expression matcher based on derivatives of regular |
|
87 |
expressions. The reason is that regular expression matching in Java |
|
156 | 88 |
and Python can sometimes be extremely slow. The advanced part is about |
89 |
an interpreter for a very simple programming language.\bigskip |
|
62 | 90 |
|
166 | 91 |
\IMPORTANT{} |
152 | 92 |
|
93 |
\noindent |
|
94 |
Also note that the running time of each part will be restricted to a |
|
166 | 95 |
maximum of 360 seconds on my laptop. |
6 | 96 |
|
166 | 97 |
\DISCLAIMER{} |
6 | 98 |
|
99 |
||
68 | 100 |
\subsection*{Part 1 (6 Marks)} |
6 | 101 |
|
69 | 102 |
The task is to implement a regular expression matcher that is based on |
153 | 103 |
derivatives of regular expressions. Most of the functions are defined by |
104 |
recursion over regular expressions and can be elegantly implemented |
|
105 |
using Scala's pattern-matching. The implementation should deal with the |
|
106 |
following regular expressions, which have been predefined in the file |
|
107 |
\texttt{re.scala}: |
|
6 | 108 |
|
109 |
\begin{center} |
|
110 |
\begin{tabular}{lcll} |
|
111 |
$r$ & $::=$ & $\ZERO$ & cannot match anything\\ |
|
112 |
& $|$ & $\ONE$ & can only match the empty string\\ |
|
158 | 113 |
& $|$ & $c$ & can match a single character (in this case $c$)\\ |
68 | 114 |
& $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\ |
115 |
& $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\ |
|
116 |
& & & then the second part with $r_2$\\ |
|
6 | 117 |
& $|$ & $r^*$ & can match zero or more times $r$\\ |
118 |
\end{tabular} |
|
119 |
\end{center} |
|
120 |
||
68 | 121 |
\noindent |
152 | 122 |
Why? Knowing how to match regular expressions and strings will let you |
123 |
solve a lot of problems that vex other humans. Regular expressions are |
|
124 |
one of the fastest and simplest ways to match patterns in text, and |
|
125 |
are endlessly useful for searching, editing and analysing data in all |
|
126 |
sorts of places (for example analysing network traffic in order to |
|
127 |
detect security breaches). However, you need to be fast, otherwise you |
|
128 |
will stumble over problems such as recently reported at |
|
68 | 129 |
|
130 |
{\small |
|
131 |
\begin{itemize} |
|
132 |
\item[$\bullet$] \url{http://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016} |
|
133 |
\item[$\bullet$] \url{https://vimeo.com/112065252} |
|
134 |
\item[$\bullet$] \url{http://davidvgalbraith.com/how-i-fixed-atom/} |
|
135 |
\end{itemize}} |
|
136 |
||
79 | 137 |
\subsubsection*{Tasks (file re.scala)} |
68 | 138 |
|
158 | 139 |
The file \texttt{re.scala} has already a definition for regular |
140 |
expressions and also defines some handy shorthand notation for |
|
141 |
regular expressions. The notation in this document matches up |
|
142 |
with the code in the file as follows: |
|
143 |
||
144 |
\begin{center} |
|
145 |
\begin{tabular}{rcl@{\hspace{10mm}}l} |
|
146 |
& & code: & shorthand:\smallskip \\ |
|
147 |
$\ZERO$ & $\mapsto$ & \texttt{ZERO}\\ |
|
148 |
$\ONE$ & $\mapsto$ & \texttt{ONE}\\ |
|
149 |
$c$ & $\mapsto$ & \texttt{CHAR(c)}\\ |
|
150 |
$r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)} & \texttt{r1 | r2}\\ |
|
151 |
$r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)} & \texttt{r1 $\sim$ r2}\\ |
|
152 |
$r^*$ & $\mapsto$ & \texttt{STAR(r)} & \texttt{r.\%} |
|
153 |
\end{tabular} |
|
154 |
\end{center} |
|
155 |
||
156 |
||
68 | 157 |
\begin{itemize} |
152 | 158 |
\item[(1a)] Implement a function, called \textit{nullable}, by |
159 |
recursion over regular expressions. This function tests whether a |
|
153 | 160 |
regular expression can match the empty string. This means given a |
158 | 161 |
regular expression it either returns true or false. The function |
162 |
\textit{nullable} |
|
163 |
is defined as follows: |
|
6 | 164 |
|
165 |
\begin{center} |
|
166 |
\begin{tabular}{lcl} |
|
167 |
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\ |
|
168 |
$\textit{nullable}(\ONE)$ & $\dn$ & $\textit{true}$\\ |
|
169 |
$\textit{nullable}(c)$ & $\dn$ & $\textit{false}$\\ |
|
170 |
$\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\ |
|
171 |
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\ |
|
172 |
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\ |
|
173 |
\end{tabular} |
|
158 | 174 |
\end{center}~\hfill[1 Mark] |
68 | 175 |
|
176 |
\item[(1b)] Implement a function, called \textit{der}, by recursion over |
|
177 |
regular expressions. It takes a character and a regular expression |
|
69 | 178 |
as arguments and calculates the derivative regular expression according |
179 |
to the rules: |
|
6 | 180 |
|
181 |
\begin{center} |
|
182 |
\begin{tabular}{lcl} |
|
183 |
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\ |
|
184 |
$\textit{der}\;c\;(\ONE)$ & $\dn$ & $\ZERO$\\ |
|
185 |
$\textit{der}\;c\;(d)$ & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\ |
|
186 |
$\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\ |
|
187 |
$\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\ |
|
188 |
& & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\ |
|
189 |
& & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\ |
|
190 |
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\ |
|
191 |
\end{tabular} |
|
69 | 192 |
\end{center} |
193 |
||
194 |
For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives |
|
195 |
w.r.t.~the characters $a$, $b$ and $c$ are |
|
196 |
||
197 |
\begin{center} |
|
198 |
\begin{tabular}{lcll} |
|
199 |
$\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & ($= r'$)\\ |
|
200 |
$\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\ |
|
201 |
$\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$ |
|
202 |
\end{tabular} |
|
203 |
\end{center} |
|
204 |
||
205 |
Let $r'$ stand for the first derivative, then taking the derivatives of $r'$ |
|
206 |
w.r.t.~the characters $a$, $b$ and $c$ gives |
|
207 |
||
208 |
\begin{center} |
|
209 |
\begin{tabular}{lcll} |
|
210 |
$\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\ |
|
211 |
$\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & ($= r''$)\\ |
|
212 |
$\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ |
|
213 |
\end{tabular} |
|
214 |
\end{center} |
|
215 |
||
216 |
One more example: Let $r''$ stand for the second derivative above, |
|
217 |
then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$ |
|
218 |
and $c$ gives |
|
219 |
||
220 |
\begin{center} |
|
221 |
\begin{tabular}{lcll} |
|
222 |
$\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\ |
|
223 |
$\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\ |
|
152 | 224 |
$\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ & |
225 |
(is $\textit{nullable}$) |
|
69 | 226 |
\end{tabular} |
227 |
\end{center} |
|
228 |
||
229 |
Note, the last derivative can match the empty string, that is it is \textit{nullable}.\\ |
|
230 |
\mbox{}\hfill\mbox{[1 Mark]} |
|
6 | 231 |
|
68 | 232 |
\item[(1c)] Implement the function \textit{simp}, which recursively |
69 | 233 |
traverses a regular expression from the inside to the outside, and |
153 | 234 |
on the way simplifies every regular expression on the left (see |
235 |
below) to the regular expression on the right, except it does not |
|
236 |
simplify inside ${}^*$-regular expressions. |
|
6 | 237 |
|
68 | 238 |
\begin{center} |
69 | 239 |
\begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll} |
6 | 240 |
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ |
241 |
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ |
|
242 |
$r \cdot \ONE$ & $\mapsto$ & $r$\\ |
|
243 |
$\ONE \cdot r$ & $\mapsto$ & $r$\\ |
|
244 |
$r + \ZERO$ & $\mapsto$ & $r$\\ |
|
245 |
$\ZERO + r$ & $\mapsto$ & $r$\\ |
|
246 |
$r + r$ & $\mapsto$ & $r$\\ |
|
247 |
\end{tabular} |
|
68 | 248 |
\end{center} |
249 |
||
69 | 250 |
For example the regular expression |
68 | 251 |
\[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\] |
252 |
||
153 | 253 |
simplifies to just $r_1$. \textbf{Hint:} Regular expressions can be |
79 | 254 |
seen as trees and there are several methods for traversing |
153 | 255 |
trees. One of them corresponds to the inside-out traversal, which is |
256 |
sometimes also called post-order traversal. Furthermore, |
|
257 |
remember numerical expressions from school times: there you had expressions |
|
152 | 258 |
like $u + \ldots + (1 \cdot x) - \ldots (z + (y \cdot 0)) \ldots$ |
79 | 259 |
and simplification rules that looked very similar to rules |
260 |
above. You would simplify such numerical expressions by replacing |
|
261 |
for example the $y \cdot 0$ by $0$, or $1\cdot x$ by $x$, and then |
|
152 | 262 |
look whether more rules are applicable. If you organise the |
79 | 263 |
simplification in an inside-out fashion, it is always clear which |
153 | 264 |
rule should be applied next.\hfill[2 Marks] |
68 | 265 |
|
266 |
\item[(1d)] Implement two functions: The first, called \textit{ders}, |
|
69 | 267 |
takes a list of characters and a regular expression as arguments, and |
268 |
builds the derivative w.r.t.~the list as follows: |
|
68 | 269 |
|
270 |
\begin{center} |
|
271 |
\begin{tabular}{lcl} |
|
69 | 272 |
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\ |
273 |
$\textit{ders}\;(c::cs)\;r$ & $\dn$ & |
|
68 | 274 |
$\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\ |
275 |
\end{tabular} |
|
6 | 276 |
\end{center} |
277 |
||
78 | 278 |
Note that this function is different from \textit{der}, which only |
279 |
takes a single character. |
|
280 |
||
281 |
The second function, called \textit{matcher}, takes a string and a |
|
282 |
regular expression as arguments. It builds first the derivatives |
|
283 |
according to \textit{ders} and after that tests whether the resulting |
|
284 |
derivative regular expression can match the empty string (using |
|
285 |
\textit{nullable}). For example the \textit{matcher} will produce |
|
153 | 286 |
true for the regular expression $(a\cdot b)\cdot c$ and the string |
287 |
$abc$, but false if you give it the string $ab$. \hfill[1 Mark] |
|
78 | 288 |
|
153 | 289 |
\item[(1e)] Implement a function, called \textit{size}, by recursion |
78 | 290 |
over regular expressions. If a regular expression is seen as a tree, |
291 |
then \textit{size} should return the number of nodes in such a |
|
292 |
tree. Therefore this function is defined as follows: |
|
293 |
||
294 |
\begin{center} |
|
295 |
\begin{tabular}{lcl} |
|
296 |
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\ |
|
297 |
$\textit{size}(\ONE)$ & $\dn$ & $1$\\ |
|
298 |
$\textit{size}(c)$ & $\dn$ & $1$\\ |
|
299 |
$\textit{size}(r_1 + r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\ |
|
300 |
$\textit{size}(r_1 \cdot r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\ |
|
301 |
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\ |
|
302 |
\end{tabular} |
|
303 |
\end{center} |
|
304 |
||
153 | 305 |
You can use \textit{size} in order to test how much the `evil' regular |
306 |
expression $(a^*)^* \cdot b$ grows when taking successive derivatives |
|
307 |
according the letter $a$ without simplification and then compare it to |
|
308 |
taking the derivative, but simplify the result. The sizes |
|
309 |
are given in \texttt{re.scala}. \hfill[1 Mark] |
|
310 |
\end{itemize} |
|
78 | 311 |
|
94
ae4708c851ee
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
312 |
\subsection*{Background} |
78 | 313 |
|
94
ae4708c851ee
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
314 |
Although easily implementable in Scala, the idea behind the derivative |
ae4708c851ee
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
315 |
function might not so easy to be seen. To understand its purpose |
ae4708c851ee
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
316 |
better, assume a regular expression $r$ can match strings of the form |
152 | 317 |
$c\!::\!cs$ (that means strings which start with a character $c$ and have |
153 | 318 |
some rest, or tail, $cs$). If you take the derivative of $r$ with |
319 |
respect to the character $c$, then you obtain a regular expression |
|
94
ae4708c851ee
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
320 |
that can match all the strings $cs$. In other words, the regular |
153 | 321 |
expression $\textit{der}\;c\;r$ can match the same strings $c\!::\!cs$ |
94
ae4708c851ee
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
86
diff
changeset
|
322 |
that can be matched by $r$, except that the $c$ is chopped off. |
75 | 323 |
|
324 |
Assume now $r$ can match the string $abc$. If you take the derivative |
|
325 |
according to $a$ then you obtain a regular expression that can match |
|
326 |
$bc$ (it is $abc$ where the $a$ has been chopped off). If you now |
|
153 | 327 |
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you |
78 | 328 |
obtain a regular expression that can match the string $c$ (it is $bc$ |
329 |
where $b$ is chopped off). If you finally build the derivative of this |
|
330 |
according $c$, that is |
|
153 | 331 |
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain |
332 |
a regular expression that can match the empty string. You can test |
|
333 |
whether this is indeed the case using the function nullable, which is |
|
334 |
what your matcher is doing. |
|
75 | 335 |
|
153 | 336 |
The purpose of the $\textit{simp}$ function is to keep the regular |
158 | 337 |
expressions small. Normally the derivative function makes the regular |
153 | 338 |
expression bigger (see the SEQ case and the example in (1b)) and the |
339 |
algorithm would be slower and slower over time. The $\textit{simp}$ |
|
340 |
function counters this increase in size and the result is that the |
|
341 |
algorithm is fast throughout. By the way, this algorithm is by Janusz |
|
342 |
Brzozowski who came up with the idea of derivatives in 1964 in his PhD |
|
343 |
thesis. |
|
75 | 344 |
|
78 | 345 |
\begin{center}\small |
346 |
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)} |
|
347 |
\end{center} |
|
6 | 348 |
|
153 | 349 |
|
350 |
If you want to see how badly the regular expression matchers do in |
|
158 | 351 |
Java\footnote{Version 8 and below; Version 9 does not seem to be as |
352 |
catastrophic, but still worse than the regular expression matcher |
|
353 |
based on derivatives.} and in Python with the `evil' regular |
|
156 | 354 |
expression $(a^*)^*\cdot b$, then have a look at the graphs below (you |
355 |
can try it out for yourself: have a look at the file |
|
356 |
\texttt{catastrophic.java} and \texttt{catastrophic.py} on |
|
357 |
KEATS). Compare this with the matcher you have implemented. How long |
|
358 |
can the string of $a$'s be in your matcher and still stay within the |
|
359 |
30 seconds time limit? |
|
153 | 360 |
|
361 |
\begin{center} |
|
163 | 362 |
\begin{tabular}{@{}cc@{}} |
363 |
\multicolumn{2}{c}{Graph: $(a^*)^*\cdot b$ and strings |
|
364 |
$\underbrace{a\ldots a}_{n}$}\bigskip\\ |
|
365 |
||
153 | 366 |
\begin{tikzpicture} |
367 |
\begin{axis}[ |
|
368 |
xlabel={$n$}, |
|
369 |
x label style={at={(1.05,0.0)}}, |
|
370 |
ylabel={time in secs}, |
|
163 | 371 |
y label style={at={(0.06,0.5)}}, |
153 | 372 |
enlargelimits=false, |
373 |
xtick={0,5,...,30}, |
|
374 |
xmax=33, |
|
163 | 375 |
ymax=45, |
376 |
ytick={0,5,...,40}, |
|
153 | 377 |
scaled ticks=false, |
378 |
axis lines=left, |
|
379 |
width=6cm, |
|
158 | 380 |
height=5.5cm, |
156 | 381 |
legend entries={Python, Java 8}, |
163 | 382 |
legend pos=north west] |
153 | 383 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data}; |
384 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data}; |
|
385 |
\end{axis} |
|
386 |
\end{tikzpicture} |
|
163 | 387 |
& |
388 |
\begin{tikzpicture} |
|
389 |
\begin{axis}[ |
|
390 |
xlabel={$n$}, |
|
391 |
x label style={at={(1.05,0.0)}}, |
|
392 |
ylabel={time in secs}, |
|
393 |
y label style={at={(0.06,0.5)}}, |
|
394 |
%enlargelimits=false, |
|
395 |
%xtick={0,5000,...,30000}, |
|
396 |
xmax=65000, |
|
397 |
ymax=45, |
|
398 |
ytick={0,5,...,40}, |
|
399 |
scaled ticks=false, |
|
400 |
axis lines=left, |
|
401 |
width=6cm, |
|
402 |
height=5.5cm, |
|
403 |
legend entries={Java 9}, |
|
404 |
legend pos=north west] |
|
405 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java9.data}; |
|
406 |
\end{axis} |
|
407 |
\end{tikzpicture} |
|
408 |
\end{tabular} |
|
153 | 409 |
\end{center} |
410 |
\newpage |
|
411 |
||
412 |
\subsection*{Part 2 (4 Marks)} |
|
413 |
||
154 | 414 |
Coming from Java or C++, you might think Scala is a quite esoteric |
415 |
programming language. But remember, some serious companies have built |
|
416 |
their business on |
|
417 |
Scala.\footnote{\url{https://en.wikipedia.org/wiki/Scala_(programming_language)\#Companies}} |
|
156 | 418 |
And there are far, far more esoteric languages out there. One is |
419 |
called \emph{brainf***}. You are asked in this part to implement an |
|
154 | 420 |
interpreter for this language. |
421 |
||
422 |
Urban M\"uller developed brainf*** in 1993. A close relative of this |
|
423 |
language was already introduced in 1964 by Corado B\"ohm, an Italian |
|
424 |
computer pioneer, who unfortunately died a few months ago. The main |
|
425 |
feature of brainf*** is its minimalistic set of instructions---just 8 |
|
426 |
instructions in total and all of which are single characters. Despite |
|
427 |
the minimalism, this language has been shown to be Turing |
|
428 |
complete\ldots{}if this doesn't ring any bell with you: it roughly |
|
158 | 429 |
means that every algorithm we know can, in principle, be implemented in |
154 | 430 |
brainf***. It just takes a lot of determination and quite a lot of |
156 | 431 |
memory resources. Some relatively sophisticated sample programs in |
154 | 432 |
brainf*** are given in the file \texttt{bf.scala}.\bigskip |
153 | 433 |
|
154 | 434 |
\noindent |
435 |
As mentioned above, brainf*** has 8 single-character commands, namely |
|
436 |
\texttt{'>'}, \texttt{'<'}, \texttt{'+'}, \texttt{'-'}, \texttt{'.'}, |
|
437 |
\texttt{','}, \texttt{'['} and \texttt{']'}. Every other character is |
|
438 |
considered a comment. Brainf*** operates on memory cells containing |
|
439 |
integers. For this it uses a single memory pointer that points at each |
|
440 |
stage to one memory cell. This pointer can be moved forward by one |
|
441 |
memory cell by using the command \texttt{'>'}, and backward by using |
|
442 |
\texttt{'<'}. The commands \texttt{'+'} and \texttt{'-'} increase, |
|
443 |
respectively decrease, by 1 the content of the memory cell to which |
|
444 |
the memory pointer currently points to. The commands for input/output |
|
445 |
are \texttt{','} and \texttt{'.'}. Output works by reading the content |
|
446 |
of the memory cell to which the memory pointer points to and printing |
|
447 |
it out as an ASCII character. Input works the other way, taking some |
|
448 |
user input and storing it in the cell to which the memory pointer |
|
449 |
points to. The commands \texttt{'['} and \texttt{']'} are looping |
|
450 |
constructs. Everything in between \texttt{'['} and \texttt{']'} is |
|
451 |
repeated until a counter (memory cell) reaches zero. A typical |
|
452 |
program in brainf*** looks as follows: |
|
153 | 453 |
|
154 | 454 |
\begin{center} |
455 |
\begin{verbatim} |
|
456 |
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++ |
|
457 |
..+++.>>.<-.<.+++.------.--------.>>+.>++. |
|
458 |
\end{verbatim} |
|
459 |
\end{center} |
|
460 |
||
461 |
\noindent |
|
462 |
This one prints out Hello World\ldots{}obviously. |
|
153 | 463 |
|
464 |
\subsubsection*{Tasks (file bf.scala)} |
|
465 |
||
466 |
\begin{itemize} |
|
154 | 467 |
\item[(2a)] Brainf*** memory is represented by a \texttt{Map} from |
468 |
integers to integers. The empty memory is represented by |
|
469 |
\texttt{Map()}, that is nothing is stored in the |
|
158 | 470 |
memory. \texttt{Map(0 -> 1, 2 -> 3)} clearly stores \texttt{1} at |
471 |
memory location \texttt{0}; at \texttt{2} it stores \texttt{3}. The |
|
472 |
convention is that if we query the memory at a location that is |
|
473 |
\emph{not} defined in the \texttt{Map}, we return \texttt{0}. Write |
|
474 |
a function, \texttt{sread}, that takes a memory (a \texttt{Map}) and |
|
475 |
a memory pointer (an \texttt{Int}) as argument, and safely reads the |
|
476 |
corresponding memory location. If the \texttt{Map} is not defined at |
|
477 |
the memory pointer, \texttt{sread} returns \texttt{0}. |
|
154 | 478 |
|
479 |
Write another function \texttt{write}, which takes a memory, a |
|
158 | 480 |
memory pointer and an integer value as argument and updates the |
481 |
\texttt{Map} with the value at the given memory location. As usual |
|
482 |
the \texttt{Map} is not updated `in-place' but a new map is created |
|
483 |
with the same data, except the value is stored at the given memory |
|
484 |
pointer.\hfill[1 Mark] |
|
154 | 485 |
|
486 |
\item[(2b)] Write two functions, \texttt{jumpRight} and |
|
487 |
\texttt{jumpLeft} that are needed to implement the loop constructs |
|
488 |
of brainf***. They take a program (a \texttt{String}) and a program |
|
489 |
counter (an \texttt{Int}) as argument and move right (respectively |
|
490 |
left) in the string in order to find the \textbf{matching} |
|
491 |
opening/closing bracket. For example, given the following program |
|
492 |
with the program counter indicated by an arrow: |
|
493 |
||
494 |
\begin{center} |
|
495 |
\texttt{--[\barbelow{.}.+>--],>,++} |
|
496 |
\end{center} |
|
497 |
||
498 |
then the matching closing bracket is in 9th position (counting from 0) and |
|
499 |
\texttt{jumpRight} is supposed to return the position just after this |
|
500 |
||
501 |
\begin{center} |
|
502 |
\texttt{--[..+>--]\barbelow{,}>,++} |
|
503 |
\end{center} |
|
504 |
||
158 | 505 |
meaning it jumps to after the loop. Similarly, if you are in 8th position |
154 | 506 |
then \texttt{jumpLeft} is supposed to jump to just after the opening |
507 |
bracket (that is jumping to the beginning of the loop): |
|
508 |
||
509 |
\begin{center} |
|
510 |
\texttt{--[..+>-\barbelow{-}],>,++} |
|
511 |
\qquad$\stackrel{\texttt{jumpLeft}}{\longrightarrow}$\qquad |
|
512 |
\texttt{--[\barbelow{.}.+>--],>,++} |
|
513 |
\end{center} |
|
514 |
||
515 |
Unfortunately we have to take into account that there might be |
|
157 | 516 |
other opening and closing brackets on the `way' to find the |
154 | 517 |
matching bracket. For example in the brainf*** program |
518 |
||
519 |
\begin{center} |
|
520 |
\texttt{--[\barbelow{.}.[+>]--],>,++} |
|
521 |
\end{center} |
|
522 |
||
523 |
we do not want to return the index for the \texttt{'-'} in the 9th |
|
524 |
position, but the program counter for \texttt{','} in 12th |
|
157 | 525 |
position. The easiest to find out whether a bracket is matched is by |
526 |
using levels (which are the third argument in \texttt{jumpLeft} and |
|
154 | 527 |
\texttt{jumpLeft}). In case of \texttt{jumpRight} you increase the |
528 |
level by one whenever you find an opening bracket and decrease by |
|
529 |
one for a closing bracket. Then in \texttt{jumpRight} you are looking |
|
530 |
for the closing bracket on level \texttt{0}. For \texttt{jumpLeft} you |
|
531 |
do the opposite. In this way you can find \textbf{matching} brackets |
|
532 |
in strings such as |
|
533 |
||
534 |
\begin{center} |
|
535 |
\texttt{--[\barbelow{.}.[[-]+>[.]]--],>,++} |
|
536 |
\end{center} |
|
537 |
||
538 |
for which \texttt{jumpRight} should produce the position: |
|
539 |
||
540 |
\begin{center} |
|
541 |
\texttt{--[..[[-]+>[.]]--]\barbelow{,}>,++} |
|
542 |
\end{center} |
|
543 |
||
544 |
It is also possible that the position returned by \texttt{jumpRight} or |
|
545 |
\texttt{jumpLeft} is outside the string in cases where there are |
|
546 |
no matching brackets. For example |
|
153 | 547 |
|
154 | 548 |
\begin{center} |
549 |
\texttt{--[\barbelow{.}.[[-]+>[.]]--,>,++} |
|
550 |
\qquad$\stackrel{\texttt{jumpRight}}{\longrightarrow}$\qquad |
|
551 |
\texttt{--[..[[-]+>[.]]-->,++\barbelow{\;\phantom{+}}} |
|
552 |
\end{center} |
|
553 |
\hfill[1 Mark] |
|
554 |
||
555 |
||
556 |
\item[(2c)] Write a recursive function \texttt{run} that executes a |
|
557 |
brainf*** program. It takes a program, a program counter, a memory |
|
157 | 558 |
pointer and a memory as arguments. If the program counter is outside |
154 | 559 |
the program string, the execution stops and \texttt{run} returns the |
560 |
memory. If the program counter is inside the string, it reads the |
|
157 | 561 |
corresponding character and updates the program counter \texttt{pc}, |
562 |
memory pointer \texttt{mp} and memory \texttt{mem} according to the |
|
563 |
rules shown in Figure~\ref{comms}. It then calls recursively |
|
564 |
\texttt{run} with the updated data. |
|
153 | 565 |
|
154 | 566 |
Write another function \texttt{start} that calls \texttt{run} with a |
157 | 567 |
given brainfu** program and memory, and the program counter and memory pointer |
154 | 568 |
set to~$0$. Like \texttt{run} it returns the memory after the execution |
569 |
of the program finishes. You can test your brainf**k interpreter with the |
|
155 | 570 |
Sierpinski triangle or the Hello world programs or have a look at |
571 |
||
572 |
\begin{center} |
|
573 |
\url{https://esolangs.org/wiki/Brainfuck} |
|
574 |
\end{center}\hfill[2 Marks] |
|
154 | 575 |
|
576 |
\begin{figure}[p] |
|
577 |
\begin{center} |
|
578 |
\begin{tabular}{|@{}p{0.8cm}|l|} |
|
579 |
\hline |
|
580 |
\hfill\texttt{'>'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
581 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
582 |
$\bullet$ & $\texttt{mp} + 1$\\ |
|
583 |
$\bullet$ & \texttt{mem} unchanged |
|
584 |
\end{tabular}\\\hline |
|
585 |
\hfill\texttt{'<'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
586 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
587 |
$\bullet$ & $\texttt{mp} - 1$\\ |
|
588 |
$\bullet$ & \texttt{mem} unchanged |
|
589 |
\end{tabular}\\\hline |
|
590 |
\hfill\texttt{'+'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
591 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
592 |
$\bullet$ & $\texttt{mp}$ unchanged\\ |
|
593 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) + 1}\\ |
|
594 |
\end{tabular}\\\hline |
|
595 |
\hfill\texttt{'-'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
596 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
597 |
$\bullet$ & $\texttt{mp}$ unchanged\\ |
|
598 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) - 1}\\ |
|
599 |
\end{tabular}\\\hline |
|
600 |
\hfill\texttt{'.'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
601 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
602 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\ |
|
158 | 603 |
$\bullet$ & print out \,\texttt{mem(mp)} as a character\\ |
154 | 604 |
\end{tabular}\\\hline |
605 |
\hfill\texttt{','} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
606 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
607 |
$\bullet$ & $\texttt{mp}$ unchanged\\ |
|
608 |
$\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\ |
|
158 | 609 |
\multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}} |
154 | 610 |
\end{tabular}\\\hline |
611 |
\hfill\texttt{'['} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
612 |
\multicolumn{2}{@{}l}{if \texttt{mem(mp) == 0} then}\\ |
|
613 |
$\bullet$ & $\texttt{pc = jumpRight(prog, pc + 1, 0)}$\\ |
|
614 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\ |
|
615 |
\multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) != 0} then}\\ |
|
616 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
617 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\ |
|
618 |
\end{tabular} |
|
619 |
\\\hline |
|
620 |
\hfill\texttt{']'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
621 |
\multicolumn{2}{@{}l}{if \texttt{mem(mp) != 0} then}\\ |
|
159 | 622 |
$\bullet$ & $\texttt{pc = jumpLeft(prog, pc - 1, 0)}$\\ |
154 | 623 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\ |
624 |
\multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) == 0} then}\\ |
|
625 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
626 |
$\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\ |
|
627 |
\end{tabular}\\\hline |
|
628 |
any other char & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}} |
|
629 |
$\bullet$ & $\texttt{pc} + 1$\\ |
|
630 |
$\bullet$ & \texttt{mp} and \texttt{mem} unchanged |
|
631 |
\end{tabular}\\ |
|
632 |
\hline |
|
633 |
\end{tabular} |
|
634 |
\end{center} |
|
157 | 635 |
\caption{The rules for how commands in the brainf*** language update the program counter \texttt{pc}, |
636 |
memory pointer \texttt{mp} and memory \texttt{mem}.\label{comms}} |
|
154 | 637 |
\end{figure} |
153 | 638 |
\end{itemize}\bigskip |
639 |
||
640 |
||
641 |
||
642 |
||
6 | 643 |
\end{document} |
644 |
||
68 | 645 |
|
6 | 646 |
%%% Local Variables: |
647 |
%%% mode: latex |
|
648 |
%%% TeX-master: t |
|
649 |
%%% End: |