handouts/ho01.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Sep 2013 13:06:27 +0100
changeset 106 93bf3182cf71
parent 105 397ecdafefd8
child 107 1bdec6a9e03d
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
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\begin{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
\section*{Handout 1}
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
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
    13
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
    14
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
    15
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
    16
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
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
    18
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
    19
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
[\text{\it h, e, l, l, o}]
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
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
\noindent
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
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
    26
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
    27
There are also some subtleties with the empty string, sometimes written as {\it ""} or as the empty list
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
of characters $[\,]$. 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
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
    31
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
\[
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
\{\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
    34
\]
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
\noindent
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
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
    38
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
    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 "the", "of", "milk", "name", "antidisestablishmentarianism", \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
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
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
    46
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
    47
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
    48
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
    49
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
    50
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
    51
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
    52
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
    53
element.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    54
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    55
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
    56
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
    57
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
    58
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
    59
regular expression\footnote{See \url{http://goo.gl/5LoVX7}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    60
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    61
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    62
([\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
    63
\]
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    66
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
    67
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
    68
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
    69
that every string is an email address. For example
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
\text{\it foo}@\text{\it bar}.\text{\it c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    73
\]
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    75
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    76
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
    77
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
    78
lower-case.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    79
\bigskip
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    81
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    82
\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
    83
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
    84
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
    85
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
    86
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    87
Regular expressions are given by the following grammar:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    88
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    89
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    90
\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
    91
  $r$ & $::=$ &   $\varnothing$         & null\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    92
        & $\mid$ & $\epsilon$              & empty string / "" / []\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    93
        & $\mid$ & $c$                         & character\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    94
        & $\mid$ & $r_1 \cdot r_2$      & sequence\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    95
        & $\mid$ & $r_1 + r_2$            & alternative / choice\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    96
        & $\mid$ & $r^*$                      & star (zero or more)\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    97
  \end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    98
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
    99
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   100
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   101
There are some subtleties you should be aware of. First, we will use parentheses to disambiguate
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   102
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
   103
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
   104
$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
   105
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
   106
respectively. The reasons for this will become clear shortly. In the literature you will often find that the choice
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 105
diff changeset
   107
$r_1 + r_2$  is written as $r_1\mid{}r_2$ 
105
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
\end{document}
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
%%% Local Variables: 
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
%%% mode: latex
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
%%% TeX-master: t
397ecdafefd8 added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
%%% End: