author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 14 Nov 2022 12:04:21 +0000 | |
changeset 444 | 7a0735db4788 |
parent 428 | cdfa6a293453 |
child 475 | 59e005dcf163 |
permissions | -rw-r--r-- |
257 | 1 |
% !TEX program = xelatex |
6 | 2 |
\documentclass{article} |
426 | 3 |
\usepackage{../styles/style} |
4 |
\usepackage{../styles/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 |
||
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
122 |
\section*{Main Part 3 (Scala, 7 Marks)} |
6 | 123 |
|
425 | 124 |
\mbox{}\hfill\textit{``Java is the most distressing thing to happen to computing since MS-DOS.''}\smallskip\\ |
125 |
\mbox{}\hfill\textit{ --- Alan Kay, the inventor of object-oriented programming}\bigskip\medskip |
|
275 | 126 |
|
127 |
\noindent |
|
351 | 128 |
This part is about a regular expression matcher described by |
415 | 129 |
Brzozowski in 1964. The |
351 | 130 |
background is that ``out-of-the-box'' regular expression matching in |
131 |
mainstream languages like Java, JavaScript and Python can sometimes be |
|
132 |
excruciatingly slow. You are supposed to implement a regular |
|
133 |
expression matcher that is much, much faster. \bigskip |
|
218 | 134 |
|
351 | 135 |
\IMPORTANTNONE{} |
62 | 136 |
|
137 |
\noindent |
|
218 | 138 |
Also note that the running time of each part will be restricted to a |
257 | 139 |
maximum of 30 seconds on my laptop. |
218 | 140 |
|
141 |
\DISCLAIMER{} |
|
86 | 142 |
|
221 | 143 |
\subsection*{Reference Implementation} |
144 |
||
351 | 145 |
This Scala assignment comes with a reference implementation in form of |
146 |
a \texttt{jar}-file. This allows you to run any test cases on your own |
|
221 | 147 |
computer. For example you can call Scala on the command line with the |
148 |
option \texttt{-cp re.jar} and then query any function from the |
|
351 | 149 |
\texttt{re.scala} template file. As usual you have to prefix the calls |
396 | 150 |
with \texttt{M3} or import this object. Since some tasks |
351 | 151 |
are time sensitive, you can check the reference implementation as |
152 |
follows: if you want to know, for example, how long it takes to match |
|
153 |
strings of $a$'s using the regular expression $(a^*)^*\cdot b$ you can |
|
154 |
query as follows: |
|
221 | 155 |
|
156 |
||
245 | 157 |
\begin{lstlisting}[xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small] |
221 | 158 |
$ scala -cp re.jar |
396 | 159 |
scala> import M3._ |
221 | 160 |
scala> for (i <- 0 to 5000000 by 500000) { |
292 | 161 |
| println(f"$i: ${time_needed(2, matcher(EVIL, "a" * i))}%.5f secs.") |
221 | 162 |
| } |
292 | 163 |
0: 0.00002 secs. |
164 |
500000: 0.10608 secs. |
|
165 |
1000000: 0.22286 secs. |
|
166 |
1500000: 0.35982 secs. |
|
167 |
2000000: 0.45828 secs. |
|
168 |
2500000: 0.59558 secs. |
|
169 |
3000000: 0.73191 secs. |
|
170 |
3500000: 0.83499 secs. |
|
171 |
4000000: 0.99149 secs. |
|
172 |
4500000: 1.15395 secs. |
|
173 |
5000000: 1.29659 secs. |
|
221 | 174 |
\end{lstlisting}%$ |
175 |
||
424 | 176 |
|
351 | 177 |
\subsection*{Preliminaries} |
218 | 178 |
|
179 |
The task is to implement a regular expression matcher that is based on |
|
180 |
derivatives of regular expressions. Most of the functions are defined by |
|
181 |
recursion over regular expressions and can be elegantly implemented |
|
182 |
using Scala's pattern-matching. The implementation should deal with the |
|
183 |
following regular expressions, which have been predefined in the file |
|
184 |
\texttt{re.scala}: |
|
6 | 185 |
|
218 | 186 |
\begin{center} |
187 |
\begin{tabular}{lcll} |
|
188 |
$r$ & $::=$ & $\ZERO$ & cannot match anything\\ |
|
189 |
& $|$ & $\ONE$ & can only match the empty string\\ |
|
190 |
& $|$ & $c$ & can match a single character (in this case $c$)\\ |
|
191 |
& $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\ |
|
192 |
& $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\ |
|
193 |
& & & then the second part with $r_2$\\ |
|
221 | 194 |
& $|$ & $r^*$ & can match a string with zero or more copies of $r$\\ |
218 | 195 |
\end{tabular} |
196 |
\end{center} |
|
6 | 197 |
|
218 | 198 |
\noindent |
221 | 199 |
Why? Regular expressions are |
200 |
one of the simplest ways to match patterns in text, and |
|
218 | 201 |
are endlessly useful for searching, editing and analysing data in all |
202 |
sorts of places (for example analysing network traffic in order to |
|
203 |
detect security breaches). However, you need to be fast, otherwise you |
|
204 |
will stumble over problems such as recently reported at |
|
205 |
||
206 |
{\small |
|
207 |
\begin{itemize} |
|
289 | 208 |
\item[$\bullet$] \url{https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019} |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
209 |
\item[$\bullet$] \texttt{\href{https://web.archive.org/web/20160801163029/https://www.stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}{https://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}} |
218 | 210 |
\item[$\bullet$] \url{https://vimeo.com/112065252} |
289 | 211 |
\item[$\bullet$] \url{https://davidvgalbraith.com/how-i-fixed-atom} |
218 | 212 |
\end{itemize}} |
213 |
||
221 | 214 |
% Knowing how to match regular expressions and strings will let you |
215 |
% solve a lot of problems that vex other humans. |
|
216 |
||
217 |
||
218 | 218 |
\subsubsection*{Tasks (file re.scala)} |
6 | 219 |
|
218 | 220 |
The file \texttt{re.scala} has already a definition for regular |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
221 |
expressions and also defines some handy shorthand notation for regular |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
222 |
expressions. The notation in this coursework description matches up |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
223 |
with the code as follows: |
218 | 224 |
|
225 |
\begin{center} |
|
226 |
\begin{tabular}{rcl@{\hspace{10mm}}l} |
|
227 |
& & code: & shorthand:\smallskip \\ |
|
228 |
$\ZERO$ & $\mapsto$ & \texttt{ZERO}\\ |
|
229 |
$\ONE$ & $\mapsto$ & \texttt{ONE}\\ |
|
230 |
$c$ & $\mapsto$ & \texttt{CHAR(c)}\\ |
|
396 | 231 |
$\sum rs$ & $\mapsto$ & \texttt{ALTs(rs)}\\ |
218 | 232 |
$r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)} & \texttt{r1 | r2}\\ |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
233 |
$\prod rs$ & $\mapsto$ & \texttt{SEQs(rs)}\\ |
218 | 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 |
||
396 | 239 |
\noindent |
444 | 240 |
The alternative regular expression comes in two versions: one is |
396 | 241 |
binary (+ / \texttt{ALT}) and the other is n-ary ($\sum$ / |
242 |
\texttt{ALTs}). The latter takes a list of regular expressions as |
|
424 | 243 |
argument. In what follows we shall use $rs$ to stand for lists of |
244 |
regular expressions. When the list is empty, we shall write $\sum\,[]$; |
|
245 |
if it is non-empty, we sometimes write $\sum\,[r_1,..., r_n]$. |
|
246 |
The binary alternative can be seen as an abbreviation, |
|
396 | 247 |
that is $r_1 + r_2 \dn \sum\,[r_1, r_2]$. As a result we can ignore the |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
248 |
binary version and only implement the n-ary alternative. Similarly |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
249 |
the sequence regular expression is only implemented with lists and the |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
250 |
binary version can be obtained by defining $r_1 \cdot r_2 \dn \prod\,[r_1, r_2]$. |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
251 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
252 |
\begin{itemize} |
351 | 253 |
\item[(1)] Implement a function, called \textit{nullable}, by |
218 | 254 |
recursion over regular expressions. This function tests whether a |
255 |
regular expression can match the empty string. This means given a |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
256 |
regular expression, it either returns true or false. The function |
218 | 257 |
\textit{nullable} |
258 |
is defined as follows: |
|
259 |
||
260 |
\begin{center} |
|
261 |
\begin{tabular}{lcl} |
|
262 |
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\ |
|
263 |
$\textit{nullable}(\ONE)$ & $\dn$ & $\textit{true}$\\ |
|
264 |
$\textit{nullable}(c)$ & $\dn$ & $\textit{false}$\\ |
|
396 | 265 |
$\textit{nullable}(\sum rs)$ & $\dn$ & $\exists r \in rs.\;\textit{nullable}(r)$\\ |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
266 |
$\textit{nullable}(\prod rs)$ & $\dn$ & $\forall r\in rs.\;\textit{nullable}(r)$\\ |
218 | 267 |
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\ |
268 |
\end{tabular} |
|
396 | 269 |
\end{center}~\hfill[0.5 Marks] |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
270 |
|
351 | 271 |
\item[(2)] Implement a function, called \textit{der}, by recursion over |
218 | 272 |
regular expressions. It takes a character and a regular expression |
424 | 273 |
as arguments and calculates the \emph{derivative} of a regular expression according |
218 | 274 |
to the rules: |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
275 |
|
218 | 276 |
\begin{center} |
277 |
\begin{tabular}{lcl} |
|
278 |
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\ |
|
279 |
$\textit{der}\;c\;(\ONE)$ & $\dn$ & $\ZERO$\\ |
|
280 |
$\textit{der}\;c\;(d)$ & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\ |
|
396 | 281 |
$\textit{der}\;c\;(\sum\;[r_1,..,r_n])$ & $\dn$ & $\sum\;[\textit{der}\;c\;r_1,..,\textit{der}\;c\;r_n]$\\ |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
282 |
$\textit{der}\;c\;(\prod\;[])$ & $\dn$ & $\ZERO$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
283 |
$\textit{der}\;c\;(\prod\;r\!::\!rs)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r)$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
284 |
& & $\textit{then}\;(\prod\;(\textit{der}\;c\;r)\!::\!rs) + (\textit{der}\;c\;(\prod rs))$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
285 |
& & $\textit{else}\;(\prod\;(\textit{der}\;c\;r)\!::\! rs)$\\ |
218 | 286 |
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\ |
287 |
\end{tabular} |
|
288 |
\end{center} |
|
289 |
\mbox{}\hfill\mbox{[1 Mark]} |
|
290 |
||
396 | 291 |
\item[(3)] We next want to simplify regular expressions: essentially |
292 |
we want to remove $\ZERO$ in regular expressions like $r + \ZERO$ |
|
293 |
and $\ZERO + r$. However, our n-ary alternative takes a list of |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
294 |
regular expressions as argument, and we therefore need a more general |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
295 |
``denesting'' function, which deletes $\ZERO$s and ``spills out'' nested |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
296 |
$\sum$s. This function, called \texttt{denest}, should analyse a |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
297 |
list of regular expressions, say $rs$, as follows: |
396 | 298 |
|
299 |
\begin{center} |
|
300 |
\begin{tabular}{lllll} |
|
301 |
1) &$rs = []$ & $\dn$ & $[]$ & (empty list)\\ |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
302 |
2) &$rs = \ZERO :: rest$ & $\dn$ & $\texttt{denest}\;rest$ & (throw away $\ZERO$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
303 |
3) &$rs = (\sum rs) :: rest$ & $\dn$ & $rs ::: \texttt{denest}\;rest$ & (spill out $\sum$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
304 |
4) &$rs = r :: rest$ & $\dn$ & $r :: \texttt{denest}\;rest$ & (otherwise)\\ |
396 | 305 |
\end{tabular} |
306 |
\end{center} |
|
307 |
||
424 | 308 |
The first clause states that empty lists cannot be further |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
309 |
denested. The second removes the first $\ZERO$ from the list and recurses. |
396 | 310 |
The third is when the first regular expression is an \texttt{ALTs}, then |
311 |
the content of this alternative should be spilled out and appended |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
312 |
with the denested rest of the list. The last case is for all other |
396 | 313 |
cases where the head of the list is not $\ZERO$ and not an \texttt{ALTs}, |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
314 |
then we just keep the head of the list and denest the rest.\\ |
396 | 315 |
\mbox{}\hfill\mbox{[1 Mark]} |
316 |
||
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
317 |
\item[(4)] Implement the function \texttt{flts} which flattens our |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
318 |
n-ary sequence regular expression $\prod$. Like \texttt{denest}, this |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
319 |
function is intended to delete $\ONE$s and spill out nested $\prod$s. |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
320 |
Unfortunately, there is a special case to do with $\ZERO$: If this function encounters a $\ZERO$, then |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
321 |
the whole ``product'' should be $\ZERO$. The problem is that the $\ZERO$ can be anywhere |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
322 |
inside the list. The easiest way to implement this function is therefore by using an |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
323 |
accumulator, which when called is set to \texttt{Nil}. This means \textit{flts} takes |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
324 |
two arguments (which are both lists of regular expressions) |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
325 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
326 |
\[ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
327 |
\texttt{flts}\;rs\;acc |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
328 |
\] |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
329 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
330 |
This function analyses the list $rs$ as follows |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
331 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
332 |
\begin{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
333 |
\begin{tabular}{@{}lllll@{}} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
334 |
1) &$rs = []$ & $\dn$ & $acc$ & (empty list)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
335 |
2) &$rs = \ZERO :: rest$ & $\dn$ & $[\ZERO]$ & (special case for $\ZERO$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
336 |
3) &$rs = \ONE :: rest$ & $\dn$ & $\texttt{flts}\,rest\,acc$ & (throw away $\ONE$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
337 |
4) &$rs = (\prod rs) :: rest$ & $\dn$ & $\texttt{flts}\;rest\,(acc ::: rs)$ & (spill out $\prod$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
338 |
5) &$rs = r :: rest$ & $\dn$ & $\texttt{flts}\;rest\,(acc ::: [r])$& (otherwise)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
339 |
\end{tabular} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
340 |
\end{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
341 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
342 |
In the first case we just return whatever has accumulated in |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
343 |
$acc$. In the fourth case we spill out the $rs$ by appending the |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
344 |
$rs$ to the end of the accumulator. Similarly in the last case we |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
345 |
append the single regular expression $r$ to the end of the |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
346 |
accumulator. I let you think why the ``end'' is needed. \mbox{}\hfill\mbox{[1 Mark]} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
347 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
348 |
\item[(5)] Before we can simplify regular expressions, we need what is often called |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
349 |
\emph{smart constructors} for $\sum$ and $\prod$. While the ``normal'' constructors |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
350 |
\texttt{ALTs} and \texttt{SEQs} give us alternatives and sequences, respectively, \emph{smart} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
351 |
constructors might return something different depending on what list of regular expressions |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
352 |
they are given as argument. |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
353 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
354 |
\begin{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
355 |
\begin{tabular}{@{}c@{\hspace{9mm}}c@{}} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
356 |
\begin{tabular}{l@{\hspace{2mm}}l@{\hspace{1mm}}ll} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
357 |
& $\sum^{smart}$\smallskip\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
358 |
1) & $rs = []$ & $\dn$ & $\ZERO$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
359 |
2) & $rs = [r]$ & $\dn$ & $r$\\ \\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
360 |
3) & otherwise & $\dn$ & $\sum\,rs$ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
361 |
\end{tabular} & |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
362 |
\begin{tabular}{l@{\hspace{2mm}}l@{\hspace{1mm}}ll} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
363 |
& $\prod^{smart}$\smallskip\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
364 |
1) & $rs = []$ & $\dn$ & $\ONE$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
365 |
2a) & $rs = [\ZERO]$ & $\dn$ & $\ZERO$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
366 |
2b) & $rs = [r]$ & $\dn$ & $r$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
367 |
3) & otherwise & $\dn$ & $\prod\,rs$ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
368 |
\end{tabular} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
369 |
\end{tabular} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
370 |
\end{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
371 |
\mbox{}\hfill\mbox{[0.5 Marks]} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
372 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
373 |
\item[(6)] Implement the function \textit{simp}, which recursively |
224 | 374 |
traverses a regular expression, and on the way up simplifies every |
375 |
regular expression on the left (see below) to the regular expression |
|
376 |
on the right, except it does not simplify inside ${}^*$-regular |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
377 |
expressions and also does not simplify $\ZERO$, $\ONE$ and characters. |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
378 |
|
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
379 |
\begin{center} |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
380 |
\begin{tabular}{@{}l@{\hspace{3mm}}c@{\hspace{3mm}}ll@{}} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
381 |
LHS: & & RHS:\smallskip\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
382 |
$\sum\;[r_1,..,r_n]$ & $\mapsto$ & $\sum^{smart}\;(\texttt{(denest + distinct)}[simp(r_1),..,simp(r_n)])$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
383 |
$\prod\;[r_1,..,r_n]$ & $\mapsto$ & $\prod^{smart}\;(\texttt{(flts)}[simp(r_1),..,simp(r_n)])$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
384 |
$r$ & $\mapsto$ & $r$ \quad (all other cases) |
218 | 385 |
\end{tabular} |
396 | 386 |
\end{center} |
387 |
||
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
388 |
The first case is as follows: first apply $simp$ to all regular |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
389 |
expressions $r_1,.. ,r_n$; then denest the resulting list using |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
390 |
\texttt{denest}; after that remove all duplicates in this list (this can be |
418 | 391 |
done in Scala using the function |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
392 |
\texttt{\_.distinct}). Finally, you end up with a list of (simplified) |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
393 |
regular expressions; apply the smart constructor $\sum^{smart}$ to this list. |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
394 |
Similarly in the $\prod$ case: simplify first all regular |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
395 |
expressions $r_1,.. ,r_n$; then flatten the resulting list using \texttt{flts} and apply the |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
396 |
smart constructor $\prod^{smart}$ to the result. In all other cases, just return the |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
397 |
input $r$ as is. |
418 | 398 |
|
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
399 |
|
218 | 400 |
For example the regular expression |
401 |
\[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\] |
|
402 |
||
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
403 |
simplifies to just $r_1$. \mbox{}\hfill\mbox{[1 Mark]} |
218 | 404 |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
405 |
\item[(7)] Implement two functions: The first, called \textit{ders}, |
218 | 406 |
takes a list of characters and a regular expression as arguments, and |
407 |
builds the derivative w.r.t.~the list as follows: |
|
408 |
||
409 |
\begin{center} |
|
410 |
\begin{tabular}{lcl} |
|
411 |
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\ |
|
412 |
$\textit{ders}\;(c::cs)\;r$ & $\dn$ & |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
413 |
$\textit{ders}\;cs\;(\textit{simp}\,(\textit{der}\;c\;r))$\\ |
218 | 414 |
\end{tabular} |
415 |
\end{center} |
|
416 |
||
417 |
Note that this function is different from \textit{der}, which only |
|
418 |
takes a single character. |
|
419 |
||
420 |
The second function, called \textit{matcher}, takes a string and a |
|
421 |
regular expression as arguments. It builds first the derivatives |
|
422 |
according to \textit{ders} and after that tests whether the resulting |
|
423 |
derivative regular expression can match the empty string (using |
|
424 |
\textit{nullable}). For example the \textit{matcher} will produce |
|
425 |
true for the regular expression $(a\cdot b)\cdot c$ and the string |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
426 |
$abc$, but false if you give it the string $ab$. \hfill[0.5 Mark] |
218 | 427 |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
428 |
\item[(8)] Implement a function, called \textit{size}, by recursion |
218 | 429 |
over regular expressions. If a regular expression is seen as a tree, |
430 |
then \textit{size} should return the number of nodes in such a |
|
431 |
tree. Therefore this function is defined as follows: |
|
432 |
||
433 |
\begin{center} |
|
434 |
\begin{tabular}{lcl} |
|
435 |
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\ |
|
436 |
$\textit{size}(\ONE)$ & $\dn$ & $1$\\ |
|
437 |
$\textit{size}(c)$ & $\dn$ & $1$\\ |
|
396 | 438 |
$\textit{size}(\sum\,[r_1,..,r_n]$ & $\dn$ & $1 + \textit{size}(r_1) + ... + \textit{size}(r_n)$\\ |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
439 |
$\textit{size}(\prod\,[r_1,..,r_n]$ & $\dn$ & $1 + \textit{size}(r_1) + ... + \textit{size}(r_n)$\\ |
218 | 440 |
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\ |
441 |
\end{tabular} |
|
442 |
\end{center} |
|
443 |
||
224 | 444 |
You can use \textit{size} in order to test how much the ``evil'' regular |
218 | 445 |
expression $(a^*)^* \cdot b$ grows when taking successive derivatives |
446 |
according the letter $a$ without simplification and then compare it to |
|
447 |
taking the derivative, but simplify the result. The sizes |
|
396 | 448 |
are given in \texttt{re.scala}. \hfill[0.5 Marks] |
221 | 449 |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
450 |
\item[(9)] You do not have to implement anything specific under this |
221 | 451 |
task. The purpose here is that you will be marked for some ``power'' |
452 |
test cases. For example can your matcher decide within 30 seconds |
|
453 |
whether the regular expression $(a^*)^*\cdot b$ matches strings of the |
|
454 |
form $aaa\ldots{}aaaa$, for say 1 Million $a$'s. And does simplification |
|
455 |
simplify the regular expression |
|
456 |
||
457 |
\[ |
|
458 |
\texttt{SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)} |
|
459 |
\] |
|
460 |
||
461 |
\noindent correctly to just \texttt{ONE}, where \texttt{SEQ} is nested |
|
245 | 462 |
50 or more times?\\ |
396 | 463 |
\mbox{}\hfill[1 Mark] |
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
464 |
\end{itemize} |
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
465 |
|
218 | 466 |
\subsection*{Background} |
467 |
||
396 | 468 |
Although easily implementable in Scala (ok maybe the \texttt{simp} functions and |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
469 |
the constructors \texttt{ALTs}/\texttt{SEQs} needs a bit more thinking), the idea behind the |
396 | 470 |
derivative function might not so easy to be seen. To understand its |
471 |
purpose better, assume a regular expression $r$ can match strings of |
|
472 |
the form $c\!::\!cs$ (that means strings which start with a character |
|
473 |
$c$ and have some rest, or tail, $cs$). If you take the derivative of |
|
474 |
$r$ with respect to the character $c$, then you obtain a regular |
|
475 |
expression that can match all the strings $cs$. In other words, the |
|
476 |
regular expression $\textit{der}\;c\;r$ can match the same strings |
|
477 |
$c\!::\!cs$ that can be matched by $r$, except that the $c$ is chopped |
|
478 |
off. |
|
218 | 479 |
|
480 |
Assume now $r$ can match the string $abc$. If you take the derivative |
|
481 |
according to $a$ then you obtain a regular expression that can match |
|
482 |
$bc$ (it is $abc$ where the $a$ has been chopped off). If you now |
|
483 |
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you |
|
484 |
obtain a regular expression that can match the string $c$ (it is $bc$ |
|
485 |
where $b$ is chopped off). If you finally build the derivative of this |
|
486 |
according $c$, that is |
|
487 |
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain |
|
488 |
a regular expression that can match the empty string. You can test |
|
489 |
whether this is indeed the case using the function nullable, which is |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
490 |
what the matcher you have implemented is doing. |
218 | 491 |
|
492 |
The purpose of the $\textit{simp}$ function is to keep the regular |
|
493 |
expressions small. Normally the derivative function makes the regular |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
494 |
expression bigger (see the \texttt{SEQs} case and the example in Task (2)) and the |
218 | 495 |
algorithm would be slower and slower over time. The $\textit{simp}$ |
496 |
function counters this increase in size and the result is that the |
|
497 |
algorithm is fast throughout. By the way, this algorithm is by Janusz |
|
498 |
Brzozowski who came up with the idea of derivatives in 1964 in his PhD |
|
499 |
thesis. |
|
500 |
||
501 |
\begin{center}\small |
|
502 |
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)} |
|
503 |
\end{center} |
|
504 |
||
105
67ce930b5935
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
100
diff
changeset
|
505 |
|
218 | 506 |
If you want to see how badly the regular expression matchers do in |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
507 |
Java\footnote{Version 8 and below; Version 9 and above does not seem |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
508 |
to be as catastrophic, but still much worse than the regular |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
509 |
expression matcher based on derivatives. BTW, Scala uses the regular |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
510 |
expression matcher provided by the Java libraries. So is just as bad.}, JavaScript, |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
511 |
Python Swift and Dart with the ``evil'' regular expression |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
512 |
$(a^*)^*\cdot b$, then have a look at the graphs below (you can try it |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
513 |
out for yourself: have a look at the files |
351 | 514 |
\texttt{catastrophic9.java}, \texttt{catastrophic.js}, |
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
515 |
\texttt{catastrophic.py} etc on KEATS). Compare this with the matcher |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
516 |
you have implemented. How long can a string of $a$'s be in your |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
517 |
matcher and still stay within the 30 seconds time limit? It should be |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
518 |
mu(uu)$^*$ch better than your off-the-shelf matcher in your |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
519 |
bog-standard language. |
78 | 520 |
|
218 | 521 |
\begin{center} |
522 |
\begin{tabular}{@{}cc@{}} |
|
523 |
\multicolumn{2}{c}{Graph: $(a^*)^*\cdot b$ and strings |
|
424 | 524 |
$\underbrace{a\ldots a}_{n}$}\medskip\\ |
218 | 525 |
|
526 |
\begin{tikzpicture} |
|
527 |
\begin{axis}[ |
|
528 |
xlabel={$n$}, |
|
529 |
x label style={at={(1.05,0.0)}}, |
|
530 |
ylabel={time in secs}, |
|
531 |
y label style={at={(0.06,0.5)}}, |
|
532 |
enlargelimits=false, |
|
533 |
xtick={0,5,...,30}, |
|
534 |
xmax=33, |
|
535 |
ymax=45, |
|
536 |
ytick={0,5,...,40}, |
|
537 |
scaled ticks=false, |
|
538 |
axis lines=left, |
|
539 |
width=6cm, |
|
424 | 540 |
height=5.5cm, |
351 | 541 |
legend entries={Python, Java 8, JavaScript, Swift, Dart}, |
222 | 542 |
legend pos=north west, |
543 |
legend cell align=left] |
|
218 | 544 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data}; |
545 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data}; |
|
221 | 546 |
\addplot[red,mark=*, mark options={fill=white}] table {re-js.data}; |
351 | 547 |
\addplot[magenta,mark=*, mark options={fill=white}] table {re-swift.data}; |
548 |
\addplot[brown,mark=*, mark options={fill=white}] table {re-dart.data}; |
|
218 | 549 |
\end{axis} |
550 |
\end{tikzpicture} |
|
551 |
& |
|
552 |
\begin{tikzpicture} |
|
553 |
\begin{axis}[ |
|
554 |
xlabel={$n$}, |
|
555 |
x label style={at={(1.05,0.0)}}, |
|
556 |
ylabel={time in secs}, |
|
557 |
y label style={at={(0.06,0.5)}}, |
|
558 |
%enlargelimits=false, |
|
559 |
%xtick={0,5000,...,30000}, |
|
560 |
xmax=65000, |
|
561 |
ymax=45, |
|
562 |
ytick={0,5,...,40}, |
|
563 |
scaled ticks=false, |
|
564 |
axis lines=left, |
|
565 |
width=6cm, |
|
424 | 566 |
height=5.5cm, |
218 | 567 |
legend entries={Java 9}, |
568 |
legend pos=north west] |
|
569 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java9.data}; |
|
570 |
\end{axis} |
|
571 |
\end{tikzpicture} |
|
572 |
\end{tabular} |
|
573 |
\end{center} |
|
574 |
\newpage |
|
575 |
||
576 |
||
577 |
||
578 |
||
6 | 579 |
|
580 |
\end{document} |
|
581 |
||
68 | 582 |
|
428
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
583 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
584 |
For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
585 |
w.r.t.~the characters $a$, $b$ and $c$ are |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
586 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
587 |
\begin{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
588 |
\begin{tabular}{lcll} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
589 |
$\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & \quad($= r'$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
590 |
$\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
591 |
$\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
592 |
\end{tabular} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
593 |
\end{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
594 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
595 |
Let $r'$ stand for the first derivative, then taking the derivatives of $r'$ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
596 |
w.r.t.~the characters $a$, $b$ and $c$ gives |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
597 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
598 |
\begin{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
599 |
\begin{tabular}{lcll} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
600 |
$\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
601 |
$\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & \quad($= r''$)\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
602 |
$\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
603 |
\end{tabular} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
604 |
\end{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
605 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
606 |
One more example: Let $r''$ stand for the second derivative above, |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
607 |
then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
608 |
and $c$ gives |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
609 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
610 |
\begin{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
611 |
\begin{tabular}{lcll} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
612 |
$\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
613 |
$\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\ |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
614 |
$\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ & |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
615 |
(is $\textit{nullable}$) |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
616 |
\end{tabular} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
617 |
\end{center} |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
618 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
619 |
Note, the last derivative can match the empty string, that is it is \textit{nullable}. |
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
620 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
621 |
|
cdfa6a293453
updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents:
426
diff
changeset
|
622 |
|
6 | 623 |
%%% Local Variables: |
624 |
%%% mode: latex |
|
625 |
%%% TeX-master: t |
|
626 |
%%% End: |