handouts/ho01.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 01 Dec 2013 10:17:17 +0000
changeset 217 cd6066f1056a
parent 123 a75f9c9d8f94
child 237 370c0647a9bf
permissions -rw-r--r--
updated handouts
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}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{hyperref}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{amssymb}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{amsmath}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage[T1]{fontenc}
217
cd6066f1056a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 123
diff changeset
     6
\usepackage{../langs}
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
     8
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
     9
112
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
    10
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
\begin{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
\section*{Handout 1}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    15
This course is about the processing of strings. Lets start with what we mean by \emph{strings}. Strings
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    16
(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
    17
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
    18
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
    19
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
    20
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    21
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
    22
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
    23
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
    24
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
[\text{\it h, e, l, l, o}]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
\]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    30
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
    31
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
    32
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
    33
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
    34
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
    35
gives {\it "foobar"}.
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    37
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
    38
is
105
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
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
\{\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
    42
\]
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
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    45
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
    46
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
    47
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
\{\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
    50
\]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
\noindent
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
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
    54
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
    55
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
    56
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
    57
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
    58
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
    59
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
    60
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
    61
the latter has one element.
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    62
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    63
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
    64
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
    65
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
    66
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
    67
regular expression\footnote{See \url{http://goo.gl/5LoVX7}}
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
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    70
([\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
    71
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    73
\noindent
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
    74
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
    75
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
    76
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
    77
that every string is an email address. For example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    78
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    79
\[
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
    80
"\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
    81
\]
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    83
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    84
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
    85
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
    86
lower-case.)
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    87
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    88
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    89
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
    90
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
    91
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
    92
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
    93
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    94
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    95
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
    96
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    97
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    98
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    99
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
   100
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
   101
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
   102
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   103
We also need to define
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   104
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
   105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   106
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   107
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   108
$A^0$ & $\dn$ & $\{[\,]\}$ \\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   109
$A^{n+1}$ & $\dn$ & $A @ A^n$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   110
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   111
\end{center}
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   114
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
   115
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
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   117
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   118
A^* \dn \bigcup_{0\le n} A^n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   119
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   121
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   122
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
   123
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
   124
have 
108
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
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   127
A^* = \{"", "a", "aa", "aaa", \ldots\}
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   130
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   131
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
   132
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   133
\begin{center}
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   134
\begin{tabular}{@{}ccc@{}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   135
\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
   136
$A \cup \varnothing$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   137
$A \cup A$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   138
$A \cup B$ & $=$ & $B \cup A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   139
\end{tabular} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   140
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   141
$A @ B$ & $\not =$ & $B @ A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   142
$A  @ \varnothing$ & $=$ & $\varnothing @ A = \varnothing$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   143
$A  @ \{""\}$ & $=$ & $\{""\} @ A = A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   144
\end{tabular} &
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   145
\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
   146
$\varnothing^*$ & $=$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   147
$\{""\}^*$ & $=$ & $\{""\}$\\
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   148
$A^\star$ & $=$ & $\{""\} \cup A\cdot A^*$\\
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   149
\end{tabular} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   150
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   151
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   152
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   153
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   154
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   155
\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
   156
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
   157
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
   158
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
   159
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   160
Regular expressions are given by the following grammar:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   161
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   162
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   163
\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
   164
  $r$ & $::=$ &   $\varnothing$         & null\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   165
        & $\mid$ & $\epsilon$              & empty string / "" / []\\
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   166
        & $\mid$ & $c$                         & single character\\
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   167
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   168
        & $\mid$ & $r_1 + r_2$            & alternative / choice\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   169
        & $\mid$ & $r^*$                      & star (zero or more)\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   170
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   171
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   173
\noindent
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   174
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
   175
$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
   176
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
   177
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
   178
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
   179
$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
   180
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
   181
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
   182
$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
   183
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
   184
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   185
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   186
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   187
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   188
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   189
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   190
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
   191
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
   192
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
   193
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   194
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   195
{\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
   196
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   197
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   198
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   199
but often just write {\it hello}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   200
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   201
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
   202
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
   203
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
   204
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
   205
as they can be seen as shorthand for
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   206
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   207
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   208
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   209
$r^+$ & $\mapsto$ & $r\cdot r^*$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   210
$r^?$ & $\mapsto$ & $\epsilon + r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   211
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   212
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   213
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   214
\end{center}
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   215
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   216
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   217
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
   218
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
   219
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
   220
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
   221
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
   222
$\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
   223
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   224
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
   225
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
   226
that is supposed to be matched by this
111
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 110
diff changeset
   227
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
   228
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   229
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   230
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   231
$L(\varnothing)$  & $\dn$ & $\{\,\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   232
$L(\epsilon)$       & $\dn$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   233
$L(c)$                  & $\dn$ & $\{"c"\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   234
$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
   235
$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
   236
$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
   237
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   238
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   239
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   240
\noindent
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   241
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
   242
${\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
   243
$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
   244
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
   245
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
   246
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
   247
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
   248
123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 113
diff changeset
   249
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
   250
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
   251
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
   252
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
   253
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   254
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   255
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler1.scala}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   256
\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
   257
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
   258
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
   259
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   260
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   261
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   262
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler2.scala}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   263
\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
   264
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
   265
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
   266
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   267
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   268
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   269
\begin{figure}[p]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   270
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler3.scala}}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   271
\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
   272
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
   273
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
   274
\end{figure}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 111
diff changeset
   275
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
\end{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
%%% Local Variables: 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
%%% mode: latex
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
%%% TeX-master: t
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
%%% End: