| author | Christian Urban <urbanc@in.tum.de> | 
| Sun, 09 Dec 2018 01:36:49 +0000 | |
| changeset 243 | 80102fba0a93 | 
| parent 224 | 1a45c9bcc77b | 
| child 245 | 744496a2bade | 
| permissions | -rw-r--r-- | 
| 6 | 1 | \documentclass{article}
 | 
| 62 | 2 | \usepackage{../style}
 | 
| 78 | 3 | \usepackage{../langs}
 | 
| 218 | 4 | \usepackage{disclaimer}
 | 
| 5 | \usepackage{tikz}
 | |
| 6 | \usepackage{pgf}
 | |
| 7 | \usepackage{pgfplots}
 | |
| 8 | \usepackage{stackengine}
 | |
| 9 | %% \usepackage{accents}
 | |
| 10 | \newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}}
 | |
| 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 | |
| 18 | 19 0.084 | |
| 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 | ||
| 221 | 49 | \begin{filecontents}{re-js.data}
 | 
| 50 | 5 0.061 | |
| 51 | 10 0.061 | |
| 52 | 15 0.061 | |
| 53 | 20 0.070 | |
| 54 | 23 0.131 | |
| 55 | 25 0.308 | |
| 56 | 26 0.564 | |
| 57 | 28 1.994 | |
| 58 | 30 7.648 | |
| 59 | 31 15.881 | |
| 60 | 32 32.190 | |
| 61 | \end{filecontents}
 | |
| 62 | ||
| 218 | 63 | \begin{filecontents}{re-java9.data}
 | 
| 64 | 1000 0.01410 | |
| 65 | 2000 0.04882 | |
| 66 | 3000 0.10609 | |
| 67 | 4000 0.17456 | |
| 68 | 5000 0.27530 | |
| 69 | 6000 0.41116 | |
| 70 | 7000 0.53741 | |
| 71 | 8000 0.70261 | |
| 72 | 9000 0.93981 | |
| 73 | 10000 0.97419 | |
| 74 | 11000 1.28697 | |
| 75 | 12000 1.51387 | |
| 76 | 14000 2.07079 | |
| 77 | 16000 2.69846 | |
| 78 | 20000 4.41823 | |
| 79 | 24000 6.46077 | |
| 80 | 26000 7.64373 | |
| 81 | 30000 9.99446 | |
| 82 | 34000 12.966885 | |
| 83 | 38000 16.281621 | |
| 84 | 42000 19.180228 | |
| 85 | 46000 21.984721 | |
| 86 | 50000 26.950203 | |
| 87 | 60000 43.0327746 | |
| 88 | \end{filecontents}
 | |
| 89 | ||
| 6 | 90 | |
| 91 | \begin{document}
 | |
| 92 | ||
| 218 | 93 | % BF IDE | 
| 94 | % https://www.microsoft.com/en-us/p/brainf-ck/9nblgggzhvq5 | |
| 95 | ||
| 221 | 96 | \section*{Coursework 9 (Scala)}
 | 
| 6 | 97 | |
| 221 | 98 | This coursework is worth 10\%. It is about a regular expression | 
| 99 | matcher and the shunting yard algorithm by Dijkstra. The first part is | |
| 100 | due on 6 December at 11pm; the second, more advanced part, is due on | |
| 224 | 101 | 20 December at 11pm. In the first part, you are asked to implement a | 
| 218 | 102 | regular expression matcher based on derivatives of regular | 
| 221 | 103 | expressions. The background is that regular expression matching in | 
| 104 | languages like Java, JavaScript and Python can sometimes be excruciatingly | |
| 105 | slow. The advanced part is about the shunting yard algorithm that | |
| 106 | transforms the usual infix notation of arithmetic expressions into the | |
| 107 | postfix notation, which is for example used in compilers.\bigskip | |
| 218 | 108 | |
| 109 | \IMPORTANT{}
 | |
| 62 | 110 | |
| 111 | \noindent | |
| 218 | 112 | Also note that the running time of each part will be restricted to a | 
| 113 | maximum of 30 seconds on my laptop. | |
| 114 | ||
| 115 | \DISCLAIMER{}
 | |
| 86 | 116 | |
| 221 | 117 | \subsection*{Reference Implementation}
 | 
| 118 | ||
| 119 | This Scala assignment comes with three reference implementations in form of | |
| 224 | 120 | \texttt{jar}-files you can download from KEATS. This allows you to run any
 | 
| 121 | test cases on your own | |
| 221 | 122 | computer. For example you can call Scala on the command line with the | 
| 123 | option \texttt{-cp re.jar} and then query any function from the
 | |
| 124 | \texttt{re.scala} template file. As usual you have to
 | |
| 125 | prefix the calls with \texttt{CW9a}, \texttt{CW9b} and \texttt{CW9c}.
 | |
| 126 | Since some tasks are time sensitive, you can check the reference | |
| 224 | 127 | implementation as follows: if you want to know, for example, how long it takes | 
| 221 | 128 | to match strings of $a$'s using the regular expression $(a^*)^*\cdot b$ | 
| 129 | you can querry as follows: | |
| 130 | ||
| 131 | ||
| 132 | ||
| 133 | \begin{lstlisting}[language={},xleftmargin=1mm,numbers=none,basicstyle=\ttfamily\small]
 | |
| 134 | $ scala -cp re.jar | |
| 135 | scala> import CW9a._ | |
| 136 | scala> for (i <- 0 to 5000000 by 500000) {
 | |
| 137 | | println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))) + "secs.") | |
| 138 | | } | |
| 139 | 0 0.00002 secs. | |
| 140 | 500000 0.10608 secs. | |
| 141 | 1000000 0.22286 secs. | |
| 142 | 1500000 0.35982 secs. | |
| 143 | 2000000 0.45828 secs. | |
| 144 | 2500000 0.59558 secs. | |
| 145 | 3000000 0.73191 secs. | |
| 146 | 3500000 0.83499 secs. | |
| 147 | 4000000 0.99149 secs. | |
| 148 | 4500000 1.15395 secs. | |
| 149 | 5000000 1.29659 secs. | |
| 150 | \end{lstlisting}%$
 | |
| 151 | ||
| 6 | 152 | |
| 218 | 153 | \subsection*{Part 1 (6 Marks)}
 | 
| 154 | ||
| 155 | The task is to implement a regular expression matcher that is based on | |
| 156 | derivatives of regular expressions. Most of the functions are defined by | |
| 157 | recursion over regular expressions and can be elegantly implemented | |
| 158 | using Scala's pattern-matching. The implementation should deal with the | |
| 159 | following regular expressions, which have been predefined in the file | |
| 160 | \texttt{re.scala}:
 | |
| 6 | 161 | |
| 218 | 162 | \begin{center}
 | 
| 163 | \begin{tabular}{lcll}
 | |
| 164 | $r$ & $::=$ & $\ZERO$ & cannot match anything\\ | |
| 165 | & $|$ & $\ONE$ & can only match the empty string\\ | |
| 166 | & $|$ & $c$ & can match a single character (in this case $c$)\\ | |
| 167 | & $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\ | |
| 168 | & $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\ | |
| 169 | & & & then the second part with $r_2$\\ | |
| 221 | 170 | & $|$ & $r^*$ & can match a string with zero or more copies of $r$\\ | 
| 218 | 171 | \end{tabular}
 | 
| 172 | \end{center}
 | |
| 6 | 173 | |
| 218 | 174 | \noindent | 
| 221 | 175 | Why? Regular expressions are | 
| 176 | one of the simplest ways to match patterns in text, and | |
| 218 | 177 | are endlessly useful for searching, editing and analysing data in all | 
| 178 | sorts of places (for example analysing network traffic in order to | |
| 179 | detect security breaches). However, you need to be fast, otherwise you | |
| 180 | will stumble over problems such as recently reported at | |
| 181 | ||
| 182 | {\small
 | |
| 183 | \begin{itemize}
 | |
| 184 | \item[$\bullet$] \url{http://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}
 | |
| 185 | \item[$\bullet$] \url{https://vimeo.com/112065252}
 | |
| 186 | \item[$\bullet$] \url{http://davidvgalbraith.com/how-i-fixed-atom/}  
 | |
| 187 | \end{itemize}}
 | |
| 188 | ||
| 221 | 189 | % Knowing how to match regular expressions and strings will let you | 
| 190 | % solve a lot of problems that vex other humans. | |
| 191 | ||
| 192 | ||
| 218 | 193 | \subsubsection*{Tasks (file re.scala)}
 | 
| 6 | 194 | |
| 218 | 195 | The file \texttt{re.scala} has already a definition for regular
 | 
| 196 | expressions and also defines some handy shorthand notation for | |
| 197 | regular expressions. The notation in this document matches up | |
| 198 | with the code in the file as follows: | |
| 199 | ||
| 200 | \begin{center}
 | |
| 201 |   \begin{tabular}{rcl@{\hspace{10mm}}l}
 | |
| 202 | & & code: & shorthand:\smallskip \\ | |
| 203 |   $\ZERO$ & $\mapsto$ & \texttt{ZERO}\\
 | |
| 204 |   $\ONE$  & $\mapsto$ & \texttt{ONE}\\
 | |
| 205 |   $c$     & $\mapsto$ & \texttt{CHAR(c)}\\
 | |
| 206 |   $r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)} & \texttt{r1 | r2}\\
 | |
| 207 |   $r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)} & \texttt{r1 $\sim$ r2}\\
 | |
| 208 |   $r^*$ & $\mapsto$ &  \texttt{STAR(r)} & \texttt{r.\%}
 | |
| 209 | \end{tabular}    
 | |
| 210 | \end{center}  
 | |
| 211 | ||
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 212 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 213 | \begin{itemize}
 | 
| 221 | 214 | \item[(1)] Implement a function, called \textit{nullable}, by
 | 
| 218 | 215 | recursion over regular expressions. This function tests whether a | 
| 216 | regular expression can match the empty string. This means given a | |
| 217 | regular expression it either returns true or false. The function | |
| 218 |   \textit{nullable}
 | |
| 219 | is defined as follows: | |
| 220 | ||
| 221 | \begin{center}
 | |
| 222 | \begin{tabular}{lcl}
 | |
| 223 | $\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\
 | |
| 224 | $\textit{nullable}(\ONE)$  & $\dn$ & $\textit{true}$\\
 | |
| 225 | $\textit{nullable}(c)$     & $\dn$ & $\textit{false}$\\
 | |
| 226 | $\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\
 | |
| 227 | $\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\
 | |
| 228 | $\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\
 | |
| 229 | \end{tabular}
 | |
| 230 | \end{center}~\hfill[1 Mark]
 | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 231 | |
| 221 | 232 | \item[(2)] Implement a function, called \textit{der}, by recursion over
 | 
| 218 | 233 | regular expressions. It takes a character and a regular expression | 
| 234 | as arguments and calculates the derivative regular expression according | |
| 235 | to the rules: | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 236 | |
| 218 | 237 | \begin{center}
 | 
| 238 | \begin{tabular}{lcl}
 | |
| 239 | $\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
 | |
| 240 | $\textit{der}\;c\;(\ONE)$  & $\dn$ & $\ZERO$\\
 | |
| 241 | $\textit{der}\;c\;(d)$     & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\
 | |
| 242 | $\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\
 | |
| 243 | $\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\
 | |
| 244 |       & & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\
 | |
| 245 |       & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\
 | |
| 246 | $\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
 | |
| 247 | \end{tabular}
 | |
| 248 | \end{center}
 | |
| 249 | ||
| 250 | For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives | |
| 251 | w.r.t.~the characters $a$, $b$ and $c$ are | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 252 | |
| 218 | 253 | \begin{center}
 | 
| 254 |   \begin{tabular}{lcll}
 | |
| 221 | 255 |     $\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & \quad($= r'$)\\
 | 
| 218 | 256 |     $\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\
 | 
| 257 |     $\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$
 | |
| 258 |   \end{tabular}
 | |
| 259 | \end{center}
 | |
| 260 | ||
| 261 | Let $r'$ stand for the first derivative, then taking the derivatives of $r'$ | |
| 262 | w.r.t.~the characters $a$, $b$ and $c$ gives | |
| 263 | ||
| 264 | \begin{center}
 | |
| 265 |   \begin{tabular}{lcll}
 | |
| 266 |     $\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\
 | |
| 221 | 267 |     $\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & \quad($= r''$)\\
 | 
| 218 | 268 |     $\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$
 | 
| 269 |   \end{tabular}
 | |
| 270 | \end{center}
 | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 271 | |
| 218 | 272 | One more example: Let $r''$ stand for the second derivative above, | 
| 273 | then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$ | |
| 274 | and $c$ gives | |
| 275 | ||
| 276 | \begin{center}
 | |
| 277 |   \begin{tabular}{lcll}
 | |
| 278 |     $\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\
 | |
| 279 |     $\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\
 | |
| 280 |     $\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ &
 | |
| 281 |     (is $\textit{nullable}$)                      
 | |
| 282 |   \end{tabular}
 | |
| 283 | \end{center}
 | |
| 284 | ||
| 285 | Note, the last derivative can match the empty string, that is it is \textit{nullable}.\\
 | |
| 286 | \mbox{}\hfill\mbox{[1 Mark]}
 | |
| 287 | ||
| 221 | 288 | \item[(3)] Implement the function \textit{simp}, which recursively
 | 
| 224 | 289 | traverses a regular expression, and on the way up simplifies every | 
| 290 | regular expression on the left (see below) to the regular expression | |
| 291 |   on the right, except it does not simplify inside ${}^*$-regular
 | |
| 292 | expressions. | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 293 | |
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 294 |   \begin{center}
 | 
| 218 | 295 | \begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll}
 | 
| 296 | $r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ | |
| 297 | $\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ | |
| 298 | $r \cdot \ONE$ & $\mapsto$ & $r$\\ | |
| 299 | $\ONE \cdot r$ & $\mapsto$ & $r$\\ | |
| 300 | $r + \ZERO$ & $\mapsto$ & $r$\\ | |
| 301 | $\ZERO + r$ & $\mapsto$ & $r$\\ | |
| 302 | $r + r$ & $\mapsto$ & $r$\\ | |
| 303 | \end{tabular}
 | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 304 |   \end{center}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 305 | |
| 218 | 306 | For example the regular expression | 
| 307 | \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\] | |
| 308 | ||
| 309 |   simplifies to just $r_1$. \textbf{Hint:} Regular expressions can be
 | |
| 310 | seen as trees and there are several methods for traversing | |
| 311 | trees. One of them corresponds to the inside-out traversal, which is | |
| 224 | 312 | sometimes also called post-order traversal: you traverse inside the | 
| 313 | tree and on the way up you apply simplification rules. | |
| 221 | 314 | Furthermore, | 
| 218 | 315 | remember numerical expressions from school times: there you had expressions | 
| 316 | like $u + \ldots + (1 \cdot x) - \ldots (z + (y \cdot 0)) \ldots$ | |
| 317 | and simplification rules that looked very similar to rules | |
| 318 | above. You would simplify such numerical expressions by replacing | |
| 319 | for example the $y \cdot 0$ by $0$, or $1\cdot x$ by $x$, and then | |
| 320 | look whether more rules are applicable. If you organise the | |
| 321 | simplification in an inside-out fashion, it is always clear which | |
| 224 | 322 | simplification should be applied next.\hfill[1 Mark] | 
| 218 | 323 | |
| 221 | 324 | \item[(4)] Implement two functions: The first, called \textit{ders},
 | 
| 218 | 325 | takes a list of characters and a regular expression as arguments, and | 
| 326 | builds the derivative w.r.t.~the list as follows: | |
| 327 | ||
| 328 | \begin{center}
 | |
| 329 | \begin{tabular}{lcl}
 | |
| 330 | $\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\
 | |
| 331 |   $\textit{ders}\;(c::cs)\;r$  & $\dn$ &
 | |
| 332 |     $\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\
 | |
| 333 | \end{tabular}
 | |
| 334 | \end{center}
 | |
| 335 | ||
| 336 | Note that this function is different from \textit{der}, which only
 | |
| 337 | takes a single character. | |
| 338 | ||
| 339 | The second function, called \textit{matcher}, takes a string and a
 | |
| 340 | regular expression as arguments. It builds first the derivatives | |
| 341 | according to \textit{ders} and after that tests whether the resulting
 | |
| 342 | derivative regular expression can match the empty string (using | |
| 343 | \textit{nullable}).  For example the \textit{matcher} will produce
 | |
| 344 | true for the regular expression $(a\cdot b)\cdot c$ and the string | |
| 345 | $abc$, but false if you give it the string $ab$. \hfill[1 Mark] | |
| 346 | ||
| 221 | 347 | \item[(5)] Implement a function, called \textit{size}, by recursion
 | 
| 218 | 348 | over regular expressions. If a regular expression is seen as a tree, | 
| 349 |   then \textit{size} should return the number of nodes in such a
 | |
| 350 | tree. Therefore this function is defined as follows: | |
| 351 | ||
| 352 | \begin{center}
 | |
| 353 | \begin{tabular}{lcl}
 | |
| 354 | $\textit{size}(\ZERO)$ & $\dn$ & $1$\\
 | |
| 355 | $\textit{size}(\ONE)$  & $\dn$ & $1$\\
 | |
| 356 | $\textit{size}(c)$     & $\dn$ & $1$\\
 | |
| 357 | $\textit{size}(r_1 + r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\
 | |
| 358 | $\textit{size}(r_1 \cdot r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\
 | |
| 359 | $\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\
 | |
| 360 | \end{tabular}
 | |
| 361 | \end{center}
 | |
| 362 | ||
| 224 | 363 | You can use \textit{size} in order to test how much the ``evil'' regular
 | 
| 218 | 364 | expression $(a^*)^* \cdot b$ grows when taking successive derivatives | 
| 365 | according the letter $a$ without simplification and then compare it to | |
| 366 | taking the derivative, but simplify the result. The sizes | |
| 367 | are given in \texttt{re.scala}. \hfill[1 Mark]
 | |
| 221 | 368 | |
| 369 | \item[(6)] You do not have to implement anything specific under this | |
| 370 | task. The purpose here is that you will be marked for some ``power'' | |
| 371 | test cases. For example can your matcher decide within 30 seconds | |
| 372 | whether the regular expression $(a^*)^*\cdot b$ matches strings of the | |
| 373 |   form $aaa\ldots{}aaaa$, for say 1 Million $a$'s. And does simplification
 | |
| 374 | simplify the regular expression | |
| 375 | ||
| 376 | \[ | |
| 377 |   \texttt{SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)}
 | |
| 378 | \] | |
| 379 | ||
| 380 |   \noindent correctly to just \texttt{ONE}, where \texttt{SEQ} is nested
 | |
| 381 | 50 or more times.\\ | |
| 382 |   \mbox{}\hfill[1 Mark]
 | |
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 383 | \end{itemize}
 | 
| 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 384 | |
| 218 | 385 | \subsection*{Background}
 | 
| 386 | ||
| 387 | Although easily implementable in Scala, the idea behind the derivative | |
| 388 | function might not so easy to be seen. To understand its purpose | |
| 389 | better, assume a regular expression $r$ can match strings of the form | |
| 390 | $c\!::\!cs$ (that means strings which start with a character $c$ and have | |
| 391 | some rest, or tail, $cs$). If you take the derivative of $r$ with | |
| 392 | respect to the character $c$, then you obtain a regular expression | |
| 393 | that can match all the strings $cs$. In other words, the regular | |
| 394 | expression $\textit{der}\;c\;r$ can match the same strings $c\!::\!cs$
 | |
| 395 | that can be matched by $r$, except that the $c$ is chopped off. | |
| 396 | ||
| 397 | Assume now $r$ can match the string $abc$. If you take the derivative | |
| 398 | according to $a$ then you obtain a regular expression that can match | |
| 399 | $bc$ (it is $abc$ where the $a$ has been chopped off). If you now | |
| 400 | build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you
 | |
| 401 | obtain a regular expression that can match the string $c$ (it is $bc$ | |
| 402 | where $b$ is chopped off). If you finally build the derivative of this | |
| 403 | according $c$, that is | |
| 404 | $\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain
 | |
| 405 | a regular expression that can match the empty string. You can test | |
| 406 | whether this is indeed the case using the function nullable, which is | |
| 407 | what your matcher is doing. | |
| 408 | ||
| 409 | The purpose of the $\textit{simp}$ function is to keep the regular
 | |
| 410 | expressions small. Normally the derivative function makes the regular | |
| 221 | 411 | expression bigger (see the SEQ case and the example in (2)) and the | 
| 218 | 412 | algorithm would be slower and slower over time. The $\textit{simp}$
 | 
| 413 | function counters this increase in size and the result is that the | |
| 414 | algorithm is fast throughout. By the way, this algorithm is by Janusz | |
| 415 | Brzozowski who came up with the idea of derivatives in 1964 in his PhD | |
| 416 | thesis. | |
| 417 | ||
| 418 | \begin{center}\small
 | |
| 419 | \url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)}
 | |
| 420 | \end{center}
 | |
| 421 | ||
| 105 
0f9f774c7697
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
100diff
changeset | 422 | |
| 218 | 423 | If you want to see how badly the regular expression matchers do in | 
| 221 | 424 | Java\footnote{Version 8 and below; Version 9 and above does not seem to be as
 | 
| 425 | catastrophic, but still much worse than the regular expression | |
| 426 | matcher based on derivatives.}, JavaScript and Python with the | |
| 427 | `evil' regular expression $(a^*)^*\cdot b$, then have a look at the | |
| 428 | graphs below (you can try it out for yourself: have a look at the file | |
| 429 | \texttt{catastrophic9.java}, \texttt{catastrophic.js} and
 | |
| 430 | \texttt{catastrophic.py} on KEATS). Compare this with the matcher you
 | |
| 431 | have implemented. How long can the string of $a$'s be in your matcher | |
| 432 | and still stay within the 30 seconds time limit? | |
| 78 | 433 | |
| 218 | 434 | \begin{center}
 | 
| 435 | \begin{tabular}{@{}cc@{}}
 | |
| 436 | \multicolumn{2}{c}{Graph: $(a^*)^*\cdot b$ and strings 
 | |
| 437 |            $\underbrace{a\ldots a}_{n}$}\bigskip\\
 | |
| 438 | ||
| 439 | \begin{tikzpicture}
 | |
| 440 | \begin{axis}[
 | |
| 441 |     xlabel={$n$},
 | |
| 442 |     x label style={at={(1.05,0.0)}},
 | |
| 443 |     ylabel={time in secs},
 | |
| 444 |     y label style={at={(0.06,0.5)}},
 | |
| 445 | enlargelimits=false, | |
| 446 |     xtick={0,5,...,30},
 | |
| 447 | xmax=33, | |
| 448 | ymax=45, | |
| 449 |     ytick={0,5,...,40},
 | |
| 450 | scaled ticks=false, | |
| 451 | axis lines=left, | |
| 452 | width=6cm, | |
| 453 | height=5.5cm, | |
| 221 | 454 |     legend entries={Python, Java 8, JavaScript},  
 | 
| 222 | 455 | legend pos=north west, | 
| 456 | legend cell align=left] | |
| 218 | 457 | \addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
 | 
| 458 | \addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
 | |
| 221 | 459 | \addplot[red,mark=*, mark options={fill=white}] table {re-js.data};
 | 
| 218 | 460 | \end{axis}
 | 
| 461 | \end{tikzpicture}
 | |
| 462 | & | |
| 463 | \begin{tikzpicture}
 | |
| 464 | \begin{axis}[
 | |
| 465 |     xlabel={$n$},
 | |
| 466 |     x label style={at={(1.05,0.0)}},
 | |
| 467 |     ylabel={time in secs},
 | |
| 468 |     y label style={at={(0.06,0.5)}},
 | |
| 469 | %enlargelimits=false, | |
| 470 |     %xtick={0,5000,...,30000},
 | |
| 471 | xmax=65000, | |
| 472 | ymax=45, | |
| 473 |     ytick={0,5,...,40},
 | |
| 474 | scaled ticks=false, | |
| 475 | axis lines=left, | |
| 476 | width=6cm, | |
| 477 | height=5.5cm, | |
| 478 |     legend entries={Java 9},  
 | |
| 479 | legend pos=north west] | |
| 480 | \addplot[cyan,mark=*, mark options={fill=white}] table {re-java9.data};
 | |
| 481 | \end{axis}
 | |
| 482 | \end{tikzpicture}
 | |
| 483 | \end{tabular}  
 | |
| 484 | \end{center}
 | |
| 485 | \newpage | |
| 486 | ||
| 487 | \subsection*{Part 2 (4 Marks)}
 | |
| 488 | ||
| 221 | 489 | The \emph{Shunting Yard Algorithm} has been developed by Edsger Dijkstra,
 | 
| 490 | an influential computer scientist who developed many well-known | |
| 491 | algorithms. This algorithm transforms the usual infix notation of | |
| 492 | arithmetic expressions into the postfix notation, sometimes also | |
| 493 | called reverse Polish notation. | |
| 218 | 494 | |
| 221 | 495 | Why on Earth do people use the postfix notation? It is much more | 
| 496 | convenient to work with the usual infix notation for arithmetic | |
| 497 | expressions. Most modern calculators (as opposed to the ones used 20 | |
| 498 | years ago) understand infix notation. So why on Earth? \ldots{}Well,
 | |
| 499 | many computers under the hood, even nowadays, use postfix notation | |
| 500 | extensively. For example if you give to the Java compiler the | |
| 224 | 501 | expression $1 + ((2 * 3) + (4 - 3))$, it will generate the Java Byte | 
| 221 | 502 | code | 
| 503 | ||
| 504 | \begin{lstlisting}[language=JVMIS,numbers=none]
 | |
| 505 | ldc 1 | |
| 506 | ldc 2 | |
| 507 | ldc 3 | |
| 508 | imul | |
| 509 | ldc 4 | |
| 510 | ldc 3 | |
| 511 | isub | |
| 512 | iadd | |
| 513 | iadd | |
| 514 | \end{lstlisting}
 | |
| 218 | 515 | |
| 516 | \noindent | |
| 221 | 517 | where the command \texttt{ldc} loads a constant onto a stack, and \texttt{imul},
 | 
| 518 | \texttt{isub} and \texttt{iadd} are commands acting on the stack. Clearly this
 | |
| 519 | is the arithmetic expression in postfix notation.\bigskip | |
| 218 | 520 | |
| 521 | \noindent | |
| 221 | 522 | The shunting yard algorithm processes an input token list using an | 
| 523 | operator stack and an output list. The input consists of numbers, | |
| 524 | operators ($+$, $-$, $*$, $/$) and parentheses, and for the purpose of | |
| 525 | the assignment we assume the input is always a well-formed expression | |
| 224 | 526 | in infix notation. The calculation in the shunting yard algorithm uses | 
| 527 | information about the | |
| 221 | 528 | precedences of the operators (given in the template file). The | 
| 529 | algorithm processes the input token list as follows: | |
| 109 | 530 | |
| 531 | \begin{itemize}
 | |
| 221 | 532 | \item If there is a number as input token, then this token is | 
| 224 | 533 | transferred directly to the output list. Then the rest of the input is | 
| 221 | 534 | processed. | 
| 535 | \item If there is an operator as input token, then you need to check | |
| 536 | what is on top of the operator stack. If there are operators with | |
| 537 | a higher or equal precedence, these operators are first popped off | |
| 224 | 538 | from the stack and moved to the output list. Then the operator from the input | 
| 221 | 539 | is pushed onto the stack and the rest of the input is processed. | 
| 540 | \item If the input is a left-parenthesis, you push it on to the stack | |
| 541 | and continue processing the input. | |
| 224 | 542 | \item If the input is a right-parenthesis, then you pop off all operators | 
| 221 | 543 | from the stack to the output list until you reach the left-parenthesis. | 
| 544 | Then you discharge the $($ and $)$ from the input and stack, and continue | |
| 224 | 545 | processing the input list. | 
| 221 | 546 | \item If the input is empty, then you move all remaining operators | 
| 547 | from the stack to the output list. | |
| 548 | \end{itemize}  
 | |
| 218 | 549 | |
| 221 | 550 | \subsubsection*{Tasks (file postfix.scala)}
 | 
| 109 | 551 | |
| 221 | 552 | \begin{itemize}
 | 
| 224 | 553 | \item[(7)] Implement the shunting yard algorithm described above. The | 
| 221 | 554 |   function, called \texttt{syard}, takes a list of tokens as first
 | 
| 555 | argument. The second and third arguments are the stack and output | |
| 556 | list represented as Scala lists. The most convenient way to | |
| 557 | implement this algorithm is to analyse what the input list, stack | |
| 224 | 558 | and output list look like in each step using pattern-matching. The | 
| 221 | 559 | algorithm transforms for example the input | 
| 218 | 560 | |
| 221 | 561 | \[ | 
| 562 |   \texttt{List(3, +, 4, *, (, 2, -, 1, ))}
 | |
| 563 | \] | |
| 109 | 564 | |
| 221 | 565 | into the postfix output | 
| 218 | 566 | |
| 221 | 567 | \[ | 
| 568 |   \texttt{List(3, 4, 2, 1, -, *, +)}
 | |
| 569 | \] | |
| 109 | 570 | |
| 221 | 571 | You can assume the input list is always a list representing | 
| 572 | a well-formed infix arithmetic expression.\hfill[1 Mark] | |
| 573 | ||
| 574 | \item[(8)] Implement a compute function that takes a postfix expression | |
| 575 | as argument and evaluates it generating an integer as result. It uses a | |
| 576 | stack to evaluate the postfix expression. The operators $+$, $-$, $*$ | |
| 577 | are as usual; $/$ is division on integers, for example $7 / 3 = 2$. | |
| 578 | \hfill[1 Mark] | |
| 579 | \end{itemize}
 | |
| 580 | ||
| 581 | \subsubsection*{Task (file postfix2.scala)}
 | |
| 582 | ||
| 583 | \begin{itemize}
 | |
| 584 | \item[(9)] Extend the code in (7) and (8) to include the power | |
| 585 | operator. This requires proper account of associativity of | |
| 586 | the operators. The power operator is right-associative, whereas the | |
| 587 | other operators are left-associative. Left-associative operators | |
| 588 | are popped off if the precedence is bigger or equal, while | |
| 589 | right-associative operators are only popped off if the precedence is | |
| 590 | bigger. The compute function in this task should use | |
| 591 |   \texttt{Long}s, rather than \texttt{Int}s.\hfill[2 Marks]
 | |
| 592 | \end{itemize}
 | |
| 218 | 593 | |
| 594 | ||
| 595 | ||
| 6 | 596 | |
| 597 | \end{document}
 | |
| 598 | ||
| 68 | 599 | |
| 6 | 600 | %%% Local Variables: | 
| 601 | %%% mode: latex | |
| 602 | %%% TeX-master: t | |
| 603 | %%% End: |