author | Christian Urban <christian.urban@kcl.ac.uk> |
Tue, 24 Nov 2020 09:04:06 +0000 | |
changeset 366 | 1c829680503e |
parent 356 | d1046d9d3213 |
child 390 | 175a950470a9 |
permissions | -rw-r--r-- |
257 | 1 |
% !TEX program = xelatex |
6 | 2 |
\documentclass{article} |
62 | 3 |
\usepackage{../style} |
78 | 4 |
\usepackage{../langs} |
218 | 5 |
\usepackage{disclaimer} |
6 |
\usepackage{tikz} |
|
7 |
\usepackage{pgf} |
|
8 |
\usepackage{pgfplots} |
|
9 |
\usepackage{stackengine} |
|
10 |
%% \usepackage{accents} |
|
11 |
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}} |
|
12 |
||
13 |
\begin{filecontents}{re-python2.data} |
|
14 |
1 0.033 |
|
15 |
5 0.036 |
|
16 |
10 0.034 |
|
17 |
15 0.036 |
|
18 |
18 0.059 |
|
19 |
19 0.084 |
|
20 |
20 0.141 |
|
21 |
21 0.248 |
|
22 |
22 0.485 |
|
23 |
23 0.878 |
|
24 |
24 1.71 |
|
25 |
25 3.40 |
|
26 |
26 7.08 |
|
27 |
27 14.12 |
|
28 |
28 26.69 |
|
29 |
\end{filecontents} |
|
30 |
||
31 |
\begin{filecontents}{re-java.data} |
|
32 |
5 0.00298 |
|
33 |
10 0.00418 |
|
34 |
15 0.00996 |
|
35 |
16 0.01710 |
|
36 |
17 0.03492 |
|
37 |
18 0.03303 |
|
38 |
19 0.05084 |
|
39 |
20 0.10177 |
|
40 |
21 0.19960 |
|
41 |
22 0.41159 |
|
42 |
23 0.82234 |
|
43 |
24 1.70251 |
|
44 |
25 3.36112 |
|
45 |
26 6.63998 |
|
46 |
27 13.35120 |
|
47 |
28 29.81185 |
|
48 |
\end{filecontents} |
|
49 |
||
221 | 50 |
\begin{filecontents}{re-js.data} |
51 |
5 0.061 |
|
52 |
10 0.061 |
|
53 |
15 0.061 |
|
54 |
20 0.070 |
|
55 |
23 0.131 |
|
56 |
25 0.308 |
|
57 |
26 0.564 |
|
58 |
28 1.994 |
|
59 |
30 7.648 |
|
60 |
31 15.881 |
|
61 |
32 32.190 |
|
62 |
\end{filecontents} |
|
63 |
||
218 | 64 |
\begin{filecontents}{re-java9.data} |
65 |
1000 0.01410 |
|
66 |
2000 0.04882 |
|
67 |
3000 0.10609 |
|
68 |
4000 0.17456 |
|
69 |
5000 0.27530 |
|
70 |
6000 0.41116 |
|
71 |
7000 0.53741 |
|
72 |
8000 0.70261 |
|
73 |
9000 0.93981 |
|
74 |
10000 0.97419 |
|
75 |
11000 1.28697 |
|
76 |
12000 1.51387 |
|
77 |
14000 2.07079 |
|
78 |
16000 2.69846 |
|
79 |
20000 4.41823 |
|
80 |
24000 6.46077 |
|
81 |
26000 7.64373 |
|
82 |
30000 9.99446 |
|
83 |
34000 12.966885 |
|
84 |
38000 16.281621 |
|
85 |
42000 19.180228 |
|
86 |
46000 21.984721 |
|
87 |
50000 26.950203 |
|
88 |
60000 43.0327746 |
|
89 |
\end{filecontents} |
|
90 |
||
351 | 91 |
\begin{filecontents}{re-swift.data} |
92 |
5 0.001 |
|
93 |
10 0.001 |
|
94 |
15 0.009 |
|
95 |
20 0.178 |
|
96 |
23 1.399 |
|
97 |
24 2.893 |
|
98 |
25 5.671 |
|
99 |
26 11.357 |
|
100 |
27 22.430 |
|
101 |
\end{filecontents} |
|
102 |
||
103 |
\begin{filecontents}{re-dart.data} |
|
104 |
20 0.042 |
|
105 |
21 0.084 |
|
106 |
22 0.190 |
|
107 |
23 0.340 |
|
108 |
24 0.678 |
|
109 |
25 1.369 |
|
110 |
26 2.700 |
|
111 |
27 5.462 |
|
112 |
28 10.908 |
|
113 |
29 21.725 |
|
114 |
30 43.492 |
|
115 |
\end{filecontents} |
|
6 | 116 |
|
117 |
\begin{document} |
|
118 |
||
218 | 119 |
% BF IDE |
120 |
% https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5 |
|
121 |
||
356 | 122 |
\section*{Main Part 3 (Scala, 7 Marks)} |
6 | 123 |
|
351 | 124 |
%\mbox{}\hfill\textit{``[Google’s MapReduce] abstraction is inspired by the}\\ |
125 |
%\mbox{}\hfill\textit{map and reduce primitives present in Lisp and many}\\ |
|
126 |
%\mbox{}\hfill\textit{other functional language.''}\smallskip\\ |
|
127 |
%\mbox{}\hfill\textit{ --- Dean and Ghemawat, who designed this concept at Google} |
|
128 |
%\bigskip\medskip |
|
275 | 129 |
|
130 |
\noindent |
|
351 | 131 |
This part is about a regular expression matcher described by |
132 |
Brzozowski in 1964. This part is due on \cwEIGHTa{} at 5pm. The |
|
133 |
background is that ``out-of-the-box'' regular expression matching in |
|
134 |
mainstream languages like Java, JavaScript and Python can sometimes be |
|
135 |
excruciatingly slow. You are supposed to implement a regular |
|
136 |
expression matcher that is much, much faster. \bigskip |
|
218 | 137 |
|
351 | 138 |
\IMPORTANTNONE{} |
62 | 139 |
|
140 |
\noindent |
|
218 | 141 |
Also note that the running time of each part will be restricted to a |
257 | 142 |
maximum of 30 seconds on my laptop. |
218 | 143 |
|
144 |
\DISCLAIMER{} |
|
86 | 145 |
|
221 | 146 |
\subsection*{Reference Implementation} |
147 |
||
351 | 148 |
This Scala assignment comes with a reference implementation in form of |
149 |
a \texttt{jar}-file. This allows you to run any test cases on your own |
|
221 | 150 |
computer. For example you can call Scala on the command line with the |
151 |
option \texttt{-cp re.jar} and then query any function from the |
|
351 | 152 |
\texttt{re.scala} template file. As usual you have to prefix the calls |
153 |
with \texttt{CW8c} or import this object. Since some tasks |
|
154 |
are time sensitive, you can check the reference implementation as |
|
155 |
follows: if you want to know, for example, how long it takes to match |
|
156 |
strings of $a$'s using the regular expression $(a^*)^*\cdot b$ you can |
|
157 |
query as follows: |
|
221 | 158 |
|
159 |
||
245 | 160 |
\begin{lstlisting}[xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small] |
221 | 161 |
$ scala -cp re.jar |
351 | 162 |
scala> import CW8c._ |
221 | 163 |
scala> for (i <- 0 to 5000000 by 500000) { |
292 | 164 |
| println(f"$i: ${time_needed(2, matcher(EVIL, "a" * i))}%.5f secs.") |
221 | 165 |
| } |
292 | 166 |
0: 0.00002 secs. |
167 |
500000: 0.10608 secs. |
|
168 |
1000000: 0.22286 secs. |
|
169 |
1500000: 0.35982 secs. |
|
170 |
2000000: 0.45828 secs. |
|
171 |
2500000: 0.59558 secs. |
|
172 |
3000000: 0.73191 secs. |
|
173 |
3500000: 0.83499 secs. |
|
174 |
4000000: 0.99149 secs. |
|
175 |
4500000: 1.15395 secs. |
|
176 |
5000000: 1.29659 secs. |
|
221 | 177 |
\end{lstlisting}%$ |
178 |
||
351 | 179 |
\subsection*{Preliminaries} |
218 | 180 |
|
181 |
The task is to implement a regular expression matcher that is based on |
|
182 |
derivatives of regular expressions. Most of the functions are defined by |
|
183 |
recursion over regular expressions and can be elegantly implemented |
|
184 |
using Scala's pattern-matching. The implementation should deal with the |
|
185 |
following regular expressions, which have been predefined in the file |
|
186 |
\texttt{re.scala}: |
|
6 | 187 |
|
218 | 188 |
\begin{center} |
189 |
\begin{tabular}{lcll} |
|
190 |
$r$ & $::=$ & $\ZERO$ & cannot match anything\\ |
|
191 |
& $|$ & $\ONE$ & can only match the empty string\\ |
|
192 |
& $|$ & $c$ & can match a single character (in this case $c$)\\ |
|
193 |
& $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\ |
|
194 |
& $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\ |
|
195 |
& & & then the second part with $r_2$\\ |
|
221 | 196 |
& $|$ & $r^*$ & can match a string with zero or more copies of $r$\\ |
218 | 197 |
\end{tabular} |
198 |
\end{center} |
|
6 | 199 |
|
218 | 200 |
\noindent |
221 | 201 |
Why? Regular expressions are |
202 |
one of the simplest ways to match patterns in text, and |
|
218 | 203 |
are endlessly useful for searching, editing and analysing data in all |
204 |
sorts of places (for example analysing network traffic in order to |
|
205 |
detect security breaches). However, you need to be fast, otherwise you |
|
206 |
will stumble over problems such as recently reported at |
|
207 |
||
208 |
{\small |
|
209 |
\begin{itemize} |
|
289 | 210 |
\item[$\bullet$] \url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019} |
211 |
\item[$\bullet$] \url{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016} |
|
218 | 212 |
\item[$\bullet$] \url{https://vimeo.com/112065252} |
289 | 213 |
\item[$\bullet$] \url{https://davidvgalbraith.com/how-i-fixed-atom} |
218 | 214 |
\end{itemize}} |
215 |
||
221 | 216 |
% Knowing how to match regular expressions and strings will let you |
217 |
% solve a lot of problems that vex other humans. |
|
218 |
||
219 |
||
218 | 220 |
\subsubsection*{Tasks (file re.scala)} |
6 | 221 |
|
218 | 222 |
The file \texttt{re.scala} has already a definition for regular |
223 |
expressions and also defines some handy shorthand notation for |
|
224 |
regular expressions. The notation in this document matches up |
|
225 |
with the code in the file as follows: |
|
226 |
||
227 |
\begin{center} |
|
228 |
\begin{tabular}{rcl@{\hspace{10mm}}l} |
|
229 |
& & code: & shorthand:\smallskip \\ |
|
230 |
$\ZERO$ & $\mapsto$ & \texttt{ZERO}\\ |
|
231 |
$\ONE$ & $\mapsto$ & \texttt{ONE}\\ |
|
232 |
$c$ & $\mapsto$ & \texttt{CHAR(c)}\\ |
|
233 |
$r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)} & \texttt{r1 | r2}\\ |
|
234 |
$r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)} & \texttt{r1 $\sim$ r2}\\ |
|
235 |
$r^*$ & $\mapsto$ & \texttt{STAR(r)} & \texttt{r.\%} |
|
236 |
\end{tabular} |
|
237 |
\end{center} |
|
238 |
||
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
239 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
240 |
\begin{itemize} |
351 | 241 |
\item[(1)] Implement a function, called \textit{nullable}, by |
218 | 242 |
recursion over regular expressions. This function tests whether a |
243 |
regular expression can match the empty string. This means given a |
|
244 |
regular expression it either returns true or false. The function |
|
245 |
\textit{nullable} |
|
246 |
is defined as follows: |
|
247 |
||
248 |
\begin{center} |
|
249 |
\begin{tabular}{lcl} |
|
250 |
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\ |
|
251 |
$\textit{nullable}(\ONE)$ & $\dn$ & $\textit{true}$\\ |
|
252 |
$\textit{nullable}(c)$ & $\dn$ & $\textit{false}$\\ |
|
253 |
$\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\ |
|
254 |
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\ |
|
255 |
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\ |
|
256 |
\end{tabular} |
|
257 |
\end{center}~\hfill[1 Mark] |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
258 |
|
351 | 259 |
\item[(2)] Implement a function, called \textit{der}, by recursion over |
218 | 260 |
regular expressions. It takes a character and a regular expression |
289 | 261 |
as arguments and calculates the derivative of a regular expression according |
218 | 262 |
to the rules: |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
263 |
|
218 | 264 |
\begin{center} |
265 |
\begin{tabular}{lcl} |
|
266 |
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\ |
|
267 |
$\textit{der}\;c\;(\ONE)$ & $\dn$ & $\ZERO$\\ |
|
268 |
$\textit{der}\;c\;(d)$ & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\ |
|
269 |
$\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\ |
|
270 |
$\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\ |
|
271 |
& & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\ |
|
272 |
& & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\ |
|
273 |
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\ |
|
274 |
\end{tabular} |
|
275 |
\end{center} |
|
276 |
||
277 |
For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives |
|
278 |
w.r.t.~the characters $a$, $b$ and $c$ are |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
279 |
|
218 | 280 |
\begin{center} |
281 |
\begin{tabular}{lcll} |
|
221 | 282 |
$\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & \quad($= r'$)\\ |
218 | 283 |
$\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\ |
284 |
$\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$ |
|
285 |
\end{tabular} |
|
286 |
\end{center} |
|
287 |
||
288 |
Let $r'$ stand for the first derivative, then taking the derivatives of $r'$ |
|
289 |
w.r.t.~the characters $a$, $b$ and $c$ gives |
|
290 |
||
291 |
\begin{center} |
|
292 |
\begin{tabular}{lcll} |
|
293 |
$\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\ |
|
221 | 294 |
$\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & \quad($= r''$)\\ |
218 | 295 |
$\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ |
296 |
\end{tabular} |
|
297 |
\end{center} |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
298 |
|
218 | 299 |
One more example: Let $r''$ stand for the second derivative above, |
300 |
then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$ |
|
301 |
and $c$ gives |
|
302 |
||
303 |
\begin{center} |
|
304 |
\begin{tabular}{lcll} |
|
305 |
$\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\ |
|
306 |
$\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\ |
|
307 |
$\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ & |
|
308 |
(is $\textit{nullable}$) |
|
309 |
\end{tabular} |
|
310 |
\end{center} |
|
311 |
||
312 |
Note, the last derivative can match the empty string, that is it is \textit{nullable}.\\ |
|
313 |
\mbox{}\hfill\mbox{[1 Mark]} |
|
314 |
||
351 | 315 |
\item[(3)] Implement the function \textit{simp}, which recursively |
224 | 316 |
traverses a regular expression, and on the way up simplifies every |
317 |
regular expression on the left (see below) to the regular expression |
|
318 |
on the right, except it does not simplify inside ${}^*$-regular |
|
319 |
expressions. |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
320 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
321 |
\begin{center} |
218 | 322 |
\begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll} |
323 |
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ |
|
324 |
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ |
|
325 |
$r \cdot \ONE$ & $\mapsto$ & $r$\\ |
|
326 |
$\ONE \cdot r$ & $\mapsto$ & $r$\\ |
|
327 |
$r + \ZERO$ & $\mapsto$ & $r$\\ |
|
328 |
$\ZERO + r$ & $\mapsto$ & $r$\\ |
|
329 |
$r + r$ & $\mapsto$ & $r$\\ |
|
330 |
\end{tabular} |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
331 |
\end{center} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
332 |
|
218 | 333 |
For example the regular expression |
334 |
\[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\] |
|
335 |
||
336 |
simplifies to just $r_1$. \textbf{Hint:} Regular expressions can be |
|
337 |
seen as trees and there are several methods for traversing |
|
245 | 338 |
trees. One of them corresponds to the inside-out traversal, which is also |
339 |
sometimes called post-order tra\-versal: you traverse inside the |
|
224 | 340 |
tree and on the way up you apply simplification rules. |
245 | 341 |
\textbf{Another Hint:} |
342 |
Remember numerical expressions from school times---there you had expressions |
|
218 | 343 |
like $u + \ldots + (1 \cdot x) - \ldots (z + (y \cdot 0)) \ldots$ |
344 |
and simplification rules that looked very similar to rules |
|
345 |
above. You would simplify such numerical expressions by replacing |
|
346 |
for example the $y \cdot 0$ by $0$, or $1\cdot x$ by $x$, and then |
|
347 |
look whether more rules are applicable. If you organise the |
|
348 |
simplification in an inside-out fashion, it is always clear which |
|
224 | 349 |
simplification should be applied next.\hfill[1 Mark] |
218 | 350 |
|
351 | 351 |
\item[(4)] Implement two functions: The first, called \textit{ders}, |
218 | 352 |
takes a list of characters and a regular expression as arguments, and |
353 |
builds the derivative w.r.t.~the list as follows: |
|
354 |
||
355 |
\begin{center} |
|
356 |
\begin{tabular}{lcl} |
|
357 |
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\ |
|
358 |
$\textit{ders}\;(c::cs)\;r$ & $\dn$ & |
|
359 |
$\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\ |
|
360 |
\end{tabular} |
|
361 |
\end{center} |
|
362 |
||
363 |
Note that this function is different from \textit{der}, which only |
|
364 |
takes a single character. |
|
365 |
||
366 |
The second function, called \textit{matcher}, takes a string and a |
|
367 |
regular expression as arguments. It builds first the derivatives |
|
368 |
according to \textit{ders} and after that tests whether the resulting |
|
369 |
derivative regular expression can match the empty string (using |
|
370 |
\textit{nullable}). For example the \textit{matcher} will produce |
|
371 |
true for the regular expression $(a\cdot b)\cdot c$ and the string |
|
372 |
$abc$, but false if you give it the string $ab$. \hfill[1 Mark] |
|
373 |
||
351 | 374 |
\item[(5)] Implement a function, called \textit{size}, by recursion |
218 | 375 |
over regular expressions. If a regular expression is seen as a tree, |
376 |
then \textit{size} should return the number of nodes in such a |
|
377 |
tree. Therefore this function is defined as follows: |
|
378 |
||
379 |
\begin{center} |
|
380 |
\begin{tabular}{lcl} |
|
381 |
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\ |
|
382 |
$\textit{size}(\ONE)$ & $\dn$ & $1$\\ |
|
383 |
$\textit{size}(c)$ & $\dn$ & $1$\\ |
|
384 |
$\textit{size}(r_1 + r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\ |
|
385 |
$\textit{size}(r_1 \cdot r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\ |
|
386 |
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\ |
|
387 |
\end{tabular} |
|
388 |
\end{center} |
|
389 |
||
224 | 390 |
You can use \textit{size} in order to test how much the ``evil'' regular |
218 | 391 |
expression $(a^*)^* \cdot b$ grows when taking successive derivatives |
392 |
according the letter $a$ without simplification and then compare it to |
|
393 |
taking the derivative, but simplify the result. The sizes |
|
394 |
are given in \texttt{re.scala}. \hfill[1 Mark] |
|
221 | 395 |
|
351 | 396 |
\item[(6)] You do not have to implement anything specific under this |
221 | 397 |
task. The purpose here is that you will be marked for some ``power'' |
398 |
test cases. For example can your matcher decide within 30 seconds |
|
399 |
whether the regular expression $(a^*)^*\cdot b$ matches strings of the |
|
400 |
form $aaa\ldots{}aaaa$, for say 1 Million $a$'s. And does simplification |
|
401 |
simplify the regular expression |
|
402 |
||
403 |
\[ |
|
404 |
\texttt{SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)} |
|
405 |
\] |
|
406 |
||
407 |
\noindent correctly to just \texttt{ONE}, where \texttt{SEQ} is nested |
|
245 | 408 |
50 or more times?\\ |
351 | 409 |
\mbox{}\hfill[2 Mark] |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
410 |
\end{itemize} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
411 |
|
218 | 412 |
\subsection*{Background} |
413 |
||
414 |
Although easily implementable in Scala, the idea behind the derivative |
|
415 |
function might not so easy to be seen. To understand its purpose |
|
416 |
better, assume a regular expression $r$ can match strings of the form |
|
417 |
$c\!::\!cs$ (that means strings which start with a character $c$ and have |
|
418 |
some rest, or tail, $cs$). If you take the derivative of $r$ with |
|
419 |
respect to the character $c$, then you obtain a regular expression |
|
420 |
that can match all the strings $cs$. In other words, the regular |
|
421 |
expression $\textit{der}\;c\;r$ can match the same strings $c\!::\!cs$ |
|
422 |
that can be matched by $r$, except that the $c$ is chopped off. |
|
423 |
||
424 |
Assume now $r$ can match the string $abc$. If you take the derivative |
|
425 |
according to $a$ then you obtain a regular expression that can match |
|
426 |
$bc$ (it is $abc$ where the $a$ has been chopped off). If you now |
|
427 |
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you |
|
428 |
obtain a regular expression that can match the string $c$ (it is $bc$ |
|
429 |
where $b$ is chopped off). If you finally build the derivative of this |
|
430 |
according $c$, that is |
|
431 |
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain |
|
432 |
a regular expression that can match the empty string. You can test |
|
433 |
whether this is indeed the case using the function nullable, which is |
|
434 |
what your matcher is doing. |
|
435 |
||
436 |
The purpose of the $\textit{simp}$ function is to keep the regular |
|
437 |
expressions small. Normally the derivative function makes the regular |
|
221 | 438 |
expression bigger (see the SEQ case and the example in (2)) and the |
218 | 439 |
algorithm would be slower and slower over time. The $\textit{simp}$ |
440 |
function counters this increase in size and the result is that the |
|
441 |
algorithm is fast throughout. By the way, this algorithm is by Janusz |
|
442 |
Brzozowski who came up with the idea of derivatives in 1964 in his PhD |
|
443 |
thesis. |
|
444 |
||
445 |
\begin{center}\small |
|
446 |
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)} |
|
447 |
\end{center} |
|
448 |
||
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
449 |
|
218 | 450 |
If you want to see how badly the regular expression matchers do in |
221 | 451 |
Java\footnote{Version 8 and below; Version 9 and above does not seem to be as |
452 |
catastrophic, but still much worse than the regular expression |
|
453 |
matcher based on derivatives.}, JavaScript and Python with the |
|
454 |
`evil' regular expression $(a^*)^*\cdot b$, then have a look at the |
|
351 | 455 |
graphs below (you can try it out for yourself: have a look at the files |
456 |
\texttt{catastrophic9.java}, \texttt{catastrophic.js}, |
|
457 |
\texttt{catastrophic.py} etc on KEATS). Compare this with the matcher you |
|
221 | 458 |
have implemented. How long can the string of $a$'s be in your matcher |
459 |
and still stay within the 30 seconds time limit? |
|
78 | 460 |
|
218 | 461 |
\begin{center} |
462 |
\begin{tabular}{@{}cc@{}} |
|
463 |
\multicolumn{2}{c}{Graph: $(a^*)^*\cdot b$ and strings |
|
464 |
$\underbrace{a\ldots a}_{n}$}\bigskip\\ |
|
465 |
||
466 |
\begin{tikzpicture} |
|
467 |
\begin{axis}[ |
|
468 |
xlabel={$n$}, |
|
469 |
x label style={at={(1.05,0.0)}}, |
|
470 |
ylabel={time in secs}, |
|
471 |
y label style={at={(0.06,0.5)}}, |
|
472 |
enlargelimits=false, |
|
473 |
xtick={0,5,...,30}, |
|
474 |
xmax=33, |
|
475 |
ymax=45, |
|
476 |
ytick={0,5,...,40}, |
|
477 |
scaled ticks=false, |
|
478 |
axis lines=left, |
|
479 |
width=6cm, |
|
480 |
height=5.5cm, |
|
351 | 481 |
legend entries={Python, Java 8, JavaScript, Swift, Dart}, |
222 | 482 |
legend pos=north west, |
483 |
legend cell align=left] |
|
218 | 484 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data}; |
485 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data}; |
|
221 | 486 |
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data}; |
351 | 487 |
\addplot[magenta,mark=*, mark options={fill=white}] table {re-swift.data}; |
488 |
\addplot[brown,mark=*, mark options={fill=white}] table {re-dart.data}; |
|
218 | 489 |
\end{axis} |
490 |
\end{tikzpicture} |
|
491 |
& |
|
492 |
\begin{tikzpicture} |
|
493 |
\begin{axis}[ |
|
494 |
xlabel={$n$}, |
|
495 |
x label style={at={(1.05,0.0)}}, |
|
496 |
ylabel={time in secs}, |
|
497 |
y label style={at={(0.06,0.5)}}, |
|
498 |
%enlargelimits=false, |
|
499 |
%xtick={0,5000,...,30000}, |
|
500 |
xmax=65000, |
|
501 |
ymax=45, |
|
502 |
ytick={0,5,...,40}, |
|
503 |
scaled ticks=false, |
|
504 |
axis lines=left, |
|
505 |
width=6cm, |
|
506 |
height=5.5cm, |
|
507 |
legend entries={Java 9}, |
|
508 |
legend pos=north west] |
|
509 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java9.data}; |
|
510 |
\end{axis} |
|
511 |
\end{tikzpicture} |
|
512 |
\end{tabular} |
|
513 |
\end{center} |
|
514 |
\newpage |
|
515 |
||
516 |
||
517 |
||
518 |
||
6 | 519 |
|
520 |
\end{document} |
|
521 |
||
68 | 522 |
|
6 | 523 |
%%% Local Variables: |
524 |
%%% mode: latex |
|
525 |
%%% TeX-master: t |
|
526 |
%%% End: |