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