handouts/ho01.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 03 Sep 2014 11:01:49 +0100
changeset 237 370c0647a9bf
parent 217 cd6066f1056a
child 242 35104ee14f87
permissions -rw-r--r--
more material
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
237
370c0647a9bf more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
     2
\usepackage{../style}
217
cd6066f1056a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
     3
\usepackage{../langs}
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
     5
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
     6
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\begin{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
\section*{Handout 1}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
237
370c0647a9bf more material
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
    11
This module is about the processing of strings. Lets start with what we mean by \emph{strings}. Strings
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    12
(they are also sometimes referred to as \emph{words}) are lists of characters drawn from an \emph{alphabet}. 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    13
If nothing else is specified, we usually assume 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    14
the alphabet consists of just the lower-case letters $a$, $b$, \ldots, $z$. Sometimes, however, we explicitly
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    15
restrict strings to contain, for example, only the letters $a$ and $b$. In this case we say the alphabet is the set $\{a, b\}$.
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    17
There are many ways how we can write down strings. In programming languages, they are usually 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    18
written as {\it "hello"} where the double quotes indicate that we dealing with a string. 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    19
Essentially, strings are lists of characters which can be written for example as follows
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
[\text{\it h, e, l, l, o}]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
\]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    26
The important point is that we can always decompose strings. For example, we will often consider the
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    27
first character of a string, say $h$, and the ``rest''  of a string say {\it "ello"} when making definitions 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    28
about strings. There are some subtleties with the empty string, sometimes written as {\it ""} but also as 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    29
the empty list of characters $[\,]$. Two strings, for example $s_1$ and $s_2$, can be \emph{concatenated}, 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    30
which we write as $s_1 @ s_2$. Suppose we are given two strings {\it "foo"} and {\it "bar"}, then their concatenation
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    31
gives {\it "foobar"}.
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    33
We often need to talk about sets of strings. For example the set of all strings over the alphabet $\{a, \ldots\, z\}$
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    34
is
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
\{\text{\it "", "a", "b", "c",\ldots,"z", "aa", "ab", "ac", \ldots, "aaa", \ldots}\}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
\]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    41
Any set of strings, not just the set-of-all-strings, is often called a \emph{language}. The idea behind
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    42
this choice of terminology is that if we enumerate, say, all words/strings from a dictionary, like 
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
\{\text{\it "the", "of", "milk", "name", "antidisestablishmentarianism", \ldots}\}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
\]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
\noindent
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
then we have essentially described the English language, or more precisely all
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
strings that can be used in a sentence of the English language. French would be a
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    51
different set of strings, and so on. In the context of this course, a language might 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    52
not necessarily make sense from a natural language point of view. For example
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    53
the set of all strings shown above is a language, as is the empty set (of strings). The
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
empty set of strings is often written as $\varnothing$ or $\{\,\}$. Note that there is a 
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    55
difference between the empty set, or empty language, and the set that 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    56
contains only the empty string $\{\text{""}\}$: the former has no elements, whereas 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    57
the latter has one element.
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    58
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    59
As seen, there are languages which contain infinitely many strings, like the set of all strings.
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    60
The ``natural'' languages like English, French and so on contain many but only finitely many 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    61
strings (namely the ones listed in a good dictionary). It might be therefore be surprising that the
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    62
language consisting of all email addresses is infinite provided we assume it is defined by the
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    63
regular expression\footnote{See \url{http://goo.gl/5LoVX7}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    64
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    65
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    66
([\text{\it{}a-z0-9\_.-}]^+)@([\text{\it a-z0-9.-}]^+).([\text{\it a-z.}]^{\{2,6\}})
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    67
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    68
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    69
\noindent
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
    70
One reason is that before the $@$-sign there can be any string you want assuming it 
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    71
is made up from letters, digits, underscores, dots and hyphens---clearly there are infinitely many
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    72
of those. Similarly the string after the $@$-sign can be any string. However, this does not mean 
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    73
that every string is an email address. For example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    75
\[
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
    76
"\text{\it foo}@\text{\it bar}.\text{\it c}"
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    77
\]
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    79
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    80
is not, because the top-level-domains must be of length of at least two. (Note that there is
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    81
the convention that uppercase letters are treated in email-addresses as if they were
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    82
lower-case.)
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    83
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    84
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    85
Before we expand on the topic of regular expressions, let us review some operations on
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    86
sets. We will use capital letters $A$, $B$, $\ldots$ to stand for sets of strings. 
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    87
The union of two sets is written as usual as $A \cup B$. We also need to define the
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    88
operation of \emph{concatenating} two sets of strings. This can be defined as
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    89
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    90
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    91
A @ B \dn \{s_1@ s_2 | s_1 \in A \wedge s_2 \in B \}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    92
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    93
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    94
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    95
which essentially means take the first string from the set $A$ and concatenate it with every
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    96
string in the set $B$, then take the second string from $A$ do the same and so on. You might
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    97
like to think about what this definition means in case $A$ or $B$ is the empty set.
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    98
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    99
We also need to define
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   100
the power of a set of strings, written as $A^n$ with $n$ being a natural number. This is defined inductively as follows
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   102
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   103
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   104
$A^0$ & $\dn$ & $\{[\,]\}$ \\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   105
$A^{n+1}$ & $\dn$ & $A @ A^n$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   106
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   107
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   109
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   110
Finally we need the \emph{star} of a set of strings, written $A^*$. This is defined as the union
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   111
of every power of $A^n$ with $n\ge 0$. The mathematical notation for this operation is
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   113
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   114
A^* \dn \bigcup_{0\le n} A^n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   115
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   117
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   118
This definition implies that the star of a set $A$ contains always the empty string (that is $A^0$), one 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   119
copy of every string in $A$ (that is $A^1$), two copies in $A$ (that is $A^2$) and so on. In case $A=\{"a"\}$ we therefore 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   120
have 
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   121
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   122
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   123
A^* = \{"", "a", "aa", "aaa", \ldots\}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   124
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   125
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   126
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   127
Be aware that these operations sometimes have quite non-intuitive properties, for example
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   128
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   129
\begin{center}
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   130
\begin{tabular}{@{}ccc@{}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   131
\begin{tabular}{@{}r@{\hspace{1mm}}c@{\hspace{1mm}}l}
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   132
$A \cup \varnothing$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   133
$A \cup A$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   134
$A \cup B$ & $=$ & $B \cup A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   135
\end{tabular} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   136
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   137
$A @ B$ & $\not =$ & $B @ A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   138
$A  @ \varnothing$ & $=$ & $\varnothing @ A = \varnothing$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   139
$A  @ \{""\}$ & $=$ & $\{""\} @ A = A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   140
\end{tabular} &
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   141
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l@{}}
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   142
$\varnothing^*$ & $=$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   143
$\{""\}^*$ & $=$ & $\{""\}$\\
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   144
$A^\star$ & $=$ & $\{""\} \cup A\cdot A^*$\\
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   145
\end{tabular} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   146
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   147
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   148
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   149
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   150
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   151
\emph{Regular expressions} are meant to conveniently describe languages...at least languages
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   152
we are interested in in Computer Science.  For example there is no convenient regular expression
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   153
for describing the English language short of enumerating all English words. 
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   154
But they seem useful for describing all permitted email addresses, as seen above. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   155
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   156
Regular expressions are given by the following grammar:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   157
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   158
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   159
\begin{tabular}{r@{\hspace{1mm}}r@{\hspace{1mm}}l@{\hspace{13mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   160
  $r$ & $::=$ &   $\varnothing$         & null\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   161
        & $\mid$ & $\epsilon$              & empty string / "" / []\\
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   162
        & $\mid$ & $c$                         & single character\\
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   163
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   164
        & $\mid$ & $r_1 + r_2$            & alternative / choice\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   165
        & $\mid$ & $r^*$                      & star (zero or more)\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   166
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   167
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   168
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   169
\noindent
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   170
Because we overload our notation, there are some subtleties you should be aware of. First, the letter 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   171
$c$ stands for any character from the
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   172
alphabet at hand. Second, we will use parentheses to disambiguate
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   173
regular expressions. For example we will write $(r_1 + r_2)^*$, which is different from, say $r_1 + (r_2)^*$.
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   174
The former means roughly zero or more times $r_1$ or $r_2$, while the latter means $r_1$ or zero or more times
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   175
$r_2$. We should also write $(r_1 + r_2) + r_3$, which is different from the regular expression $r_1 + (r_2 + r_3)$,
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   176
but in case of $+$ and $\cdot$ we actually do not care about the order and just write $r_1 + r_2 + r_3$, or $r_1 \cdot r_2 \cdot r_3$,
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   177
respectively. The reasons for this will become clear shortly. In the literature you will often find that the choice
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   178
$r_1 + r_2$  is written as $r_1\mid{}r_2$ or $r_1\mid\mid{}r_2$. Also following the convention in the literature, we will in case of $\cdot$ even 
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   179
often omit it all together. For example the regular expression for email addresses shown above is meant to be of the form
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   180
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   181
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   182
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   183
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   184
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   185
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   186
meaning first comes a name (specified by the regular expression $([\ldots])^+$), then an $@$-sign, then
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   187
a domain name (specified by the regular expression $([\ldots])^+$), then a dot and then a top-level domain. Similarly if
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   188
we want to specify the regular expression for the string {\it "hello"} we should write
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   189
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   190
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   191
{\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   192
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   194
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   195
but often just write {\it hello}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   196
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   197
Another source of confusion might arise from the fact that we use the term \emph{regular expression} for the regular expressions used in ``theory''
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   198
and also the ones used in ``practice''. In this course we refer by default to the regular expressions defined by the grammar above. 
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   199
In ``practice'' we often use $r^+$ to stand for one or more times, $\backslash{}d$ to stand for a digit, $r^?$ to stand for an optional regular
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   200
expression, or ranges such as $[\text{\it a - z}]$ to stand for any lower case letter from $a$ to $z$. They are however mere convenience 
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   201
as they can be seen as shorthand for
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   202
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   203
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   204
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   205
$r^+$ & $\mapsto$ & $r\cdot r^*$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   206
$r^?$ & $\mapsto$ & $\epsilon + r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   207
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   208
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   209
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   210
\end{center}
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   211
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   212
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   213
We will see later that the \emph{not}-regular-expression can also be seen as convenience. This regular
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   214
expression is supposed to stand for every string \emph{not} matched by a regular expression. We will write
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   215
such not-regular-expressions as $\sim{}r$. While being ``convenience'' it is often not so clear what the shorthand for
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   216
these kind of not-regular-expressions is. Try to write down the regular expression which can match any
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   217
string except the two strings {\it "hello"} and {\it "world"}. It is possible in principle, but often it is easier to just include
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   218
$\sim{}r$ in the definition of regular expressions. Whenever we do so, we will state it explicitly.
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   219
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   220
So far we have only considered informally what the \emph{meaning} of a regular expression is.  
111
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 110
diff changeset
   221
To do so more formally we will associate with every regular expression a set of strings 
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   222
that is supposed to be matched by this
111
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 110
diff changeset
   223
regular expression. This can be defined recursively  as follows
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   225
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   226
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   227
$L(\varnothing)$  & $\dn$ & $\{\,\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   228
$L(\epsilon)$       & $\dn$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   229
$L(c)$                  & $\dn$ & $\{"c"\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   230
$L(r_1+ r_2)$      & $\dn$ & $L(r_1) \cup L(r_2)$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   231
$L(r_1 \cdot r_2)$  & $\dn$ & $\{s_1@ s_2 | s_1 \in L(r_1) \wedge s_2 \in L(r_2) \}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   232
$L(r^*)$                   & $\dn$ & $\bigcup_{n \ge 0} L(r)^n$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   233
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   234
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   235
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   236
\noindent
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   237
As a result we can now precisely state what the meaning, for example, of the regular expression 
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   238
${\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}$ is, namely 
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   239
$L({\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}) = \{\text{\it"hello"}\}$...as expected. Similarly if we have the 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   240
choice-regular-expression $a + b$, its meaning is $L(a + b) = \{\text{\it"a"}, \text{\it"b"}\}$, namely the only two strings which can possibly
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   241
be matched by this choice. You can now also see why we do not make a difference
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   242
between the different regular expressions $(r_1 + r_2) + r_3$ and $r_1 + (r_2 + r_3)$....they 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   243
are not the same regular expression, but have the same meaning. 
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   244
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   245
The point of the definition of $L$ is that we can use it to precisely specify when a string $s$ is matched by a
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   246
regular expression $r$, namely only when $s \in L(r)$. In fact we will write a program {\it match} that takes any string $s$ and
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   247
any regular expression $r$ as argument and returns \emph{yes}, if $s \in L(r)$ and \emph{no},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   248
if $s \not\in L(r)$. We leave this for the next lecture.
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   249
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   250
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   251
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler1.scala}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   252
\caption{Scala code for a web-crawler that can detect broken links in a web-page. It uses
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   253
the regular expression {\tt http\_pattern} in Line~15 for recognising URL-addresses. It finds
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   254
all links using the library function {\tt findAllIn} in Line~21.}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   255
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   256
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   257
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   258
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler2.scala}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   259
\caption{A version of the web-crawler which only follows links in ``my'' domain---since these are the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   260
ones I am interested in to fix. It uses the regular expression {\tt my\_urls} in Line~16.
113
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
   261
The main change is in Line~26 where there is a test whether URL is in ``my'' domain or not.}
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   262
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   263
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   264
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   265
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   266
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler3.scala}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   267
\caption{A small email harvester---whenever we download a web-page, we also check whether
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   268
it contains any email addresses. For this we use the regular expression {\tt email\_pattern} in
113
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 112
diff changeset
   269
Line~17.  The main change is in Lines 33 and 34 where all email addresses that can be found in a page are printed.}
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   270
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   271
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
\end{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
%%% Local Variables: 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
%%% mode: latex
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
%%% TeX-master: t
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
%%% End: