author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 26 Sep 2013 13:15:05 +0100 | |
changeset 107 | 1bdec6a9e03d |
parent 106 | 93bf3182cf71 |
child 108 | 52ee218151f9 |
permissions | -rw-r--r-- |
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
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
53 |
element. |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
54 |
|
93bf3182cf71
added
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. |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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}} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
60 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
61 |
\[ |
93bf3182cf71
added
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\}}) |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
63 |
\] |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
64 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
65 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
70 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
71 |
\[ |
93bf3182cf71
added
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} |
93bf3182cf71
added
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
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
75 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
78 |
lower-case. |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
79 |
\bigskip |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
80 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
81 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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. |
93bf3182cf71
added
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. |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
86 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
87 |
Regular expressions are given by the following grammar: |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
88 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
89 |
\begin{center} |
93bf3182cf71
added
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} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
91 |
$r$ & $::=$ & $\varnothing$ & null\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
92 |
& $\mid$ & $\epsilon$ & empty string / "" / []\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
93 |
& $\mid$ & $c$ & character\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
94 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
95 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
96 |
& $\mid$ & $r^*$ & star (zero or more)\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
97 |
\end{tabular} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
98 |
\end{center} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
99 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
100 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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)^*$. |
93bf3182cf71
added
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 |
93bf3182cf71
added
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)$, |
93bf3182cf71
added
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$, |
93bf3182cf71
added
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 |
107
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
107 |
$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 |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
108 |
the regular expression for email addresses is meant to be of the form |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
109 |
|
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
110 |
\[ |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
111 |
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
112 |
\] |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
113 |
|
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
114 |
\noindent |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
115 |
meaning first comes a name (specified by the regular expression $([\ldots])^+$), then an $@$-sign, then |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
116 |
a domain name (specified by the regular expression $([\ldots])^+$), then a top-level domain. |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
117 |
|
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
118 |
|
105
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
\end{document} |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
|
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
%%% Local Variables: |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
%%% mode: latex |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
%%% TeX-master: t |
397ecdafefd8
added handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
%%% End: |