handouts/ho01.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 27 Sep 2013 10:53:29 +0100
changeset 110 9353308f1c6a
parent 108 52ee218151f9
child 111 1933e88cb73e
permissions -rw-r--r--
updated
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{charter}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{hyperref}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{amssymb}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{amsmath}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\usepackage[T1]{fontenc}
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
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
\begin{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
\section*{Handout 1}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    14
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
    15
(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
    16
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
    17
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
    18
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
    19
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    20
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
    21
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
    22
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
    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
[\text{\it h, e, l, l, o}]
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
\]
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
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    29
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
    30
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
    31
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
    32
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
    33
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
    34
gives {\it "foobar"}.
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    36
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
    37
is
105
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
\{\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
    41
\]
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
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    44
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
    45
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
    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
\{\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
    49
\]
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
\noindent
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
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
    53
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
    54
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
    55
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
    56
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
    57
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
    58
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
    59
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
    60
the latter has one element.
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    61
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    62
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
    63
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
    64
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
    65
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
    66
regular expression\footnote{See \url{http://goo.gl/5LoVX7}}
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
([\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
    70
\]
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
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    73
The reason is that for example before the $@$-sign there can be any string you want assuming it 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    74
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
    75
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
    76
that every string is an email address. For example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    77
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
\text{\it foo}@\text{\it bar}.\text{\it c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    80
\]
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    82
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
    83
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
    84
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
    85
lower-case.)
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    86
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    87
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    88
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
    89
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
    90
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
    91
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
    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
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
    95
\]
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    98
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
    99
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
   100
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
   101
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   102
We also need to define
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   103
the power of a set, 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
   104
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   105
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   106
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   107
$A^0$ & $\dn$ & $\{[\,]\}$ \\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   108
$A^{n+1}$ & $\dn$ & $A @ A^n$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   109
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   110
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   111
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   112
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   113
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
   114
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
   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
A^* \dn \bigcup_{0\le n} A^n
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   118
\]
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
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   121
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
   122
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
   123
have 
108
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
A^* = \{"", "a", "aa", "aaa", \ldots\}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   127
\]
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
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   130
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
   131
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   132
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   133
\begin{tabular}{ccc}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   134
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   135
$A \cup \varnothing$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   136
$A \cup A$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   137
$A \cup B$ & $=$ & $B \cup A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   138
\end{tabular} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   139
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   140
$A @ B$ & $\not =$ & $B @ A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   141
$A  @ \varnothing$ & $=$ & $\varnothing @ A = \varnothing$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   142
$A  @ \{""\}$ & $=$ & $\{""\} @ A = A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   143
\end{tabular} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   144
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   145
$\varnothing^*$ & $=$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   146
$\{""\}^*$ & $=$ & $\{""\}$\\
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   147
$A^\star$ & $=$ & $\{""\} \cup A\cdot A^*$\\
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   148
\end{tabular} 
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{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   151
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   152
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   153
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   154
\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
   155
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
   156
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
   157
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
   158
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   159
Regular expressions are given by the following grammar:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   160
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   161
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   162
\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
   163
  $r$ & $::=$ &   $\varnothing$         & null\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   164
        & $\mid$ & $\epsilon$              & empty string / "" / []\\
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   165
        & $\mid$ & $c$                         & single character\\
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   166
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   167
        & $\mid$ & $r_1 + r_2$            & alternative / choice\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   168
        & $\mid$ & $r^*$                      & star (zero or more)\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   169
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   170
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   171
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   172
\noindent
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   173
Because we overload our notation there are some subtleties you should be aware of. The letter $c$ stands for any character from the
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   174
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
   175
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
   176
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
   177
$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
   178
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
   179
respectively. The reasons for this will become clear shortly. In the literature you will often find that the choice
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   180
$r_1 + r_2$  is written as $r_1\mid{}r_2$. Also following the convention in the literature, we will in case of $\cdot$ even 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   181
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
   182
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
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   187
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   188
meaning first comes a name (specified by the regular expression $([\ldots])^+$), then an $@$-sign, then
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   189
a domain name (specified by the regular expression $([\ldots])^+$), then a top-level domain. Similarly if
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   190
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
   191
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
{\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
   194
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   195
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   196
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   197
but often just write {\it hello}.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   198
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   199
Another source of confusion might arise from the fact that we use the term \emph{regular expressions} for the ones used in ``theory''
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   200
and also the ones in ``practice''. In this course we refer by default to the regular expressions defined by the grammar above. 
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   201
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
   202
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
   203
as they can be seen as shorthand for
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   204
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   205
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   206
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   207
$r^+$ & $\mapsto$ & $r\cdot r^*$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   208
$r^?$ & $\mapsto$ & $\epsilon + r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   209
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   210
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   211
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   212
\end{center}
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   213
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   214
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   215
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
   216
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
   217
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
   218
these kind of not-regular-expressions is. Try to write down the regular expression which can match any
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   219
string except {\it "hello"} and {\it "world"}. It is possible in principle, but often it is easier to just include
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   220
$\sim{}r$ in the definition or 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
   221
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   222
So far we have only considered informally what the \emph{meaning} of a regular expression is.  
110
9353308f1c6a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 108
diff changeset
   223
Formally, we associate with every regular expression a set of strings which are matched by this
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   224
regular expression. This can be formally defined as 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   225
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   226
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   227
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   228
$L(\varnothing)$  & $\dn$ & $\{\,\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   229
$L(\epsilon)$       & $\dn$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   230
$L(c)$                  & $\dn$ & $\{"c"\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   231
$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
   232
$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
   233
$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
   234
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   235
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   236
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   237
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   238
This means we can now precisely state what the meaning, for example, of the regular expression 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   239
${\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}$ is, namely 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   240
$L({\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}) = \{\text{\it"hello"}\}$. Similarly if we have the choice
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   241
$a + b$, the meaning is $L(a + b) = \{\text{\it"a"}, \text{\it"b"}\}$, namely the only two strings which can possibly
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   242
be matched by this choice. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   243
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   244
The point of this definition is that we can now precisely specify when a string is matched by a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   245
regular expression, namely a string, say $s$, is matched by a regular expression, say $r$, if
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   246
and only if $s \in L(r)$. In fact we will write a program {\it match} that takes any string $s$ and
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.
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
\end{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
%%% Local Variables: 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
%%% mode: latex
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
%%% TeX-master: t
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
%%% End: