handouts/ho01.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Sep 2013 15:11:25 +0100
changeset 108 52ee218151f9
parent 107 1bdec6a9e03d
child 110 9353308f1c6a
permissions -rw-r--r--
added
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
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
This course is about processing of strings. Lets start with what we mean by \emph{string}. Strings
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
are lists of characters drawn from an \emph{alphabet}. If nothing else is specified, we usually assume 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
the alphabet are letters $a$, $b$, \ldots, $z$ and $A$, $B$, \ldots $Z$. Sometimes we explicitly
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
restrict strings to only contain the letters $a$ and $b$. Then we say the alphabet is the set $\{a, b\}$.
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
There are many ways how we write string. Since they are lists of characters we might write
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
them as {\it "hello"} being enclosed by double quotes. This is a short-hand for the list
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
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
[\text{\it h, e, l, l, o}]
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
\noindent
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
The important point is that we can always decompose strings. For example we often consider the
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
first character of a string, say $h$, and the ``rest''  of a string {\it "ello"}. 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
There are also some subtleties with the empty string, sometimes written as {\it ""} or as the empty list
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    30
of characters $[\,]$. Two strings, say $s_1$ and $s_2$, can be \emph{concatenated} which is written
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    31
as $s_1 @ s_2$. For example if we have the two strings {\it "foo"} and {\it "bar"}, their concatenation
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    32
gives {\it "foobar"}.
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
We often need to talk about sets of strings. For example the set of all strings
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
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
Any set of strings, not just the set of all strings, is often called a \emph{language}. The idea behind
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
this choice is that if we enumerate, say, all words/strings from a dictionary, like 
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
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
different set of string, and so on. In the context of this course, a language might 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
not necessarily make sense from a natural language perspective. For example
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
the set of all strings from above is a language, as is the empty set (of strings). The
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 
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    55
difference between the empty set, or empty language, and the set, or language, that 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    56
contains the empty string $\{\text{""}\}$: the former has no elements, the latter has one 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    57
element.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    58
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    59
As seen there are languages which contain infinitely many strings, like the set of all strings.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    60
The ``natural'' languages English, French and so on contain many but only finitely many 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    61
strings (the ones listed in a good dictionary). It might be therefore surprising that the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    62
language consisting of all email addresses is infinite if we assume it is defined by the
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    70
The reason is that for example before the $@$-sign there can be any string you want if it 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    71
is made up from letters, digits, underscore, dot and hyphen---there are infinitely many
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    72
of those. Similarly the string after the $@$-sign can be any string. This does not mean 
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
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    76
\text{\it foo}@\text{\it bar}.\text{\it c}
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    80
is not, since the top-level-domains must be of length of at least two. Note that there is
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    82
lower-case.
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    86
sets. We will use capital letters, such as $A$, $B$ and so on, to stand for sets of strings. 
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    88
\emph{concatenation} of two sets (of strings). This can be defined as
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    96
string in the set $B$, then take the second string from $A$ and so on. We also need to define
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    97
the power of a set, written as $A^n$. This is defined inductively as follows
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    98
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
    99
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   100
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   101
$A^0$ & $\dn$ & $\{[\,]\}$ \\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   102
$A^{n+1}$ & $\dn$ & $A @ A^n$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   103
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   104
\end{center}
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   107
Finally we need the \emph{star} of a set of strings, written $A^*$. This is defined as the union
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   108
of every power of $A^n$ for every $n\ge 0$. The mathematical notation for this operation is
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   109
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   110
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   111
A^* \dn \bigcup_{0\le n} A^n
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   115
This means the star of a set $A$ contains always the empty string, one copy of every string in $A$, two
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   116
copies and so on. In case $A=\{"a\}$ we have 
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
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   119
A^* = \{"", "a", "aa", "aaa", \ldots\}
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   122
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   123
Be aware that these operations sometimes have non-intuitive properties, for example
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
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   126
\begin{tabular}{ccc}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   127
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   128
$A \cup \varnothing$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   129
$A \cup A$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   130
$A \cup B$ & $=$ & $B \cup A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   131
\end{tabular} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   132
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   133
$A @ B$ & $\not =$ & $B @ A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   134
$A  @ \varnothing$ & $=$ & $\varnothing @ A = \varnothing$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   135
$A  @ \{""\}$ & $=$ & $\{""\} @ A = A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   136
\end{tabular} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   137
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   138
$\varnothing^*$ & $=$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   139
$\{""\}^*$ & $=$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   140
$A^1$ & $=$ & $A$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   141
\end{tabular} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   142
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   143
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   144
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   145
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   146
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   147
\emph{Regular expressions} are meant for conveniently describing languages...at least languages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   148
we are interested in in Computer Science.  For example there is no convenient regular expression
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   149
for describing the English language short of enumerating all English words/strings like in a dictionary. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   150
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
   151
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   152
Regular expressions are given by the following grammar:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   153
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   154
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   155
\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
   156
  $r$ & $::=$ &   $\varnothing$         & null\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   157
        & $\mid$ & $\epsilon$              & empty string / "" / []\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   158
        & $\mid$ & $c$                         & character\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   159
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   160
        & $\mid$ & $r_1 + r_2$            & alternative / choice\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   161
        & $\mid$ & $r^*$                      & star (zero or more)\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   162
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   163
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   164
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   165
\noindent
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   166
There are some subtleties you should be aware of. The letter $c$ stands for any character from the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   167
alphabet. Second, we will use parentheses to disambiguate
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   168
regular expressions. For example we will write $(r_1 + r_2)^*$, which is different from $r_1 + (r_2)^*$.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   169
The former means roughly zero or more times $r_1$ or $r_2$, while the latter means $r_1$ or zero or more times
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   170
$r_2$. We should also write $(r_1 + r_2) + r_3$ which is a regular expression different from $r_1 + (r_2 + r_3)$,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   171
but in case of $+$ and $\cdot$ we actually do not care and just write $r_1 + r_2 + r_3$, or $r_1 \cdot r_2 \cdot r_3$,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   172
respectively. The reasons for this will become clear shortly. In the literature you will often find that the choice
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   173
$r_1 + r_2$  is written as $r_1\mid{}r_2$. In case of $\cdot$ we will even often omit it all together. For example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   174
the regular expression for email addresses is meant to be of the form
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   175
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   176
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   177
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   178
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   179
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   180
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   181
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
   182
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
   183
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
   184
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   185
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   186
{\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
   187
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   188
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   189
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   190
but often just write {\it hello}.
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
Another source of confusion might be that we use the term \emph{regular expressions} for the ones used in ``theory''
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   193
and the ones in ``practice''. In this course by default we refer to the regular expressions defined by the grammar above. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   194
In ``practice'' we often use $r^+$ for one or more times, $\backslash{}d$ to stand for a digit, $r^?$ for an optional regular
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   195
expression, or ranges such as $[\text{\it a - z}]$ to stand for any lower case letter from $a$ to $z$. They are mere convenience 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   196
as they can be seen as shorthand for
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
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   199
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   200
$r^+$ & $\mapsto$ & $r\cdot r^*$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   201
$r^?$ & $\mapsto$ & $\epsilon + r$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   202
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   203
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   204
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   205
\end{center}
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   206
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   207
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   208
We will see later that the \emph{not}-regular-expression can also be seen as convenience. This regular
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   209
expression is supposed to stand for every string \emph{not} match by a regular expression. We will write
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   210
such regular expressions as $~r$. While being ``convenience'' it is often not so clear what the shorthand for
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   211
these kind of regular expressions is.
107
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 106
diff changeset
   212
108
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   213
So far we have only considered informally what the \emph{meaning} of a regular expression is.  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   214
Formally we associate with every regular expression a set of strings which are matched by this
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   215
regular expression. This can be formally defined as 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   216
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   217
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   218
\begin{tabular}{rcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   219
$L(\varnothing)$  & $\dn$ & $\{\,\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   220
$L(\epsilon)$       & $\dn$ & $\{""\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   221
$L(c)$                  & $\dn$ & $\{"c"\}$\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   222
$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
   223
$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
   224
$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
   225
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   226
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   227
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   228
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   229
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
   230
${\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
   231
$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
   232
$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
   233
be matched by this choice. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   234
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 107
diff changeset
   235
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
   236
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
   237
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
   238
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
   239
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
   240
\end{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
%%% Local Variables: 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
%%% mode: latex
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
%%% TeX-master: t
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
%%% End: