coursework/cw01.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 07 Oct 2013 08:52:03 +0100
changeset 128 44863a6b468a
parent 127 41ef073ac6c4
child 129 722d88a38b04
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{charter}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{hyperref}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{amssymb}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{amsmath}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
\section*{Coursework 1}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
This coursework is worth 3\% and is due on 12 November at 16:00. You are asked to implement 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
a regular expression matcher and submit a document containing the answers for the questions 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
below. You can do the implementation in any programming language you like, but you need 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
to submit the source code with which you answered the questions. However, the coursework 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
will \emph{only} be judged according to the answers only. \bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
The task is to implement a regular expression matcher based on derivatives. The implementation 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
should be able to deal with the usual regular expressions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
\varnothing, \epsilon, c, r_1 + r_2, r_1 \cdot r_2, r^*
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
but also with
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
\begin{tabular}{ll}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
$[c_1 c_2 \ldots c_n]$ & a range of characters\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
$r^+$ & one or more times $r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
$r^?$ & optional $r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
$r^{\{n,m\}}$ & at least $n$-times $r$ but no more than $m$-times\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
$\sim{}r$ & not-regular expression of $r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
In the case of $r^{\{n,m\}}$ we have the convention that $0 \le n \le m$.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
The meaning of these regular expressions is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
\begin{tabular}{r@{\hspace{2mm}}c@{\hspace{2mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
$L([c_1 c_2 \ldots c_n])$ & $\dn$ & $\{"c_1", "c_2", \ldots, "c_n"\}$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
$L(r^+)$            & $\dn$ & $\bigcup_{1\le i}. L(r)^i$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
$L(r^?)$            & $\dn$ & $L(r) \cup \{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
$L(r^{\{n,m\}})$ & $\dn$ & $\bigcup_{n\le i \le m}. L(r)^i$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
$L(\sim{}r)$       & $\dn$ & $UNIV - L(r)$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
whereby in the last clause the set $UNIV$ stands for the set of \emph{all} strings.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
So $\sim{}r$ means `all the strings that $r$ cannot match'. We assume ranges 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
like $[a\mbox{-}z0\mbox{-}9]$ are a shorthand for the regular expression
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
[a b c d\ldots z 0 1\ldots 9]\;.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
Be careful that your implementations for $nullable$ and $der$ satisfies for every $r$ the following two
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
properties:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
\begin{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
\item $nullable(r)$ if and only if $""\in L(r)$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
\item $L(der\,c\,r)) = Der\,c\,(L(r))$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
\end{itemize}
128
44863a6b468a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
    70
\newpage
127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
\subsection*{Question 1 (unmarked)}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
What is your King's email address (you will need it in the questions below)?\bigskip 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
\subsection*{Question 2 (marked with 1\%)}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
Implement the following regular expression for email addresses
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
([a\mbox{-}z0\mbox{-}9\_\!\_\,.-]^+)\cdot @\cdot ([a\mbox{-}z0\mbox{-}9\,.-]^+)\cdot .\cdot ([a\mbox{-}z\,.]^{\{2,6\}})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
and calculate the derivative according to your email address. When calculating
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
the derivative, simplify all regular expressions as much as possible, but at least apply the following 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
six rules:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
\begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
$r \cdot \varnothing$ & $\mapsto$ & $\varnothing$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
$\varnothing \cdot r$ & $\mapsto$ & $\varnothing$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
$r \cdot \epsilon$ & $\mapsto$ & $r$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
$\epsilon \cdot r$ & $\mapsto$ & $r$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
$r + \varnothing$ & $\mapsto$ & $r$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
$\varnothing + r$ & $\mapsto$ & $r$\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
Write down your simplified derivative.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
\subsection*{Question 3 (marked with 1\%)}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
Consider the regular expression $/ \cdot * \cdot (\sim{}([a\mbox{-}z]^* \cdot * \cdot / \cdot [a\mbox{-}z]^*)) \cdot * \cdot /$ and decide
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
wether the following four strings are matched by this regular expression. Answer yes or no.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
\begin{enumerate}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
\item "/**/"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
\item "/*foobar*/"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
\item "/*test*/test*/"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
\item "/*test/*test*/"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
\end{enumerate}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
\subsection*{Question 4 (marked with 1\%)}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
Let $r_1$ be the regular expression $a\cdot a\cdot a$ and $r_2$ be $(a^{\{19,19\}}) \cdot (a^?)$.
128
44863a6b468a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
   118
Decide whether the following three strings consisting of $a$s only can be matched by $(r_1^+)^+$. 
44863a6b468a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
   119
Similarly test them with $(r_2^+)^+$. Again answer in all six cases with yes or no. Be careful when 
44863a6b468a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
   120
copy-and-pasting the strings so as to not forgetting any $a$ and to not introducing any
44863a6b468a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 127
diff changeset
   121
other character.
127
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
\begin{enumerate}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
\item $"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
\item $"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
\item$"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
\end{enumerate}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
%%% End: