author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 21 Aug 2014 15:10:53 +0100 | |
changeset 227 | 93bd75031ced |
child 228 | 4df4404455d0 |
permissions | -rw-r--r-- |
227
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{hyperref} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{amssymb} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
\usepackage{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
\usepackage{menukeys} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
\usepackage{amsmath} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\usepackage{../langs} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
\usepackage{mathpazo} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
\usepackage[scaled=.95]{helvet} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
\begin{document} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
\section*{A Crash-Course on Scala} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
Scala is programming language that combines functional and |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
object-oriented programming-styles. This language received in |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
the last five years quite a bit of attention. One reason is |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
that, like the Java programming language, it compiles to the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
Java Virtual Machine (JVM) and therefore can run under MacOSX, |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
Linux and Windows.\footnote{There are also experimental |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
backends for Android and JavaScript.} The main compiler can be |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
downloaded from |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
\begin{quote} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
\url{http://www.scala-lang.org} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
\end{quote} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
\noindent Why do I use Scala in this course? Actually, you can |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
do any part of the programming coursework in \emph{any} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
programming language you like. I use Scale because its |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
functional programming-style allows for some very small and |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
elegant code. Since the compiler is free, you can download it |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
and run every example I give. But if you prefer, you can also |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
translate the examples into any other functional language, for |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
example Haskell, ML, F\# and so on. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
Writing programs in Scala can be done with Eclipse IDE and |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
also with IntelliJ, but I am using just the Emacs-editor and |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
run programs on the command line. One advantage of Scala is |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
that it has an interpreter (a REPL --- read-eval-print-loop) |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
with which you can run and test small code-snippets without |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
the need of the compiler. This helps a lot for interactively |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
developing programs. Once you installed Scala correctly, you |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
can start the interpreter by typing |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
\begin{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
$ scala\small |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM). |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
Type in expressions to have them evaluated. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
Type :help for more information.\normalsize |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
scala> |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
\end{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
\noindent At the scala prompt you can type things like {\tt 2 + 3} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
\keys{Ret}. The output will be |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
\begin{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
scala> 2 + 3 |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
res0: Int = 5 |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
\end{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
indicating that the result is of type {\tt Int} and the result |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
of the addition is {\tt 5}. Another example you can type in |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
immediately is |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
\begin{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
scala> print ("hello world") |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
hello world |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
\end{alltt} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
\noindent which prints out a string. Note that in this case |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
there is no result: the reason is that {\tt print} does not |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
produce any result indicated by {\tt res\_}, rather it is a |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
function that causes a \emph{side-effect} of printing out a |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
string. Once you are more familiar with the functional |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
programming-style, you will immediately see what the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
difference is between a function that returns a result and a |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
function that causes a side-effect (the latter always has as |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
return type {\tt Unit}). |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
\subsection*{Inductive Datatypes} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
The elegance and conciseness of Scala programs stems often |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
from the fact that inductive datatypes can be easily defined. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
For example in ``Mathematics'' we would define regular |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
expressions by the grammar |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
\begin{tabular}{r@{\hspace{2mm}}r@{\hspace{2mm}}l@{\hspace{13mm}}l} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
$r$ & $::=$ & $\varnothing$ & null\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
& $\mid$ & $\epsilon$ & empty string\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
& $\mid$ & $c$ & single character\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
& $\mid$ & $r^*$ & star (zero or more)\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
\noindent This grammar specifies what regular expressions are |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
(essentially a kind of tree-structure with three kinds of |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
inner nodes and three leave nodes). If you are familiar with |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
Java, it might be an instructive exercise to define this |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
kind of inductive datatypes in Java. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
Implementing the regular expressions from above in Scala |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
requires an \emph{abstract class}, say, {\tt Rexp}. The |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
different kinds of regular expressions will be instances of |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
this abstract class. The cases for $\varnothing$ and |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
$\epsilon$ do not require any arguments, while in the other |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
cases we do have arguments. For example the character regular |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
expressions need to take as argument the character they are |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
supposed to recognise. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
. is a relative recen programming language |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
This course is about the processing of strings. Lets start |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
with what we mean by \emph{strings}. Strings (they are also |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
sometimes referred to as \emph{words}) are lists of characters |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
drawn from an \emph{alphabet}. If nothing else is specified, |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
we usually assume the alphabet consists of just the lower-case |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
letters $a$, $b$, \ldots, $z$. Sometimes, however, we |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
explicitly restrict strings to contain, for example, only the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
letters $a$ and $b$. In this case we say the alphabet is the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
set $\{a, b\}$. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
There are many ways how we can write down strings. In programming languages, they are usually |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
written as {\it "hello"} where the double quotes indicate that we dealing with a string. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
Essentially, strings are lists of characters which can be written for example as follows |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
[\text{\it h, e, l, l, o}] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
The important point is that we can always decompose strings. For example, we will often consider the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
first character of a string, say $h$, and the ``rest'' of a string say {\it "ello"} when making definitions |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
about strings. There are some subtleties with the empty string, sometimes written as {\it ""} but also as |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
the empty list of characters $[\,]$. Two strings, for example $s_1$ and $s_2$, can be \emph{concatenated}, |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
which we write as $s_1 @ s_2$. Suppose we are given two strings {\it "foo"} and {\it "bar"}, then their concatenation |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
144 |
gives {\it "foobar"}. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
145 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
We often need to talk about sets of strings. For example the set of all strings over the alphabet $\{a, \ldots\, z\}$ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
is |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
149 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
\{\text{\it "", "a", "b", "c",\ldots,"z", "aa", "ab", "ac", \ldots, "aaa", \ldots}\} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
Any set of strings, not just the set-of-all-strings, is often called a \emph{language}. The idea behind |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
this choice of terminology is that if we enumerate, say, all words/strings from a dictionary, like |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
\{\text{\it "the", "of", "milk", "name", "antidisestablishmentarianism", \ldots}\} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
then we have essentially described the English language, or more precisely all |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
strings that can be used in a sentence of the English language. French would be a |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
different set of strings, and so on. In the context of this course, a language might |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
not necessarily make sense from a natural language point of view. For example |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
the set of all strings shown above is a language, as is the empty set (of strings). The |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
empty set of strings is often written as $\varnothing$ or $\{\,\}$. Note that there is a |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
difference between the empty set, or empty language, and the set that |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
contains only the empty string $\{\text{""}\}$: the former has no elements, whereas |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
170 |
the latter has one element. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
As seen, there are languages which contain infinitely many strings, like the set of all strings. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
The ``natural'' languages like English, French and so on contain many but only finitely many |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
strings (namely the ones listed in a good dictionary). It might be therefore be surprising that the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
language consisting of all email addresses is infinite provided we assume it is defined by the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
regular expression\footnote{See \url{http://goo.gl/5LoVX7}} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
178 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
179 |
([\text{\it{}a-z0-9\_.-}]^+)@([\text{\it a-z0-9.-}]^+).([\text{\it a-z.}]^{\{2,6\}}) |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
180 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
181 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
182 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
183 |
One reason is that before the $@$-sign there can be any string you want assuming it |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
184 |
is made up from letters, digits, underscores, dots and hyphens---clearly there are infinitely many |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
185 |
of those. Similarly the string after the $@$-sign can be any string. However, this does not mean |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
186 |
that every string is an email address. For example |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
187 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
188 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
189 |
"\text{\it foo}@\text{\it bar}.\text{\it c}" |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
190 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
191 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
192 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
193 |
is not, because the top-level-domains must be of length of at least two. (Note that there is |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
194 |
the convention that uppercase letters are treated in email-addresses as if they were |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
195 |
lower-case.) |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
196 |
\bigskip |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
197 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
198 |
Before we expand on the topic of regular expressions, let us review some operations on |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
sets. We will use capital letters $A$, $B$, $\ldots$ to stand for sets of strings. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
200 |
The union of two sets is written as usual as $A \cup B$. We also need to define the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
201 |
operation of \emph{concatenating} two sets of strings. This can be defined as |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
202 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
203 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
204 |
A @ B \dn \{s_1@ s_2 | s_1 \in A \wedge s_2 \in B \} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
205 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
206 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
207 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
208 |
which essentially means take the first string from the set $A$ and concatenate it with every |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
209 |
string in the set $B$, then take the second string from $A$ do the same and so on. You might |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
210 |
like to think about what this definition means in case $A$ or $B$ is the empty set. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
211 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
212 |
We also need to define |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
213 |
the power of a set of strings, written as $A^n$ with $n$ being a natural number. This is defined inductively as follows |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
214 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
215 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
216 |
\begin{tabular}{rcl} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
217 |
$A^0$ & $\dn$ & $\{[\,]\}$ \\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
218 |
$A^{n+1}$ & $\dn$ & $A @ A^n$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
219 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
220 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
221 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
222 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
223 |
Finally we need the \emph{star} of a set of strings, written $A^*$. This is defined as the union |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
224 |
of every power of $A^n$ with $n\ge 0$. The mathematical notation for this operation is |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
225 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
226 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
227 |
A^* \dn \bigcup_{0\le n} A^n |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
228 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
229 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
230 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
231 |
This definition implies that the star of a set $A$ contains always the empty string (that is $A^0$), one |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
232 |
copy of every string in $A$ (that is $A^1$), two copies in $A$ (that is $A^2$) and so on. In case $A=\{"a"\}$ we therefore |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
233 |
have |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
234 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
235 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
236 |
A^* = \{"", "a", "aa", "aaa", \ldots\} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
237 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
238 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
239 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
240 |
Be aware that these operations sometimes have quite non-intuitive properties, for example |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
241 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
242 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
243 |
\begin{tabular}{@{}ccc@{}} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
244 |
\begin{tabular}{@{}r@{\hspace{1mm}}c@{\hspace{1mm}}l} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
245 |
$A \cup \varnothing$ & $=$ & $A$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
246 |
$A \cup A$ & $=$ & $A$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
247 |
$A \cup B$ & $=$ & $B \cup A$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
248 |
\end{tabular} & |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
249 |
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
250 |
$A @ B$ & $\not =$ & $B @ A$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
251 |
$A @ \varnothing$ & $=$ & $\varnothing @ A = \varnothing$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
252 |
$A @ \{""\}$ & $=$ & $\{""\} @ A = A$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
253 |
\end{tabular} & |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
254 |
\begin{tabular}{r@{\hspace{1mm}}c@{\hspace{1mm}}l@{}} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
255 |
$\varnothing^*$ & $=$ & $\{""\}$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
256 |
$\{""\}^*$ & $=$ & $\{""\}$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
257 |
$A^\star$ & $=$ & $\{""\} \cup A\cdot A^*$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
258 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
259 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
260 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
261 |
\bigskip |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
262 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
263 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
264 |
\emph{Regular expressions} are meant to conveniently describe languages...at least languages |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
265 |
we are interested in in Computer Science. For example there is no convenient regular expression |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
266 |
for describing the English language short of enumerating all English words. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
267 |
But they seem useful for describing all permitted email addresses, as seen above. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
268 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
269 |
Regular expressions are given by the following grammar: |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
270 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
271 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
272 |
\begin{tabular}{r@{\hspace{1mm}}r@{\hspace{1mm}}l@{\hspace{13mm}}l} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
273 |
$r$ & $::=$ & $\varnothing$ & null\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
274 |
& $\mid$ & $\epsilon$ & empty string / "" / []\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
275 |
& $\mid$ & $c$ & single character\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
276 |
& $\mid$ & $r_1 \cdot r_2$ & sequence\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
277 |
& $\mid$ & $r_1 + r_2$ & alternative / choice\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
278 |
& $\mid$ & $r^*$ & star (zero or more)\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
279 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
280 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
281 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
282 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
283 |
Because we overload our notation, there are some subtleties you should be aware of. First, the letter |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
284 |
$c$ stands for any character from the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
285 |
alphabet at hand. Second, we will use parentheses to disambiguate |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
286 |
regular expressions. For example we will write $(r_1 + r_2)^*$, which is different from, say $r_1 + (r_2)^*$. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
287 |
The former means roughly zero or more times $r_1$ or $r_2$, while the latter means $r_1$ or zero or more times |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
288 |
$r_2$. We should also write $(r_1 + r_2) + r_3$, which is different from the regular expression $r_1 + (r_2 + r_3)$, |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
289 |
but in case of $+$ and $\cdot$ we actually do not care about the order and just write $r_1 + r_2 + r_3$, or $r_1 \cdot r_2 \cdot r_3$, |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
290 |
respectively. The reasons for this will become clear shortly. In the literature you will often find that the choice |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
291 |
$r_1 + r_2$ is written as $r_1\mid{}r_2$ or $r_1\mid\mid{}r_2$. Also following the convention in the literature, we will in case of $\cdot$ even |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
292 |
often omit it all together. For example the regular expression for email addresses shown above is meant to be of the form |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
293 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
294 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
295 |
([\ldots])^+ \cdot @ \cdot ([\ldots])^+ \cdot . \cdot \ldots |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
296 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
297 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
298 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
299 |
meaning first comes a name (specified by the regular expression $([\ldots])^+$), then an $@$-sign, then |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
300 |
a domain name (specified by the regular expression $([\ldots])^+$), then a dot and then a top-level domain. Similarly if |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
301 |
we want to specify the regular expression for the string {\it "hello"} we should write |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
302 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
303 |
\[ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
304 |
{\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
305 |
\] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
306 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
307 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
308 |
but often just write {\it hello}. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
309 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
310 |
Another source of confusion might arise from the fact that we use the term \emph{regular expression} for the regular expressions used in ``theory'' |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
311 |
and also the ones used in ``practice''. In this course we refer by default to the regular expressions defined by the grammar above. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
312 |
In ``practice'' we often use $r^+$ to stand for one or more times, $\backslash{}d$ to stand for a digit, $r^?$ to stand for an optional regular |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
313 |
expression, or ranges such as $[\text{\it a - z}]$ to stand for any lower case letter from $a$ to $z$. They are however mere convenience |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
314 |
as they can be seen as shorthand for |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
315 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
316 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
317 |
\begin{tabular}{rcl} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
318 |
$r^+$ & $\mapsto$ & $r\cdot r^*$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
319 |
$r^?$ & $\mapsto$ & $\epsilon + r$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
320 |
$\backslash d$ & $\mapsto$ & $0 + 1 + 2 + \ldots + 9$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
321 |
$[\text{\it a - z}]$ & $\mapsto$ & $a + b + \ldots + z$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
322 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
323 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
324 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
325 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
326 |
We will see later that the \emph{not}-regular-expression can also be seen as convenience. This regular |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
327 |
expression is supposed to stand for every string \emph{not} matched by a regular expression. We will write |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
328 |
such not-regular-expressions as $\sim{}r$. While being ``convenience'' it is often not so clear what the shorthand for |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
329 |
these kind of not-regular-expressions is. Try to write down the regular expression which can match any |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
330 |
string except the two strings {\it "hello"} and {\it "world"}. It is possible in principle, but often it is easier to just include |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
331 |
$\sim{}r$ in the definition of regular expressions. Whenever we do so, we will state it explicitly. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
332 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
333 |
So far we have only considered informally what the \emph{meaning} of a regular expression is. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
334 |
To do so more formally we will associate with every regular expression a set of strings |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
335 |
that is supposed to be matched by this |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
336 |
regular expression. This can be defined recursively as follows |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
337 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
338 |
\begin{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
339 |
\begin{tabular}{rcl} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
340 |
$L(\varnothing)$ & $\dn$ & $\{\,\}$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
341 |
$L(\epsilon)$ & $\dn$ & $\{""\}$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
342 |
$L(c)$ & $\dn$ & $\{"c"\}$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
343 |
$L(r_1+ r_2)$ & $\dn$ & $L(r_1) \cup L(r_2)$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
344 |
$L(r_1 \cdot r_2)$ & $\dn$ & $\{s_1@ s_2 | s_1 \in L(r_1) \wedge s_2 \in L(r_2) \}$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
345 |
$L(r^*)$ & $\dn$ & $\bigcup_{n \ge 0} L(r)^n$\\ |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
346 |
\end{tabular} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
347 |
\end{center} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
348 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
349 |
\noindent |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
350 |
As a result we can now precisely state what the meaning, for example, of the regular expression |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
351 |
${\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}$ is, namely |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
352 |
$L({\it h} \cdot {\it e} \cdot {\it l} \cdot {\it l} \cdot {\it o}) = \{\text{\it"hello"}\}$...as expected. Similarly if we have the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
353 |
choice-regular-expression $a + b$, its meaning is $L(a + b) = \{\text{\it"a"}, \text{\it"b"}\}$, namely the only two strings which can possibly |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
354 |
be matched by this choice. You can now also see why we do not make a difference |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
355 |
between the different regular expressions $(r_1 + r_2) + r_3$ and $r_1 + (r_2 + r_3)$....they |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
356 |
are not the same regular expression, but have the same meaning. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
357 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
358 |
The point of the definition of $L$ is that we can use it to precisely specify when a string $s$ is matched by a |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
359 |
regular expression $r$, namely only when $s \in L(r)$. In fact we will write a program {\it match} that takes any string $s$ and |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
360 |
any regular expression $r$ as argument and returns \emph{yes}, if $s \in L(r)$ and \emph{no}, |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
361 |
if $s \not\in L(r)$. We leave this for the next lecture. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
362 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
363 |
\begin{figure}[p] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
364 |
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler1.scala}}} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
365 |
\caption{Scala code for a web-crawler that can detect broken links in a web-page. It uses |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
366 |
the regular expression {\tt http\_pattern} in Line~15 for recognising URL-addresses. It finds |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
367 |
all links using the library function {\tt findAllIn} in Line~21.} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
368 |
\end{figure} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
369 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
370 |
\begin{figure}[p] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
371 |
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler2.scala}}} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
372 |
\caption{A version of the web-crawler which only follows links in ``my'' domain---since these are the |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
373 |
ones I am interested in to fix. It uses the regular expression {\tt my\_urls} in Line~16. |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
374 |
The main change is in Line~26 where there is a test whether URL is in ``my'' domain or not.} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
375 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
376 |
\end{figure} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
377 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
378 |
\begin{figure}[p] |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
379 |
{\lstset{language=Scala}\texttt{\lstinputlisting{../progs/crawler3.scala}}} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
380 |
\caption{A small email harvester---whenever we download a web-page, we also check whether |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
381 |
it contains any email addresses. For this we use the regular expression {\tt email\_pattern} in |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
382 |
Line~17. The main change is in Lines 33 and 34 where all email addresses that can be found in a page are printed.} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
383 |
\end{figure} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
384 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
385 |
\end{document} |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
386 |
|
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
387 |
%%% Local Variables: |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
388 |
%%% mode: latex |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
389 |
%%% TeX-master: t |
93bd75031ced
added handout
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
390 |
%%% End: |