cws/cw03.tex
author Christian Urban <urbanc@in.tum.de>
Thu, 07 Dec 2017 12:04:31 +0000
changeset 163 84917f2e16cd
parent 159 92c31cbb1952
child 166 780c40aaad27
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}
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     7
\usepackage{stackengine}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     8
%%\usepackage{accents}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     9
\newcommand\barbelow[1]{\stackunder[1.2pt]{#1}{\raisebox{-4mm}{\boldmath$\uparrow$}}}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    10
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    11
\begin{filecontents}{re-python2.data}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    12
1 0.033
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    13
5 0.036
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    14
10 0.034
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    15
15 0.036
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    16
18 0.059
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    17
19 0.084
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    18
20 0.141
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    19
21 0.248
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    20
22 0.485
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    21
23 0.878
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    22
24 1.71
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    23
25 3.40
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    24
26 7.08
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    25
27 14.12
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    26
28 26.69
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    27
\end{filecontents}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    28
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    29
\begin{filecontents}{re-java.data}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    30
5  0.00298
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    31
10  0.00418
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    32
15  0.00996
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    33
16  0.01710
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    34
17  0.03492
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    35
18  0.03303
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    36
19  0.05084
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    37
20  0.10177
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    38
21  0.19960
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    39
22  0.41159
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    40
23  0.82234
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    41
24  1.70251
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    42
25  3.36112
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    43
26  6.63998
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    44
27  13.35120
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    45
28  29.81185
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    46
\end{filecontents}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    47
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    48
\begin{filecontents}{re-java9.data}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    49
1000  0.01410
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    50
2000  0.04882
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    51
3000  0.10609
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    52
4000  0.17456
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    53
5000  0.27530
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    54
6000  0.41116
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    55
7000  0.53741
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    56
8000  0.70261
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    57
9000  0.93981
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    58
10000 0.97419
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    59
11000 1.28697
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    60
12000 1.51387
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    61
14000 2.07079
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    62
16000 2.69846
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    63
20000 4.41823
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    64
24000 6.46077
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    65
26000 7.64373
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    66
30000 9.99446
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    67
34000 12.966885
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    68
38000 16.281621
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    69
42000 19.180228
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    70
46000 21.984721
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    71
50000 26.950203
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    72
60000 43.0327746
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    73
\end{filecontents}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
    74
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    78
  
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    79
\section*{Coursework 8 (Regular Expressions and Brainf***)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
    81
This coursework is worth 10\%. It is about regular expressions,
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    82
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
    83
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
    84
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
    85
regular expression matcher based on derivatives of regular
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    86
expressions. The reason is that regular expression matching in Java
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    87
and Python can sometimes be extremely slow. The advanced part is about
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    88
an interpreter for a very simple programming language.\bigskip
62
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    89
2151c77e1e24 updated
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    90
\noindent
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    91
\textbf{Important:}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    92
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    93
\begin{itemize}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    94
\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
    95
  \mbox{\texttt{scala <<filename.scala>>}} on the commandline. Use the
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    96
  template files provided and do not make any changes to arguments of
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    97
  functions or to any types. You are free to implement any auxiliary
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    98
  function you might need.
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    99
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   100
\item Do not use any mutable data structures in your
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   101
submissions! They are not needed. This means you cannot create new 
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   102
\texttt{Array}s or \texttt{ListBuffer}s, for example. 
86
f8a781322499 updated
Christian Urban <urbanc@in.tum.de>
parents: 79
diff changeset
   103
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   104
\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
   105
  meaning in Scala, than in Java.
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   106
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   107
\item Do not use \texttt{var}! This declares a mutable variable. Only
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   108
  use \texttt{val}!
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   109
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   110
\item Do not use any parallel collections! No \texttt{.par} therefore!
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   111
  Our testing and marking infrastructure is not set up for it.
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   112
\end{itemize}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   113
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   114
\noindent
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   115
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
   116
maximum of 360 seconds on my laptop
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
110
62389faa66e4 updated
Christian Urban <urbanc@in.tum.de>
parents: 100
diff changeset
   119
\subsection*{Disclaimer}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
It should be understood that the work you submit represents
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   122
your \textbf{own} effort! You have not copied from anyone else. An
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
exception is the Scala code I showed during the lectures or
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
uploaded to KEATS, which you can freely use.\bigskip
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   127
\subsection*{Part 1 (6 Marks)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   129
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
   130
derivatives of regular expressions. Most of the functions are defined by
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   131
recursion over regular expressions and can be elegantly implemented
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   132
using Scala's pattern-matching. The implementation should deal with the
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   133
following regular expressions, which have been predefined in the file
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   134
\texttt{re.scala}:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
\begin{tabular}{lcll}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  $r$ & $::=$ & $\ZERO$     & cannot match anything\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
      &   $|$ & $\ONE$      & can only match the empty string\\
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   140
      &   $|$ & $c$         & can match a single character (in this case $c$)\\
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   141
      &   $|$ & $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
   142
  &   $|$ & $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
   143
          &  & & then the second part with $r_2$\\
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
      &   $|$ & $r^*$       & can match zero or more times $r$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
\end{tabular}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
\end{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   148
\noindent 
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   149
Why? Knowing how to match regular expressions and strings will let you
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   150
solve a lot of problems that vex other humans. Regular expressions are
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   151
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
   152
are endlessly useful for searching, editing and analysing data in all
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   153
sorts of places (for example analysing network traffic in order to
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   154
detect security breaches). However, you need to be fast, otherwise you
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   155
will stumble over problems such as recently reported at
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   156
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   157
{\small
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   158
\begin{itemize}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   159
\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
   160
\item[$\bullet$] \url{https://vimeo.com/112065252}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   161
\item[$\bullet$] \url{http://davidvgalbraith.com/how-i-fixed-atom/}  
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   162
\end{itemize}}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   163
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   164
\subsubsection*{Tasks (file re.scala)}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   165
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   166
The file \texttt{re.scala} has already a definition for regular
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   167
expressions and also defines some handy shorthand notation for
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   168
regular expressions. The notation in this document matches up
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   169
with the code in the file as follows:
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   170
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   171
\begin{center}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   172
  \begin{tabular}{rcl@{\hspace{10mm}}l}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   173
    & & code: & shorthand:\smallskip \\ 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   174
  $\ZERO$ & $\mapsto$ & \texttt{ZERO}\\
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   175
  $\ONE$  & $\mapsto$ & \texttt{ONE}\\
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   176
  $c$     & $\mapsto$ & \texttt{CHAR(c)}\\
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   177
  $r_1 + r_2$ & $\mapsto$ & \texttt{ALT(r1, r2)} & \texttt{r1 | r2}\\
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   178
  $r_1 \cdot r_2$ & $\mapsto$ & \texttt{SEQ(r1, r2)} & \texttt{r1 $\sim$ r2}\\
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   179
  $r^*$ & $\mapsto$ &  \texttt{STAR(r)} & \texttt{r.\%}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   180
\end{tabular}    
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   181
\end{center}  
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   182
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   183
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   184
\begin{itemize}
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   185
\item[(1a)] Implement a function, called \textit{nullable}, by
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   186
  recursion over regular expressions. This function tests whether a
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   187
  regular expression can match the empty string. This means given a
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   188
  regular expression it either returns true or false. The function
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   189
  \textit{nullable}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   190
  is defined as follows:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
\begin{tabular}{lcl}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
$\textit{nullable}(\ZERO)$ & $\dn$ & $\textit{false}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
$\textit{nullable}(\ONE)$  & $\dn$ & $\textit{true}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
$\textit{nullable}(c)$     & $\dn$ & $\textit{false}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
$\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
   198
$\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
   199
$\textit{nullable}(r^*)$ & $\dn$ & $\textit{true}$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
\end{tabular}
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   201
\end{center}~\hfill[1 Mark]
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   202
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   203
\item[(1b)] Implement a function, called \textit{der}, by recursion over
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   204
  regular expressions. It takes a character and a regular expression
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   205
  as arguments and calculates the derivative regular expression according
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   206
  to the rules:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
\begin{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
\begin{tabular}{lcl}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
$\textit{der}\;c\;(\ZERO)$ & $\dn$ & $\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
$\textit{der}\;c\;(\ONE)$  & $\dn$ & $\ZERO$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
$\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
   213
$\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
   214
$\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
   215
      & & $\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
   216
      & & $\textit{else}\;(\textit{der}\;c\;r_1)\cdot r_2$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
$\textit{der}\;c\;(r^*)$ & $\dn$ & $(\textit{der}\;c\;r)\cdot (r^*)$\\
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
\end{tabular}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   219
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   220
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   221
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
   222
w.r.t.~the characters $a$, $b$ and $c$ are
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   223
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   224
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   225
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   226
    $\textit{der}\;a\;r$ & $=$ & $(\ONE \cdot b)\cdot c$ & ($= r'$)\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   227
    $\textit{der}\;b\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   228
    $\textit{der}\;c\;r$ & $=$ & $(\ZERO \cdot b)\cdot c$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   229
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   230
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   231
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   232
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
   233
w.r.t.~the characters $a$, $b$ and $c$ gives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   234
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   235
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   236
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   237
    $\textit{der}\;a\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$ \\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   238
    $\textit{der}\;b\;r'$ & $=$ & $((\ZERO \cdot b) + \ONE)\cdot c$ & ($= r''$)\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   239
    $\textit{der}\;c\;r'$ & $=$ & $((\ZERO \cdot b) + \ZERO)\cdot c$
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   240
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   241
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   242
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   243
One more example: Let $r''$ stand for the second derivative above,
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   244
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
   245
and $c$ gives
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   246
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   247
\begin{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   248
  \begin{tabular}{lcll}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   249
    $\textit{der}\;a\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$ \\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   250
    $\textit{der}\;b\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ZERO$\\
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   251
    $\textit{der}\;c\;r''$ & $=$ & $((\ZERO \cdot b) + \ZERO) \cdot c + \ONE$ &
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   252
    (is $\textit{nullable}$)                      
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   253
  \end{tabular}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   254
\end{center}
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   255
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   256
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
   257
\mbox{}\hfill\mbox{[1 Mark]}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   259
\item[(1c)] Implement the function \textit{simp}, which recursively
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   260
  traverses a regular expression from the inside to the outside, and
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   261
  on the way simplifies every regular expression on the left (see
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   262
  below) to the regular expression on the right, except it does not
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   263
  simplify inside ${}^*$-regular expressions.
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   265
  \begin{center}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   266
\begin{tabular}{l@{\hspace{4mm}}c@{\hspace{4mm}}ll}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
$r \cdot \ZERO$ & $\mapsto$ & $\ZERO$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
$\ZERO \cdot r$ & $\mapsto$ & $\ZERO$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
$r \cdot \ONE$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
$\ONE \cdot r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
$r + \ZERO$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
$\ZERO + r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
$r + r$ & $\mapsto$ & $r$\\ 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
\end{tabular}
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   275
  \end{center}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   276
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   277
  For example the regular expression
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   278
  \[(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
   279
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   280
  simplifies to just $r_1$. \textbf{Hint:} Regular expressions can be
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   281
  seen as trees and there are several methods for traversing
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   282
  trees. One of them corresponds to the inside-out traversal, which is
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   283
  sometimes also called post-order traversal. Furthermore,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   284
  remember numerical expressions from school times: there you had expressions
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
   285
  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
   286
  and simplification rules that looked very similar to rules
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   287
  above. You would simplify such numerical expressions by replacing
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   288
  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
   289
  look whether more rules are applicable. If you organise the
79
2d57b0d43a0f updated
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   290
  simplification in an inside-out fashion, it is always clear which
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   291
  rule should be applied next.\hfill[2 Marks]
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   292
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   293
\item[(1d)] Implement two functions: The first, called \textit{ders},
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   294
  takes a list of characters and a regular expression as arguments, and
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   295
  builds the derivative w.r.t.~the list as follows:
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   296
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   297
\begin{center}
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   298
\begin{tabular}{lcl}
69
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   299
$\textit{ders}\;(Nil)\;r$ & $\dn$ & $r$\\
f1295a0ab4ed updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   300
  $\textit{ders}\;(c::cs)\;r$  & $\dn$ &
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   301
    $\textit{ders}\;cs\;(\textit{simp}(\textit{der}\;c\;r))$\\
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   302
\end{tabular}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
\end{center}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   305
Note that this function is different from \textit{der}, which only
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   306
takes a single character.
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   307
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   308
The second function, called \textit{matcher}, takes a string and a
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   309
regular expression as arguments. It builds first the derivatives
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   310
according to \textit{ders} and after that tests whether the resulting
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   311
derivative regular expression can match the empty string (using
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   312
\textit{nullable}).  For example the \textit{matcher} will produce
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   313
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
   314
$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
   315
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   316
\item[(1e)] Implement a function, called \textit{size}, by recursion
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   317
  over regular expressions. If a regular expression is seen as a tree,
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   318
  then \textit{size} should return the number of nodes in such a
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   319
  tree. Therefore this function is defined as follows:
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   320
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   321
\begin{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   322
\begin{tabular}{lcl}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   323
$\textit{size}(\ZERO)$ & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   324
$\textit{size}(\ONE)$  & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   325
$\textit{size}(c)$     & $\dn$ & $1$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   326
$\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
   327
$\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
   328
$\textit{size}(r^*)$ & $\dn$ & $1 + \textit{size}(r)$\\
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   329
\end{tabular}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   330
\end{center}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   331
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   332
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
   333
expression $(a^*)^* \cdot b$ grows when taking successive derivatives
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   334
according the letter $a$ without simplification and then compare it to
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   335
taking the derivative, but simplify the result.  The sizes
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   336
are given in \texttt{re.scala}. \hfill[1 Mark]
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   337
\end{itemize}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   338
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   339
\subsection*{Background}
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   340
94
ae4708c851ee updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 86
diff changeset
   341
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
   342
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
   343
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
   344
$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
   345
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
   346
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
   347
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
   348
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
   349
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
   350
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   351
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
   352
according to $a$ then you obtain a regular expression that can match
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   353
$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
   354
build the derivative $\textit{der}\;b\;(\textit{der}\;a\;r)$ you
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   355
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
   356
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
   357
according $c$, that is
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   358
$\textit{der}\;c\;(\textit{der}\;b\;(\textit{der}\;a\;r))$, you obtain
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   359
a regular expression that can match the empty string. You can test
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   360
whether this is indeed the case using the function nullable, which is
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   361
what your matcher is doing.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   362
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   363
The purpose of the $\textit{simp}$ function is to keep the regular
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   364
expressions small. Normally the derivative function makes the regular
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   365
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
   366
algorithm would be slower and slower over time. The $\textit{simp}$
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   367
function counters this increase in size and the result is that the
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   368
algorithm is fast throughout.  By the way, this algorithm is by Janusz
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   369
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
   370
thesis.
75
71e463b33a9e updated
Christian Urban <urbanc@in.tum.de>
parents: 74
diff changeset
   371
78
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   372
\begin{center}\small
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   373
\url{https://en.wikipedia.org/wiki/Janusz_Brzozowski_(computer_scientist)}
85f2f75abeeb updated
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   374
\end{center}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
153
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
If you want to see how badly the regular expression matchers do in
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   378
Java\footnote{Version 8 and below; Version 9 does not seem to be as
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   379
  catastrophic, but still worse than the regular expression matcher
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   380
based on derivatives.} and in Python with the `evil' regular
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   381
expression $(a^*)^*\cdot b$, then have a look at the graphs below (you
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   382
can try it out for yourself: have a look at the file
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   383
\texttt{catastrophic.java} and \texttt{catastrophic.py} on
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   384
KEATS). Compare this with the matcher you have implemented. How long
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   385
can the string of $a$'s be in your matcher and still stay within the
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   386
30 seconds time limit?
153
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
\begin{center}
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   389
\begin{tabular}{@{}cc@{}}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   390
\multicolumn{2}{c}{Graph: $(a^*)^*\cdot b$ and strings 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   391
           $\underbrace{a\ldots a}_{n}$}\bigskip\\
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   392
  
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   393
\begin{tikzpicture}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   394
\begin{axis}[
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   395
    xlabel={$n$},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   396
    x label style={at={(1.05,0.0)}},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   397
    ylabel={time in secs},
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   398
    y label style={at={(0.06,0.5)}},
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   399
    enlargelimits=false,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   400
    xtick={0,5,...,30},
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   401
    xmax=33,
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   402
    ymax=45,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   403
    ytick={0,5,...,40},
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   404
    scaled ticks=false,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   405
    axis lines=left,
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   406
    width=6cm,
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   407
    height=5.5cm, 
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   408
    legend entries={Python, Java 8},  
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   409
    legend pos=north west]
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   410
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   411
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   412
\end{axis}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   413
\end{tikzpicture}
163
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   414
  & 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   415
\begin{tikzpicture}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   416
\begin{axis}[
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   417
    xlabel={$n$},
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   418
    x label style={at={(1.05,0.0)}},
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   419
    ylabel={time in secs},
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   420
    y label style={at={(0.06,0.5)}},
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   421
    %enlargelimits=false,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   422
    %xtick={0,5000,...,30000},
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   423
    xmax=65000,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   424
    ymax=45,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   425
    ytick={0,5,...,40},
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   426
    scaled ticks=false,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   427
    axis lines=left,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   428
    width=6cm,
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   429
    height=5.5cm, 
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   430
    legend entries={Java 9},  
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   431
    legend pos=north west]
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   432
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java9.data};
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   433
\end{axis}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   434
\end{tikzpicture}
84917f2e16cd updated
Christian Urban <urbanc@in.tum.de>
parents: 159
diff changeset
   435
\end{tabular}  
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   436
\end{center}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   437
\newpage
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   438
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   439
\subsection*{Part 2 (4 Marks)}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   440
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   441
Coming from Java or C++, you might think Scala is a quite esoteric
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   442
programming language.  But remember, some serious companies have built
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   443
their business on
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   444
Scala.\footnote{\url{https://en.wikipedia.org/wiki/Scala_(programming_language)\#Companies}}
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   445
And there are far, far more esoteric languages out there. One is
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   446
called \emph{brainf***}. You are asked in this part to implement an
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   447
interpreter for this language.
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   448
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   449
Urban M\"uller developed brainf*** in 1993.  A close relative of this
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   450
language was already introduced in 1964 by Corado B\"ohm, an Italian
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   451
computer pioneer, who unfortunately died a few months ago. The main
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   452
feature of brainf*** is its minimalistic set of instructions---just 8
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   453
instructions in total and all of which are single characters. Despite
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   454
the minimalism, this language has been shown to be Turing
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   455
complete\ldots{}if this doesn't ring any bell with you: it roughly
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   456
means that every algorithm we know can, in principle, be implemented in
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   457
brainf***. It just takes a lot of determination and quite a lot of
156
cc6d036401f4 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   458
memory resources. Some relatively sophisticated sample programs in
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   459
brainf*** are given in the file \texttt{bf.scala}.\bigskip
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   460
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   461
\noindent
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   462
As mentioned above, brainf*** has 8 single-character commands, namely
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   463
\texttt{'>'}, \texttt{'<'}, \texttt{'+'}, \texttt{'-'}, \texttt{'.'},
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   464
\texttt{','}, \texttt{'['} and \texttt{']'}. Every other character is
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   465
considered a comment.  Brainf*** operates on memory cells containing
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   466
integers. For this it uses a single memory pointer that points at each
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   467
stage to one memory cell. This pointer can be moved forward by one
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   468
memory cell by using the command \texttt{'>'}, and backward by using
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   469
\texttt{'<'}. The commands \texttt{'+'} and \texttt{'-'} increase,
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   470
respectively decrease, by 1 the content of the memory cell to which
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   471
the memory pointer currently points to. The commands for input/output
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   472
are \texttt{','} and \texttt{'.'}. Output works by reading the content
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   473
of the memory cell to which the memory pointer points to and printing
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   474
it out as an ASCII character. Input works the other way, taking some
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   475
user input and storing it in the cell to which the memory pointer
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   476
points to. The commands \texttt{'['} and \texttt{']'} are looping
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   477
constructs. Everything in between \texttt{'['} and \texttt{']'} is
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   478
repeated until a counter (memory cell) reaches zero.  A typical
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   479
program in brainf*** looks as follows:
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   480
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   481
\begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   482
\begin{verbatim}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   483
 ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   484
 ..+++.>>.<-.<.+++.------.--------.>>+.>++.
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   485
\end{verbatim}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   486
\end{center}  
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   487
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   488
\noindent
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   489
This one prints out Hello World\ldots{}obviously. 
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   490
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   491
\subsubsection*{Tasks (file bf.scala)}
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   492
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   493
\begin{itemize}
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   494
\item[(2a)] Brainf*** memory is represented by a \texttt{Map} from
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   495
  integers to integers. The empty memory is represented by
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   496
  \texttt{Map()}, that is nothing is stored in the
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   497
  memory. \texttt{Map(0 -> 1, 2 -> 3)} clearly stores \texttt{1} at
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   498
  memory location \texttt{0}; at \texttt{2} it stores \texttt{3}. The
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   499
  convention is that if we query the memory at a location that is
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   500
  \emph{not} defined in the \texttt{Map}, we return \texttt{0}. Write
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   501
  a function, \texttt{sread}, that takes a memory (a \texttt{Map}) and
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   502
  a memory pointer (an \texttt{Int}) as argument, and safely reads the
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   503
  corresponding memory location. If the \texttt{Map} is not defined at
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   504
  the memory pointer, \texttt{sread} returns \texttt{0}.
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   505
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   506
  Write another function \texttt{write}, which takes a memory, a
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   507
  memory pointer and an integer value as argument and updates the
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   508
  \texttt{Map} with the value at the given memory location. As usual
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   509
  the \texttt{Map} is not updated `in-place' but a new map is created
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   510
  with the same data, except the value is stored at the given memory
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   511
  pointer.\hfill[1 Mark]
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   512
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   513
\item[(2b)] Write two functions, \texttt{jumpRight} and
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   514
  \texttt{jumpLeft} that are needed to implement the loop constructs
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   515
  of brainf***. They take a program (a \texttt{String}) and a program
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   516
  counter (an \texttt{Int}) as argument and move right (respectively
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   517
  left) in the string in order to find the \textbf{matching}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   518
  opening/closing bracket. For example, given the following program
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   519
  with the program counter indicated by an arrow:
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   520
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   521
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   522
  \texttt{--[\barbelow{.}.+>--],>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   523
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   524
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   525
  then the matching closing bracket is in 9th position (counting from 0) and
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   526
  \texttt{jumpRight} is supposed to return the position just after this
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   527
  
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   528
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   529
  \texttt{--[..+>--]\barbelow{,}>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   530
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   531
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   532
  meaning it jumps to after the loop. Similarly, if you are in 8th position
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   533
  then \texttt{jumpLeft} is supposed to jump to just after the opening
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   534
  bracket (that is jumping to the beginning of the loop):
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   535
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   536
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   537
    \texttt{--[..+>-\barbelow{-}],>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   538
    \qquad$\stackrel{\texttt{jumpLeft}}{\longrightarrow}$\qquad
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   539
    \texttt{--[\barbelow{.}.+>--],>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   540
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   541
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   542
  Unfortunately we have to take into account that there might be
157
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   543
  other opening and closing brackets on the `way' to find the
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   544
  matching bracket. For example in the brainf*** program
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   545
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   546
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   547
  \texttt{--[\barbelow{.}.[+>]--],>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   548
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   549
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   550
  we do not want to return the index for the \texttt{'-'} in the 9th
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   551
  position, but the program counter for \texttt{','} in 12th
157
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   552
  position. The easiest to find out whether a bracket is matched is by
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   553
  using levels (which are the third argument in \texttt{jumpLeft} and
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   554
  \texttt{jumpLeft}). In case of \texttt{jumpRight} you increase the
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   555
  level by one whenever you find an opening bracket and decrease by
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   556
  one for a closing bracket. Then in \texttt{jumpRight} you are looking
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   557
  for the closing bracket on level \texttt{0}. For \texttt{jumpLeft} you
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   558
  do the opposite. In this way you can find \textbf{matching} brackets
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   559
  in strings such as
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   560
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   561
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   562
  \texttt{--[\barbelow{.}.[[-]+>[.]]--],>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   563
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   564
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   565
  for which \texttt{jumpRight} should produce the position:
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   566
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   567
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   568
  \texttt{--[..[[-]+>[.]]--]\barbelow{,}>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   569
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   570
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   571
  It is also possible that the position returned by \texttt{jumpRight} or
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   572
  \texttt{jumpLeft} is outside the string in cases where there are
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   573
  no matching brackets. For example
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   574
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   575
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   576
  \texttt{--[\barbelow{.}.[[-]+>[.]]--,>,++}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   577
  \qquad$\stackrel{\texttt{jumpRight}}{\longrightarrow}$\qquad
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   578
  \texttt{--[..[[-]+>[.]]-->,++\barbelow{\;\phantom{+}}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   579
  \end{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   580
  \hfill[1 Mark]
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   581
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   582
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   583
\item[(2c)] Write a recursive function \texttt{run} that executes a
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   584
  brainf*** program. It takes a program, a program counter, a memory
157
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   585
  pointer and a memory as arguments. If the program counter is outside
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   586
  the program string, the execution stops and \texttt{run} returns the
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   587
  memory. If the program counter is inside the string, it reads the
157
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   588
  corresponding character and updates the program counter \texttt{pc},
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   589
  memory pointer \texttt{mp} and memory \texttt{mem} according to the
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   590
  rules shown in Figure~\ref{comms}. It then calls recursively
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   591
  \texttt{run} with the updated data.
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   592
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   593
  Write another function \texttt{start} that calls \texttt{run} with a
157
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   594
  given brainfu** program and memory, and the program counter and memory pointer
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   595
  set to~$0$. Like \texttt{run} it returns the memory after the execution
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   596
  of the program finishes. You can test your brainf**k interpreter with the
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   597
  Sierpinski triangle or the Hello world programs or have a look at
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   598
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   599
  \begin{center}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   600
  \url{https://esolangs.org/wiki/Brainfuck}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
   601
  \end{center}\hfill[2 Marks]
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   602
  
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   603
  \begin{figure}[p]
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   604
  \begin{center}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   605
    \begin{tabular}{|@{}p{0.8cm}|l|}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   606
      \hline
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   607
      \hfill\texttt{'>'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   608
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   609
                       $\bullet$ & $\texttt{mp} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   610
                       $\bullet$ & \texttt{mem} unchanged
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   611
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   612
      \hfill\texttt{'<'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   613
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   614
                       $\bullet$ & $\texttt{mp} - 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   615
                       $\bullet$ & \texttt{mem} unchanged
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   616
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   617
      \hfill\texttt{'+'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   618
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   619
                       $\bullet$ & $\texttt{mp}$ unchanged\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   620
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) + 1}\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   621
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   622
      \hfill\texttt{'-'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   623
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   624
                       $\bullet$ & $\texttt{mp}$ unchanged\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   625
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> mem(mp) - 1}\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   626
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   627
      \hfill\texttt{'.'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   628
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   629
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   630
                       $\bullet$ & print out \,\texttt{mem(mp)} as a character\\
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   631
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   632
      \hfill\texttt{','} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   633
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   634
                       $\bullet$ & $\texttt{mp}$ unchanged\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   635
                       $\bullet$ & \texttt{mem} updated with \texttt{mp -> \textrm{input}}\\
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   636
                       \multicolumn{2}{@{}l}{the input is given by \texttt{Console.in.read().toByte}}
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   637
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   638
      \hfill\texttt{'['} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   639
                       \multicolumn{2}{@{}l}{if \texttt{mem(mp) == 0} then}\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   640
                       $\bullet$ & $\texttt{pc = jumpRight(prog, pc + 1, 0)}$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   641
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   642
                       \multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) != 0} then}\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   643
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   644
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   645
                     \end{tabular}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   646
                     \\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   647
      \hfill\texttt{']'} & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   648
                       \multicolumn{2}{@{}l}{if \texttt{mem(mp) != 0} then}\\
159
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   649
                       $\bullet$ & $\texttt{pc = jumpLeft(prog, pc - 1, 0)}$\\
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   650
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\medskip\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   651
                       \multicolumn{2}{@{}l}{otherwise if \texttt{mem(mp) == 0} then}\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   652
                       $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   653
                       $\bullet$ & $\texttt{mp}$ and \texttt{mem} unchanged\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   654
                     \end{tabular}\\\hline   
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   655
      any other char & \begin{tabular}[t]{@{}l@{\hspace{2mm}}l@{}}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   656
                         $\bullet$ & $\texttt{pc} + 1$\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   657
                         $\bullet$ & \texttt{mp} and \texttt{mem} unchanged
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   658
                       \end{tabular}\\
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   659
      \hline                 
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   660
    \end{tabular}
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   661
  \end{center}
157
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   662
  \caption{The rules for how commands in the brainf*** language update the program counter \texttt{pc},
15f1fca879c5 updated
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   663
    memory pointer \texttt{mp} and memory \texttt{mem}.\label{comms}}
154
39c6b93718f0 updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   664
  \end{figure}
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   665
\end{itemize}\bigskip  
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   666
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   667
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   668
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   669
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
\end{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 62
diff changeset
   672
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
%%% End: