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-- |
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
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
8 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% |
52ee218151f9
added
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
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
57 |
element. |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
58 |
|
93bf3182cf71
added
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. |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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}} |
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 |
\[ |
93bf3182cf71
added
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\}}) |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
67 |
\] |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
68 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
69 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
74 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
75 |
\[ |
93bf3182cf71
added
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} |
93bf3182cf71
added
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
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
79 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
82 |
lower-case. |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
83 |
\bigskip |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
84 |
|
108
52ee218151f9
added
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 |
52ee218151f9
added
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. |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
89 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
90 |
\[ |
52ee218151f9
added
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 \} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
92 |
\] |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
93 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
94 |
\noindent |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
98 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
99 |
\begin{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
100 |
\begin{tabular}{rcl} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
101 |
$A^0$ & $\dn$ & $\{[\,]\}$ \\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
102 |
$A^{n+1}$ & $\dn$ & $A @ A^n$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
103 |
\end{tabular} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
104 |
\end{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
105 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
106 |
\noindent |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
109 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
110 |
\[ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
111 |
A^* \dn \bigcup_{0\le n} A^n |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
112 |
\] |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
113 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
114 |
\noindent |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
117 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
118 |
\[ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
119 |
A^* = \{"", "a", "aa", "aaa", \ldots\} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
120 |
\] |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
121 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
122 |
\noindent |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
124 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
125 |
\begin{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
126 |
\begin{tabular}{ccc} |
52ee218151f9
added
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} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
128 |
$A \cup \varnothing$ & $=$ & $A$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
129 |
$A \cup A$ & $=$ & $A$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
130 |
$A \cup B$ & $=$ & $B \cup A$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
131 |
\end{tabular} & |
52ee218151f9
added
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} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
133 |
$A @ B$ & $\not =$ & $B @ A$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
134 |
$A @ \varnothing$ & $=$ & $\varnothing @ A = \varnothing$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
135 |
$A @ \{""\}$ & $=$ & $\{""\} @ A = A$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
136 |
\end{tabular} & |
52ee218151f9
added
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} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
138 |
$\varnothing^*$ & $=$ & $\{""\}$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
139 |
$\{""\}^*$ & $=$ & $\{""\}$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
140 |
$A^1$ & $=$ & $A$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
141 |
\end{tabular} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
142 |
\end{tabular} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
143 |
\end{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
144 |
\bigskip |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
145 |
|
106
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
146 |
\noindent |
93bf3182cf71
added
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 |
93bf3182cf71
added
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 |
93bf3182cf71
added
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. |
93bf3182cf71
added
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. |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
151 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
152 |
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
|
153 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
154 |
\begin{center} |
93bf3182cf71
added
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} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
156 |
$r$ & $::=$ & $\varnothing$ & null\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
157 |
& $\mid$ & $\epsilon$ & empty string / "" / []\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
158 |
& $\mid$ & $c$ & character\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
159 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
160 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
161 |
& $\mid$ & $r^*$ & star (zero or more)\\ |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
162 |
\end{tabular} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
163 |
\end{center} |
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
164 |
|
93bf3182cf71
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
105
diff
changeset
|
165 |
\noindent |
108
52ee218151f9
added
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 |
52ee218151f9
added
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
93bf3182cf71
added
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)^*$. |
93bf3182cf71
added
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 |
93bf3182cf71
added
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)$, |
93bf3182cf71
added
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$, |
93bf3182cf71
added
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
1bdec6a9e03d
added
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 |
1bdec6a9e03d
added
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 |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
175 |
|
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
176 |
\[ |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
177 |
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
178 |
\] |
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
179 |
|
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
180 |
\noindent |
1bdec6a9e03d
added
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
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
184 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
185 |
\[ |
52ee218151f9
added
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} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
187 |
\] |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
188 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
189 |
\noindent |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
190 |
but often just write {\it hello}. |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
191 |
|
52ee218151f9
added
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'' |
52ee218151f9
added
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. |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
196 |
as they can be seen as shorthand for |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
197 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
198 |
\begin{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
199 |
\begin{tabular}{rcl} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
200 |
$r^+$ & $\mapsto$ & $r\cdot r^*$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
201 |
$r^?$ & $\mapsto$ & $\epsilon + r$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
202 |
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\ |
52ee218151f9
added
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$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
204 |
\end{tabular} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
205 |
\end{center} |
107
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
206 |
|
108
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
207 |
\noindent |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
211 |
these kind of regular expressions is. |
107
1bdec6a9e03d
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
106
diff
changeset
|
212 |
|
108
52ee218151f9
added
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. |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
215 |
regular expression. This can be formally defined as |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
216 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
217 |
\begin{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
218 |
\begin{tabular}{rcl} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
219 |
$L(\varnothing)$ & $\dn$ & $\{\,\}$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
220 |
$L(\epsilon)$ & $\dn$ & $\{""\}$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
221 |
$L(c)$ & $\dn$ & $\{"c"\}$\\ |
52ee218151f9
added
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)$\\ |
52ee218151f9
added
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) \}$\\ |
52ee218151f9
added
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$\\ |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
225 |
\end{tabular} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
226 |
\end{center} |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
227 |
|
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
228 |
\noindent |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
233 |
be matched by this choice. |
52ee218151f9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
107
diff
changeset
|
234 |
|
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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 |
52ee218151f9
added
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}, |
52ee218151f9
added
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: |