cws/cw03.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 23 Nov 2017 10:56:47 +0000
changeset 153 4383809c176a
parent 152 114a89518aea
child 154 39c6b93718f0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
     2
\usepackage{../style}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
     3
\usepackage{../langs}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
     4
\usepackage{tikz}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
     5
\usepackage{pgf}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
     6
\usepackage{pgfplots}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
     7
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
     8
\begin{filecontents}{re-python2.data}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
     9
1 0.033
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    10
5 0.036
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    11
10 0.034
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    12
15 0.036
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    13
18 0.059
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    14
19 0.084
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    15
20 0.141
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    16
21 0.248
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    17
22 0.485
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    18
23 0.878
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    19
24 1.71
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    20
25 3.40
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    21
26 7.08
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    22
27 14.12
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    23
28 26.69
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    24
\end{filecontents}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    25
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    26
\begin{filecontents}{re-java.data}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    27
5  0.00298
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    28
10  0.00418
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    29
15  0.00996
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    30
16  0.01710
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    31
17  0.03492
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    32
18  0.03303
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    33
19  0.05084
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    34
20  0.10177
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    35
21  0.19960
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    36
22  0.41159
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    37
23  0.82234
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    38
24  1.70251
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    39
25  3.36112
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    40
26  6.63998
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    41
27  13.35120
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    42
28  29.81185
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    43
\end{filecontents}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    44
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    48
\section*{Coursework 8 (Scala, Regular Expressions, Brainf***)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    50
This coursework is worth 10\%. It is about regular expressions,
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    51
pattern matching and an interpreter. The first part is due on 30
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    52
November at 11pm; the second, more advanced part, is due on 21
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    53
December at 11pm. In the first part, you are asked to implement a
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    54
regular expression matcher based on derivatives of regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    55
expressions. The reason is that regular expression matching in Java
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    56
can sometimes be extremely slow. The advanced part is about an
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    57
interpreter for a very simple programming language.\bigskip
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    58
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    59
\noindent
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    60
\textbf{Important:}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    61
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    62
\begin{itemize}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    63
\item Make sure the files you submit can be processed by just calling\\
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    64
  \mbox{\texttt{scala <<filename.scala>>}} on the commandline. Use the
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    65
  template files provided and do not make any changes to arguments of
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    66
  functions or to any types. You are free to implement any auxiliary
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    67
  function you might need.
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    68
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    69
\item Do not use any mutable data structures in your
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    70
submissions! They are not needed. This means you cannot create new 
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    71
\texttt{Array}s or \texttt{ListBuffer}s, for example. 
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
    72
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    73
\item Do not use \texttt{return} in your code! It has a different
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    74
  meaning in Scala, than in Java.
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    75
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    76
\item Do not use \texttt{var}! This declares a mutable variable. Only
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    77
  use \texttt{val}!
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    78
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    79
\item Do not use any parallel collections! No \texttt{.par} therefore!
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    80
  Our testing and marking infrastructure is not set up for it.
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    81
\end{itemize}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    82
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    83
\noindent
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    84
Also note that the running time of each part will be restricted to a
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    85
maximum of 360 seconds on my laptop
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 100
diff changeset
    88
\subsection*{Disclaimer}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
It should be understood that the work you submit represents
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    91
your own effort! You have not copied from anyone else. An
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
exception is the Scala code I showed during the lectures or
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
uploaded to KEATS, which you can freely use.\bigskip
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
    96
\subsection*{Part 1 (6 Marks)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
    98
The task is to implement a regular expression matcher that is based on
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    99
derivatives of regular expressions. Most of the functions are defined by
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   100
recursion over regular expressions and can be elegantly implemented
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   101
using Scala's pattern-matching. The implementation should deal with the
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   102
following regular expressions, which have been predefined in the file
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   103
\texttt{re.scala}:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
\begin{tabular}{lcll}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  $r$ & $::=$ & $\ZERO$     & cannot match anything\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
      &   $|$ & $\ONE$      & can only match the empty string\\
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   109
      &   $|$ & $c$         & can match a character (in this case $c$)\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   110
      &   $|$ & $r_1 + r_2$ & can match a string either with $r_1$ or with $r_2$\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   111
  &   $|$ & $r_1\cdot r_2$ & can match the first part of a string with $r_1$ and\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   112
          &  & & then the second part with $r_2$\\
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
      &   $|$ & $r^*$       & can match zero or more times $r$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
\end{tabular}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
\end{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   117
\noindent 
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   118
Why? Knowing how to match regular expressions and strings will let you
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   119
solve a lot of problems that vex other humans. Regular expressions are
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   120
one of the fastest and simplest ways to match patterns in text, and
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   121
are endlessly useful for searching, editing and analysing data in all
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   122
sorts of places (for example analysing network traffic in order to
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   123
detect security breaches). However, you need to be fast, otherwise you
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   124
will stumble over problems such as recently reported at
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   125
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   126
{\small
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   127
\begin{itemize}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   128
\item[$\bullet$] \url{http://stackstatus.net/post/147710624694/outage-postmortem-july-20-2016}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   129
\item[$\bullet$] \url{https://vimeo.com/112065252}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   130
\item[$\bullet$] \url{http://davidvgalbraith.com/how-i-fixed-atom/}  
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   131
\end{itemize}}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   132
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   133
\subsubsection*{Tasks (file re.scala)}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   134
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   135
\begin{itemize}
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   136
\item[(1a)] Implement a function, called \textit{nullable}, by
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   137
  recursion over regular expressions. This function tests whether a
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   138
  regular expression can match the empty string. This means given a
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   139
  regular expression it either returns true or false.
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
\begin{tabular}{lcl}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
$\textit{nullable}(\ONE)$  & $\dn$ & $\textit{true}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
$\textit{nullable}(c)$     & $\dn$ & $\textit{false}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
$\textit{nullable}(r_1 + r_2)$ & $\dn$ & $\textit{nullable}(r_1) \vee \textit{nullable}(r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
$\textit{nullable}(r_1 \cdot r_2)$ & $\dn$ & $\textit{nullable}(r_1) \wedge \textit{nullable}(r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
\end{tabular}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   150
\end{center}\hfill[1 Mark]
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   151
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   152
\item[(1b)] Implement a function, called \textit{der}, by recursion over
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   153
  regular expressions. It takes a character and a regular expression
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   154
  as arguments and calculates the derivative regular expression according
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   155
  to the rules:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
\begin{tabular}{lcl}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
$\textit{der}\;c\;(\ONE)$  & $\dn$ & $\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
$\textit{der}\;c\;(d)$     & $\dn$ & $\textit{if}\; c = d\;\textit{then} \;\ONE \; \textit{else} \;\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
$\textit{der}\;c\;(r_1 + r_2)$ & $\dn$ & $(\textit{der}\;c\;r_1) + (\textit{der}\;c\;r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
$\textit{der}\;c\;(r_1 \cdot r_2)$ & $\dn$ & $\textit{if}\;\textit{nullable}(r_1)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
      & & $\textit{then}\;((\textit{der}\;c\;r_1)\cdot r_2) + (\textit{der}\;c\;r_2)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
      & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
\end{tabular}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   168
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   169
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   170
For example given the regular expression $r = (a \cdot b) \cdot c$, the derivatives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   171
w.r.t.~the characters $a$, $b$ and $c$ are
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   172
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   173
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   174
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   175
    $\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & ($= r'$)\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   176
    $\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   177
    $\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   178
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   179
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   180
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   181
Let $r'$ stand for the first derivative, then taking the derivatives of $r'$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   182
w.r.t.~the characters $a$, $b$ and $c$ gives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   183
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   184
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   185
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   186
    $\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   187
    $\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & ($= r''$)\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   188
    $\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   189
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   190
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   191
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   192
One more example: Let $r''$ stand for the second derivative above,
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   193
then taking the derivatives of $r''$ w.r.t.~the characters $a$, $b$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   194
and $c$ gives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   195
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   196
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   197
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   198
    $\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   199
    $\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   200
    $\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ &
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   201
    (is $\textit{nullable}$)                      
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   202
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   203
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   204
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   205
Note, the last derivative can match the empty string, that is it is \textit{nullable}.\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   206
\mbox{}\hfill\mbox{[1 Mark]}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   208
\item[(1c)] Implement the function \textit{simp}, which recursively
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   209
  traverses a regular expression from the inside to the outside, and
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   210
  on the way simplifies every regular expression on the left (see
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   211
  below) to the regular expression on the right, except it does not
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   212
  simplify inside ${}^*$-regular expressions.
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   214
  \begin{center}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   215
\begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
$r \cdot \ONE$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
$\ONE \cdot r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
$r + \ZERO$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
$\ZERO + r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
$r + r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
\end{tabular}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   224
  \end{center}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   225
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   226
  For example the regular expression
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   227
  \[(r_1 + \ZERO) \cdot \ONE + ((\ONE + r_2) + r_3) \cdot (r_4 \cdot \ZERO)\]
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   228
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   229
  simplifies to just $r_1$. \textbf{Hint:} Regular expressions can be
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   230
  seen as trees and there are several methods for traversing
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   231
  trees. One of them corresponds to the inside-out traversal, which is
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   232
  sometimes also called post-order traversal. Furthermore,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   233
  remember numerical expressions from school times: there you had expressions
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   234
  like $u + \ldots + (1 \cdot x) - \ldots (z + (y \cdot 0)) \ldots$
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   235
  and simplification rules that looked very similar to rules
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   236
  above. You would simplify such numerical expressions by replacing
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   237
  for example the $y \cdot 0$ by $0$, or $1\cdot x$ by $x$, and then
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   238
  look whether more rules are applicable. If you organise the
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   239
  simplification in an inside-out fashion, it is always clear which
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   240
  rule should be applied next.\hfill[2 Marks]
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   241
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   242
\item[(1d)] Implement two functions: The first, called \textit{ders},
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   243
  takes a list of characters and a regular expression as arguments, and
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   244
  builds the derivative w.r.t.~the list as follows:
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   245
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   246
\begin{center}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   247
\begin{tabular}{lcl}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   248
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   249
  $\textit{ders}\;(c::cs)\;r$  & $\dn$ &
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   250
    $\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   251
\end{tabular}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
\end{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   254
Note that this function is different from \textit{der}, which only
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   255
takes a single character.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   256
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   257
The second function, called \textit{matcher}, takes a string and a
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   258
regular expression as arguments. It builds first the derivatives
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   259
according to \textit{ders} and after that tests whether the resulting
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   260
derivative regular expression can match the empty string (using
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   261
\textit{nullable}).  For example the \textit{matcher} will produce
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   262
true for the regular expression $(a\cdot b)\cdot c$ and the string
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   263
$abc$, but false if you give it the string $ab$. \hfill[1 Mark]
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   264
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   265
\item[(1e)] Implement a function, called \textit{size}, by recursion
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   266
  over regular expressions. If a regular expression is seen as a tree,
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   267
  then \textit{size} should return the number of nodes in such a
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   268
  tree. Therefore this function is defined as follows:
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   269
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   270
\begin{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   271
\begin{tabular}{lcl}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   272
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   273
$\textit{size}(\ONE)$  & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   274
$\textit{size}(c)$     & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   275
$\textit{size}(r_1 + r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   276
$\textit{size}(r_1 \cdot r_2)$ & $\dn$ & $1 + \textit{size}(r_1) + \textit{size}(r_2)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   277
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   278
\end{tabular}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   279
\end{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   280
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   281
You can use \textit{size} in order to test how much the `evil' regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   282
expression $(a^*)^* \cdot b$ grows when taking successive derivatives
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   283
according the letter $a$ without simplification and then compare it to
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   284
taking the derivative, but simplify the result.  The sizes
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   285
are given in \texttt{re.scala}. \hfill[1 Mark]
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   286
\end{itemize}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   287
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   288
\subsection*{Background}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   289
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   290
Although easily implementable in Scala, the idea behind the derivative
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   291
function might not so easy to be seen. To understand its purpose
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   292
better, assume a regular expression $r$ can match strings of the form
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   293
$c\!::\!cs$ (that means strings which start with a character $c$ and have
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   294
some rest, or tail, $cs$). If you take the derivative of $r$ with
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   295
respect to the character $c$, then you obtain a regular expression
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   296
that can match all the strings $cs$.  In other words, the regular
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   297
expression $\textit{der}\;c\;r$ can match the same strings $c\!::\!cs$
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   298
that can be matched by $r$, except that the $c$ is chopped off.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   299
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   300
Assume now $r$ can match the string $abc$. If you take the derivative
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   301
according to $a$ then you obtain a regular expression that can match
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   302
$bc$ (it is $abc$ where the $a$ has been chopped off). If you now
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   303
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   304
obtain a regular expression that can match the string $c$ (it is $bc$
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   305
where $b$ is chopped off). If you finally build the derivative of this
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   306
according $c$, that is
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   307
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   308
a regular expression that can match the empty string. You can test
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   309
whether this is indeed the case using the function nullable, which is
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   310
what your matcher is doing.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   311
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   312
The purpose of the $\textit{simp}$ function is to keep the regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   313
expression small. Normally the derivative function makes the regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   314
expression bigger (see the SEQ case and the example in (1b)) and the
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   315
algorithm would be slower and slower over time. The $\textit{simp}$
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   316
function counters this increase in size and the result is that the
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   317
algorithm is fast throughout.  By the way, this algorithm is by Janusz
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   318
Brzozowski who came up with the idea of derivatives in 1964 in his PhD
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   319
thesis.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   320
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   321
\begin{center}\small
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   322
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   323
\end{center}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   325
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   326
If you want to see how badly the regular expression matchers do in
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   327
Java and Python with the `evil' regular expression, then have a look
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   328
at the graphs below (you can try it out for yourself: have a look at
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   329
the file \texttt{catastrophic.java} on KEATS). Compare this with the
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   330
matcher you have implemented. How long can the string of $a$'s be
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   331
in your matcher and stay within the 30 seconds time limit?
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   332
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   333
\begin{center}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   334
\begin{tikzpicture}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   335
\begin{axis}[
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   336
    title={Graph: $(a^*)^*\cdot b$ and strings 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   337
           $\underbrace{a\ldots a}_{n}$},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   338
    xlabel={$n$},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   339
    x label style={at={(1.05,0.0)}},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   340
    ylabel={time in secs},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   341
    enlargelimits=false,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   342
    xtick={0,5,...,30},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   343
    xmax=33,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   344
    ymax=35,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   345
    ytick={0,5,...,30},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   346
    scaled ticks=false,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   347
    axis lines=left,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   348
    width=6cm,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   349
    height=5.0cm, 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   350
    legend entries={Python, Java},  
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   351
    legend pos=outer north east]
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   352
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   353
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   354
\end{axis}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   355
\end{tikzpicture}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   356
\end{center}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   357
\newpage
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   358
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   359
\subsection*{Part 2 (4 Marks)}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   360
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   361
Comming from Java or C++, you might think Scala is a quite
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   362
esotheric programming language.  But remember, some serious companies
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   363
have built their business on Scala. And there are far more esotheric
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   364
languages out there. One is called \emph{brainf***}. Urban M\"uller
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   365
developed this language in 1993.  A close relative was already
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   366
introduced in ... by Corado B\"ohm, an Italian computer pionier, who
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   367
unfortunately died a few months ago. One feature of brainf*** is its
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   368
minimalistic set of instructions. It has just 8 instructions, all of
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   369
which are single characters. Despite this minimalism, this language,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   370
given enough memory, has been shown to be Turing complete. In this
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   371
part you will implement an interpreter for this language.
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   372
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   373
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   374
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   375
\subsubsection*{Tasks (file bf.scala)}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   376
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   377
\begin{itemize}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   378
\item[(2a)] 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   379
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   380
\item[(2b)]   
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   381
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   382
\item[(2c)]
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   383
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   384
\end{itemize}\bigskip  
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   385
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   386
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   387
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   388
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   391
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
%%% End: