author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sat, 06 Dec 2014 18:50:58 +0000 | |
changeset 319 | e7b110f93697 |
parent 296 | 796b9b81ac8d |
child 326 | 94700593a2d5 |
permissions | -rw-r--r-- |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
\usepackage{../style} |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs} |
282
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
4 |
\usepackage{../graphics} |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
282
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
9 |
\section*{Handout 4 (Sulzmann \& Lu Algorithm)} |
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
10 |
|
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
11 |
So far our algorithm based on derivatives was only able to say |
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
12 |
yes or no depending on whether a string was matched by regular |
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
13 |
expression or not. Often a more interesting question is to |
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
14 |
find out \emph{how} a regular expression matched a string? |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
15 |
Answering this question will also help us with the problem we |
319
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
16 |
are after, namely tokenising an input string. |
282
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
17 |
|
319
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
18 |
The algorithm we will be looking at was designed by Sulzmann \& Lu in |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
19 |
a rather recent paper. A link to it is provided on KEATS, in case you |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
20 |
are interested.\footnote{In my humble opinion this is an interesting |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
21 |
instance of the research literature: it contains a very neat idea, |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
22 |
but its presentation is rather sloppy. In earlier versions of their |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
23 |
paper, students and I found several rather annoying typos in their |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
24 |
examples and definitions.} In order to give an answer for how a |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
25 |
regular expression matched a string, Sulzmann and Lu introduce |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
26 |
\emph{values}. A value will be the output of the algorithm whenever |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
27 |
the regular expression matches the string. If the string does not |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
28 |
match the string, an error will be raised. Since the first phase of |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
29 |
the algorithm by Sulzmann \& Lu is identical to the derivative based |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
30 |
matcher from the first coursework, the function $nullable$ will be |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
31 |
used to decide whether as string is matched by a regular |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
32 |
expression. If $nullable$ says yes, then values are constructed that |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
33 |
reflect how the regular expression matched the string. The definitions |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
34 |
for regular expressions $r$ and values $v$ is shown next to each other |
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
35 |
below: |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
36 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
37 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
38 |
\begin{tabular}{cc} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
39 |
\begin{tabular}{@{}rrl@{}} |
319
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
40 |
\multicolumn{3}{c}{regular expressions}\medskip\\ |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
41 |
$r$ & $::=$ & $\varnothing$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
42 |
& $\mid$ & $\epsilon$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
43 |
& $\mid$ & $c$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
44 |
& $\mid$ & $r_1 \cdot r_2$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
45 |
& $\mid$ & $r_1 + r_2$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
46 |
\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
47 |
& $\mid$ & $r^*$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
48 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
49 |
& |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
50 |
\begin{tabular}{@{\hspace{0mm}}rrl@{}} |
319
e7b110f93697
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
296
diff
changeset
|
51 |
\multicolumn{3}{c}{values}\medskip\\ |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
52 |
$v$ & $::=$ & \\ |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
53 |
& & $Empty$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
54 |
& $\mid$ & $Char(c)$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
55 |
& $\mid$ & $Seq(v_1,v_2)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
56 |
& $\mid$ & $Left(v)$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
57 |
& $\mid$ & $Right(v)$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
58 |
& $\mid$ & $[v_1,\ldots\,v_n]$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
59 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
60 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
61 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
62 |
|
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
63 |
\noindent The point is that there is a very strong |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
64 |
correspondence between them. There is no value for the |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
65 |
$\varnothing$ regular expression, since it does not match any |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
66 |
string. Otherwise there is exactly one value corresponding to |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
67 |
each regular expression with the exception of $r_1 + r_2$ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
68 |
where there are two values, namely $Left(v)$ and $Right(v)$ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
69 |
corresponding to the two alternatives. Note that $r^*$ is |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
70 |
associated with a list of values, one for each copy of $r$ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
71 |
that was needed to match the string. This means we might also |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
72 |
return the empty list $[]$, if no copy was needed. |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
73 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
74 |
To emphasise the connection between regular expressions and |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
75 |
values, I have in my implementation the convention that |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
76 |
regular expressions are written entirely with upper-case |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
77 |
letters, while values just start with a single upper-case |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
78 |
character. My definition of values in Scala is below. I use |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
79 |
this in the REPL of Scala; when I use the Scala compiler |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
80 |
I need to rename some constructors, because Scala on Macs |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
81 |
does not like classes that are called \pcode{EMPTY} and |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
82 |
\pcode{Empty}. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
83 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
84 |
{\small\lstinputlisting[language=Scala,numbers=none] |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
85 |
{../progs/app02.scala}} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
86 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
87 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
88 |
Graphically the algorithm by Sulzmann \& Lu can be illustrated |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
89 |
by the picture in Figure~\ref{Sulz} where the path from the |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
90 |
left to the right involving $der/nullable$ is the first phase |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
91 |
of the algorithm and $mkeps/inj$, the path from right to left, |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
92 |
the second phase. This picture shows the steps required when a |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
93 |
regular expression, say $r_1$, matches the string $abc$. We |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
94 |
first build the three derivatives (according to $a$, $b$ and |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
95 |
$c$). We then use $nullable$ to find out whether the resulting |
296
796b9b81ac8d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
288
diff
changeset
|
96 |
regular expression can match the empty string. If yes, we call |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
97 |
the function $mkeps$. |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
98 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
99 |
\begin{figure}[t] |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
100 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
101 |
\begin{tikzpicture}[scale=2,node distance=1.2cm, |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
102 |
every node/.style={minimum size=7mm}] |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
103 |
\node (r1) {$r_1$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
104 |
\node (r2) [right=of r1]{$r_2$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
105 |
\draw[->,line width=1mm](r1)--(r2) node[above,midway] {$der\,a$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
106 |
\node (r3) [right=of r2]{$r_3$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
107 |
\draw[->,line width=1mm](r2)--(r3) node[above,midway] {$der\,b$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
108 |
\node (r4) [right=of r3]{$r_4$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
109 |
\draw[->,line width=1mm](r3)--(r4) node[above,midway] {$der\,c$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
110 |
\draw (r4) node[anchor=west] {\;\raisebox{3mm}{$nullable$}}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
111 |
\node (v4) [below=of r4]{$v_4$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
112 |
\draw[->,line width=1mm](r4) -- (v4); |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
113 |
\node (v3) [left=of v4] {$v_3$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
114 |
\draw[->,line width=1mm](v4)--(v3) node[below,midway] {$inj\,c$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
115 |
\node (v2) [left=of v3]{$v_2$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
116 |
\draw[->,line width=1mm](v3)--(v2) node[below,midway] {$inj\,b$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
117 |
\node (v1) [left=of v2] {$v_1$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
118 |
\draw[->,line width=1mm](v2)--(v1) node[below,midway] {$inj\,a$}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
119 |
\draw (r4) node[anchor=north west] {\;\raisebox{-8mm}{$mkeps$}}; |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
120 |
\end{tikzpicture} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
121 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
122 |
\caption{The two phases of the algorithm by Sulzmann \& Lu. |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
123 |
\label{Sulz}} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
124 |
\end{figure} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
125 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
126 |
The $mkeps$ function calculates a value for how a regular |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
127 |
expression has matched the empty string. Its definition |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
128 |
is as follows: |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
129 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
130 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
131 |
\begin{tabular}{lcl} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
132 |
$mkeps(\epsilon)$ & $\dn$ & $Empty$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
133 |
$mkeps(r_1 + r_2)$ & $\dn$ & if $nullable(r_1)$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
134 |
& & then $Left(mkeps(r_1))$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
135 |
& & else $Right(mkeps(r_2))$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
136 |
$mkeps(r_1 \cdot r_2)$ & $\dn$ & $Seq(mkeps(r_1),mkeps(r_2))$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
137 |
$mkeps(r^*)$ & $\dn$ & $[]$ \\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
138 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
139 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
140 |
|
296
796b9b81ac8d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
288
diff
changeset
|
141 |
\noindent There are no cases for $\varnothing$ and $c$, since |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
142 |
these regular expression cannot match the empty string. Note |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
143 |
also that in case of alternatives we give preference to the |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
144 |
regular expression on the left-hand side. This will become |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
145 |
important later on. |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
146 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
147 |
The second phase of the algorithm is organised so that it will |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
148 |
calculate a value for how the derivative regular expression |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
149 |
has matched a string whose first character has been chopped |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
150 |
off. Now we need a function that reverses this ``chopping |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
151 |
off'' for values. The corresponding function is called $inj$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
152 |
for injection. This function takes three arguments: the first |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
153 |
one is a regular expression for which we want to calculate the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
154 |
value, the second is the character we want to inject and the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
155 |
third argument is the value where we will inject the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
156 |
character. The result of this function is a new value. The |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
157 |
definition of $inj$ is as follows: |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
158 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
159 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
160 |
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
161 |
$inj\,(c)\,c\,Empty$ & $\dn$ & $Char\,c$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
162 |
$inj\,(r_1 + r_2)\,c\,Left(v)$ & $\dn$ & $Left(inj\,r_1\,c\,v)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
163 |
$inj\,(r_1 + r_2)\,c\,Right(v)$ & $\dn$ & $Right(inj\,r_2\,c\,v)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
164 |
$inj\,(r_1 \cdot r_2)\,c\,Seq(v_1,v_2)$ & $\dn$ & $Seq(inj\,r_1\,c\,v_1,v_2)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
165 |
$inj\,(r_1 \cdot r_2)\,c\,Left(Seq(v_1,v_2))$ & $\dn$ & $Seq(inj\,r_1\,c\,v_1,v_2)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
166 |
$inj\,(r_1 \cdot r_2)\,c\,Right(v)$ & $\dn$ & $Seq(mkeps(r_1),inj\,r_2\,c\,v)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
167 |
$inj\,(r^*)\,c\,Seq(v,vs)$ & $\dn$ & $inj\,r\,c\,v\,::\,vs$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
168 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
169 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
170 |
|
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
171 |
\noindent This definition is by recursion on the regular |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
172 |
expression and by analysing the shape of the values. Therefore |
296
796b9b81ac8d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
288
diff
changeset
|
173 |
there are, for example, three cases for sequence regular |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
174 |
expressions. The last clause for the star regular expression |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
175 |
returns a list where the first element is $inj\,r\,c\,v$ and |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
176 |
the other elements are $vs$. That means $\_\,::\,\_$ should be |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
177 |
read as list cons. |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
178 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
179 |
To understand what is going on, it might be best to do some |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
180 |
example calculations and compare them with Figure~\ref{Sulz}. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
181 |
For this note that we have not yet dealt with the need of |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
182 |
simplifying regular expressions (this will be a topic on its |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
183 |
own later). Suppose the regular expression is $a \cdot (b |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
184 |
\cdot c)$ and the input string is $abc$. The derivatives from |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
185 |
the first phase are as follows: |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
186 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
187 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
188 |
\begin{tabular}{ll} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
189 |
$r_1$: & $a \cdot (b \cdot c)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
190 |
$r_2$: & $\epsilon \cdot (b \cdot c)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
191 |
$r_3$: & $(\varnothing \cdot (b \cdot c)) + (\epsilon \cdot c)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
192 |
$r_4$: & $(\varnothing \cdot (b \cdot c)) + ((\varnothing \cdot c) + \epsilon)$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
193 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
194 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
195 |
|
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
196 |
\noindent According to the simple algorithm, we would test |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
197 |
whether $r_4$ is nullable, which in this case it is. This |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
198 |
means we can use the function $mkeps$ to calculate a value for |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
199 |
how $r_4$ was able to match the empty string. Remember that |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
200 |
this function gives preference for alternatives on the |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
201 |
left-hand side. However there is only $\epsilon$ on the very |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
202 |
right-hand side of $r_4$ that matches the empty string. |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
203 |
Therefore $mkeps$ returns the value |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
204 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
205 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
206 |
$v_4:\;Right(Right(Empty))$ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
207 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
208 |
|
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
209 |
\noindent The point is that from this value we can directly |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
210 |
read off which part of $r_4$ matched the empty string. Next we |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
211 |
have to ``inject'' the last character, that is $c$ in the |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
212 |
running example, into this value $v_4$ in order to calculate |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
213 |
how $r_3$ could have matched the string $c$. According to the |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
214 |
definition of $inj$ we obtain |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
215 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
216 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
217 |
$v_3:\;Right(Seq(Empty, Char(c)))$ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
218 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
219 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
220 |
\noindent This is the correct result, because $r_3$ needs |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
221 |
to use the right-hand alternative, and then $\epsilon$ needs |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
222 |
to match the empty string and $c$ needs to match $c$. |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
223 |
Next we need to inject back the letter $b$ into $v_3$. This |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
224 |
gives |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
225 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
226 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
227 |
$v_2:\;Seq(Empty, Seq(Char(b), Char(c)))$ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
228 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
229 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
230 |
\noindent which is again the correct result for how $r_2$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
231 |
matched the string $bc$. Finally we need to inject back the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
232 |
letter $a$ into $v_2$ giving the final result |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
233 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
234 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
235 |
$v_1:\;Seq(Char(a), Seq(Char(b), Char(c)))$ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
236 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
237 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
238 |
\noindent This now corresponds to how the regular |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
239 |
expression $a \cdot (b \cdot c)$ matched the string $abc$. |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
240 |
|
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
241 |
There are a few auxiliary functions that are of interest |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
242 |
when analysing this algorithm. One is called \emph{flatten}, |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
243 |
written $|\_|$, which extracts the string ``underlying'' a |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
244 |
value. It is defined recursively as |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
245 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
246 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
247 |
\begin{tabular}{lcl} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
248 |
$|Empty|$ & $\dn$ & $[]$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
249 |
$|Char(c)|$ & $\dn$ & $[c]$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
250 |
$|Left(v)|$ & $\dn$ & $|v|$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
251 |
$|Right(v)|$ & $\dn$ & $|v|$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
252 |
$|Seq(v_1,v_2)|$& $\dn$ & $|v_1| \,@\, |v_2|$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
253 |
$|[v_1,\ldots ,v_n]|$ & $\dn$ & $|v_1| \,@\ldots @\, |v_n|$\\ |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
254 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
255 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
256 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
257 |
\noindent Using flatten we can see what is the string behind |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
258 |
the values calculated by $mkeps$ and $inj$ in our running |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
259 |
example are: |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
260 |
|
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
261 |
\begin{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
262 |
\begin{tabular}{ll} |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
263 |
$|v_4|$: & $[]$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
264 |
$|v_3|$: & $c$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
265 |
$|v_2|$: & $bc$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
266 |
$|v_1|$: & $abc$ |
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
267 |
\end{tabular} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
268 |
\end{center} |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
269 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
270 |
\noindent This indicates that $inj$ indeed is injecting, or |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
271 |
adding, back a character into the value. Next we look at how |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
272 |
simplification can be included into this algorithm. |
282
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
273 |
|
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
274 |
|
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
275 |
\subsubsection*{Simplification} |
282
3e3b927a85cf
added ho04
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
276 |
|
283
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
277 |
Generally the matching algorithms based on derivatives do |
c14e5ebf0c3b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
282
diff
changeset
|
278 |
poorly unless the regular expressions are simplified after |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
279 |
each derivative step. But this is a bit more involved in the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
280 |
algorithm of Sulzmann \& Lu. So what follows might require you |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
281 |
to read several times before it makes sense and also might |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
282 |
require that you do some example calculations. As a first |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
283 |
example consider the last derivation step in our earlier |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
284 |
example: |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
285 |
|
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
286 |
\begin{center} |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
287 |
$r_4 = der\,c\,r_3 = |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
288 |
(\varnothing \cdot (b \cdot c)) + ((\varnothing \cdot c) + \epsilon)$ |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
289 |
\end{center} |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
290 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
291 |
\noindent Simplifying this regular expression would just give |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
292 |
us $\epsilon$. Running $mkeps$ with this regular expression as |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
293 |
input, however, would then provide us with $Empty$ instead of |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
294 |
$Right(Right(Empty))$ that was obtained without the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
295 |
simplification. The problem is we need to recreate this more |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
296 |
complicated value, rather than just $Empty$. |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
297 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
298 |
This will require what I call \emph{rectification functions}. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
299 |
They need to be calculated whenever a regular expression gets |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
300 |
simplified. Rectification functions take a value as argument |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
301 |
and return a (rectified) value. Let us first take a look again |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
302 |
at our simplification rules: |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
303 |
|
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
304 |
\begin{center} |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
305 |
\begin{tabular}{l@{\hspace{2mm}}c@{\hspace{2mm}}l} |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
306 |
$r \cdot \varnothing$ & $\mapsto$ & $\varnothing$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
307 |
$\varnothing \cdot r$ & $\mapsto$ & $\varnothing$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
308 |
$r \cdot \epsilon$ & $\mapsto$ & $r$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
309 |
$\epsilon \cdot r$ & $\mapsto$ & $r$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
310 |
$r + \varnothing$ & $\mapsto$ & $r$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
311 |
$\varnothing + r$ & $\mapsto$ & $r$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
312 |
$r + r$ & $\mapsto$ & $r$\\ |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
313 |
\end{tabular} |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
314 |
\end{center} |
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
315 |
|
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
316 |
\noindent Applying them to $r_4$ will require several nested |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
317 |
simplifications in order end up with just $\epsilon$. However, |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
318 |
it is possible to apply them in a depth-first, or inside-out, |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
319 |
manner in order to calculate this simplified regular |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
320 |
expression. |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
321 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
322 |
The rectification we can implement this by letting simp return |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
323 |
not just a (simplified) regular expression, but also a |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
324 |
rectification function. Let us consider the alternative case, |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
325 |
$r_1 + r_2$, first. By going depth-first, we first simplify |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
326 |
the component regular expressions $r_1$ and $r_2.$ This will |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
327 |
return simplified versions (if they can be simplified), say |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
328 |
$r_{1s}$ and $r_{2s}$, but also two rectification functions |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
329 |
$f_{1s}$ and $f_{2s}$. We need to assemble them in order to |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
330 |
obtain a rectified value for $r_1 + r_2$. In case $r_{1s}$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
331 |
simplified to $\varnothing$, we continue the derivative |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
332 |
calculation with $r_{2s}$. The Sulzmann \& Lu algorithm would |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
333 |
return a corresponding value, say $v_{2s}$. But now this value |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
334 |
needs to be ``rectified'' to the value |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
335 |
|
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
336 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
337 |
$Right(v_{2s})$ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
338 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
339 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
340 |
\noindent The reason is that we look for the value that tells |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
341 |
us how $r_1 + r_2$ could have matched the string, not just |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
342 |
$r_2$ or $r_{2s}$. Unfortunately, this is still not the right |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
343 |
value in general because there might be some simplifications |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
344 |
that happened inside $r_2$ and for which the simplification |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
345 |
function retuned also a rectification function $f_{2s}$. So in |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
346 |
fact we need to apply this one too which gives |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
347 |
|
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
348 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
349 |
$Right(f_{2s}(v_{2s}))$ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
350 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
351 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
352 |
\noindent This is now the correct, or rectified, value. Since |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
353 |
the simplification will be done in the first phase of the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
354 |
algorithm, but the rectification needs to be done to the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
355 |
values in the second phase, it is advantageous to calculate |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
356 |
the rectification as a function, remember this function and |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
357 |
then apply the value to this function during the second phase. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
358 |
So if we want to implement the rectification as function, we |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
359 |
would need to return |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
360 |
|
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
361 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
362 |
$\lambda v.\,Right(f_{2s}(v))$ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
363 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
364 |
|
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
365 |
\noindent which is the lambda-calculus notation for |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
366 |
a function that expects a value $v$ and returns everything |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
367 |
after the dot where $v$ is replaced by whatever value is |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
368 |
given. |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
369 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
370 |
Let us package this idea with rectification functions |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
371 |
into a single function (still only considering the alternative |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
372 |
case): |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
373 |
|
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
374 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
375 |
\begin{tabular}{l} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
376 |
$simp(r)$:\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
377 |
\quad case $r = r_1 + r_2$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
378 |
\qquad let $(r_{1s}, f_{1s}) = simp(r_1)$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
379 |
\qquad \phantom{let} $(r_{2s}, f_{2s}) = simp(r_2)$\smallskip\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
380 |
\qquad case $r_{1s} = \varnothing$: |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
381 |
return $(r_{2s}, \lambda v. \,Right(f_{2s}(v)))$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
382 |
\qquad case $r_{2s} = \varnothing$: |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
383 |
return $(r_{1s}, \lambda v. \,Left(f_{1s}(v)))$\\ |
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
384 |
\qquad case $r_{1s} = r_{2s}$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
385 |
return $(r_{1s}, \lambda v. \,Left(f_{1s}(v)))$\\ |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
386 |
\qquad otherwise: |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
387 |
return $(r_{1s} + r_{2s}, f_{alt}(f_{1s}, f_{2s}))$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
388 |
\end{tabular} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
389 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
390 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
391 |
\noindent We first recursively call the simplification with |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
392 |
$r_1$ and $r_2$. This gives simplified regular expressions, |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
393 |
$r_{1s}$ and $r_{2s}$, as well as two rectification functions |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
394 |
$f_{1s}$ and $f_{2s}$. We next need to test whether the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
395 |
simplified regular expressions are $\varnothing$ so as to make |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
396 |
further simplifications. In case $r_{1s}$ is $\varnothing$, |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
397 |
then we can return $r_{2s}$ (the other alternative). However |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
398 |
for this we need to build a corresponding rectification |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
399 |
function, which as said above is |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
400 |
|
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
401 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
402 |
$\lambda v.\,Right(f_{2s}(v))$ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
403 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
404 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
405 |
\noindent The case where $r_{2s} = \varnothing$ is similar: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
406 |
We return $r_{1s}$ and rectify with $Left(\_)$ and the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
407 |
other calculated rectification function $f_{1s}$. This gives |
284
0afe43616b6a
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
283
diff
changeset
|
408 |
|
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
409 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
410 |
$\lambda v.\,Left(f_{1s}(v))$ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
411 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
412 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
413 |
\noindent The next case where $r_{1s} = r_{2s}$ can be treated |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
414 |
like the one where $r_{2s} = \varnothing$. We return $r_{1s}$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
415 |
and rectify with $Left(\_)$ and so on. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
416 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
417 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
418 |
The otherwise-case is slightly more complicated. In this case |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
419 |
neither $r_{1s}$ nor $r_{2s}$ are $\varnothing$ and also |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
420 |
$r_{1s} \not= r_{2s}$, which means no further simplification |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
421 |
can be applied. Accordingly, we return $r_{1s} + r_{2s}$ as |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
422 |
the simplified regular expression. In principle we also do not |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
423 |
have to do any rectification, because no simplification was |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
424 |
done in this case. But this is actually not true: There might |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
425 |
have been simplifications inside $r_{1s}$ and $r_{2s}$. We |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
426 |
therefore need to take into account the calculated |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
427 |
rectification functions $f_{1s}$ and $f_{2s}$. We can do this |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
428 |
by defining a rectification function $f_{alt}$ which takes two |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
429 |
rectification functions as arguments and applies them |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
430 |
according to whether the value is of the form $Left(\_)$ or |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
431 |
$Right(\_)$: |
285
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
432 |
|
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
433 |
\begin{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
434 |
\begin{tabular}{l@{\hspace{1mm}}l} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
435 |
$f_{alt}(f_1, f_2) \dn$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
436 |
\qquad $\lambda v.\,$ case $v = Left(v')$: |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
437 |
& return $Left(f_1(v'))$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
438 |
\qquad \phantom{$\lambda v.\,$} case $v = Right(v')$: |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
439 |
& return $Right(f_2(v'))$\\ |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
440 |
\end{tabular} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
441 |
\end{center} |
8a222559278f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
284
diff
changeset
|
442 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
443 |
The other interesting case with simplification is the sequence |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
444 |
case. In this case the main simplification function is as |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
445 |
follows |
286
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
446 |
|
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
447 |
\begin{center} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
448 |
\begin{tabular}{l} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
449 |
$simp(r)$:\qquad\qquad (continued)\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
450 |
\quad case $r = r_1 \cdot r_2$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
451 |
\qquad let $(r_{1s}, f_{1s}) = simp(r_1)$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
452 |
\qquad \phantom{let} $(r_{2s}, f_{2s}) = simp(r_2)$\smallskip\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
453 |
\qquad case $r_{1s} = \varnothing$: |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
454 |
return $(\varnothing, f_{error})$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
455 |
\qquad case $r_{2s} = \varnothing$: |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
456 |
return $(\varnothing, f_{error})$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
457 |
\qquad case $r_{1s} = \epsilon$: |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
458 |
return $(r_{2s}, \lambda v. \,Seq(f_{1s}(Empty), f_{2s}(v)))$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
459 |
\qquad case $r_{2s} = \epsilon$: |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
460 |
return $(r_{1s}, \lambda v. \,Seq(f_{1s}(v), f_{2s}(Empty)))$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
461 |
\qquad otherwise: |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
462 |
return $(r_{1s} \cdot r_{2s}, f_{seq}(f_{1s}, f_{2s}))$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
463 |
\end{tabular} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
464 |
\end{center} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
465 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
466 |
\noindent whereby in the last line $f_{seq}$ is again pushing |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
467 |
the two rectification functions into the two components of the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
468 |
Seq-value: |
286
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
469 |
|
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
470 |
\begin{center} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
471 |
\begin{tabular}{l@{\hspace{1mm}}l} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
472 |
$f_{seq}(f_1, f_2) \dn$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
473 |
\qquad $\lambda v.\,$ case $v = Seq(v_1, v_2)$: |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
474 |
& return $Seq(f_1(v_1), f_2(v_2))$\\ |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
475 |
\end{tabular} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
476 |
\end{center} |
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
477 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
478 |
\noindent Note that in the case of $r_{1s} = \varnothing$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
479 |
(similarly $r_{2s}$) we use the function $f_{error}$ for |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
480 |
rectification. If you think carefully, then you will realise |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
481 |
that this function will actually never been called. This is |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
482 |
because a sequence with $\varnothing$ will never recognise any |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
483 |
string and therefore the second phase of the algorithm would |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
484 |
never been called. The simplification function still expects |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
485 |
us to give a function. So in my own implementation I just |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
486 |
returned a function which raises an error. In the case |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
487 |
where $r_{1s} = \epsilon$ (similarly $r_{2s}$) we have |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
488 |
to create a sequence where the first component is a rectified |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
489 |
version of $Empty$. Therefore we call $f_{1s}$ with $Empty$. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
490 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
491 |
Since we only simplify regular expressions of the form $r_1 + |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
492 |
r_2$ and $r_1 \cdot r_2$ we do not have to do anything else |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
493 |
in the remaining cases. The rectification function will |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
494 |
be just the identity, which in lambda-calculus terms is |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
495 |
just |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
496 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
497 |
\begin{center} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
498 |
$\lambda v.\,v$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
499 |
\end{center} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
500 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
501 |
\noindent This completes the high-level version of the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
502 |
simplification function, which is also shown again in |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
503 |
Figure~\ref{simp}. This can now be used in a \emph{lexing |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
504 |
function} as follows: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
505 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
506 |
\begin{figure}[t] |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
507 |
\begin{center} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
508 |
\begin{tabular}{l} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
509 |
$simp(r)$:\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
510 |
\quad case $r = r_1 + r_2$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
511 |
\qquad let $(r_{1s}, f_{1s}) = simp(r_1)$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
512 |
\qquad \phantom{let} $(r_{2s}, f_{2s}) = simp(r_2)$\smallskip\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
513 |
\qquad case $r_{1s} = \varnothing$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
514 |
return $(r_{2s}, \lambda v. \,Right(f_{2s}(v)))$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
515 |
\qquad case $r_{2s} = \varnothing$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
516 |
return $(r_{1s}, \lambda v. \,Left(f_{1s}(v)))$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
517 |
\qquad case $r_{1s} = r_{2s}$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
518 |
return $(r_{1s}, \lambda v. \,Left(f_{1s}(v)))$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
519 |
\qquad otherwise: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
520 |
return $(r_{1s} + r_{2s}, f_{alt}(f_{1s}, f_{2s}))$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
521 |
\medskip\\ |
286
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
522 |
|
287
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
523 |
\quad case $r = r_1 \cdot r_2$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
524 |
\qquad let $(r_{1s}, f_{1s}) = simp(r_1)$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
525 |
\qquad \phantom{let} $(r_{2s}, f_{2s}) = simp(r_2)$\smallskip\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
526 |
\qquad case $r_{1s} = \varnothing$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
527 |
return $(\varnothing, f_{error})$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
528 |
\qquad case $r_{2s} = \varnothing$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
529 |
return $(\varnothing, f_{error})$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
530 |
\qquad case $r_{1s} = \epsilon$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
531 |
return $(r_{2s}, \lambda v. \,Seq(f_{1s}(Empty), f_{2s}(v)))$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
532 |
\qquad case $r_{2s} = \epsilon$: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
533 |
return $(r_{1s}, \lambda v. \,Seq(f_{1s}(v), f_{2s}(Empty)))$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
534 |
\qquad otherwise: |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
535 |
return $(r_{1s} \cdot r_{2s}, f_{seq}(f_{1s}, f_{2s}))$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
536 |
\medskip\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
537 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
538 |
\quad otherwise:\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
539 |
\qquad return $(r, \lambda v.\,v)$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
540 |
\end{tabular} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
541 |
\end{center} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
542 |
\caption{The simplification function that returns a simplified |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
543 |
regular expression and a rectification function.\label{simp}} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
544 |
\end{figure} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
545 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
546 |
\begin{center} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
547 |
\begin{tabular}{lcl} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
548 |
$lex\,r\,[]$ & $\dn$ & if $nullable(r)$ then $mkeps(r)$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
549 |
& & else $error$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
550 |
$lex\,r\,c\!::\!s$ & $\dn$ & let |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
551 |
$(r_{simp}, f_{rect}) = simp(der(c, r))$\\ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
552 |
& & $inj\,r\,c\,f_{rect}(lex\,r_{simp}\,s)$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
553 |
\end{tabular} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
554 |
\end{center} |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
555 |
|
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
556 |
\noindent This corresponds to the $matches$ function we |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
557 |
have seen in earlier lectures. In the first clause we are |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
558 |
given an empty string, $[]$, and need to test wether the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
559 |
regular expression is $nullable$. If yes we can proceed |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
560 |
normally and just return the value calculated by $mkeps$. |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
561 |
The second clause is for strings where the first character is |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
562 |
$c$ and the rest of the string is $s$. We first build the |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
563 |
derivative of $r$ with respect to $c$; simplify the resulting |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
564 |
regulare expression. We continue lexing with the simplified |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
565 |
regular expression and the string $s$. Whatever will be |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
566 |
returned as value, we sill rectify using the $f_{rect}$ |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
567 |
from the simplification and finally inject $c$ back into |
2c50b8b5886c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
286
diff
changeset
|
568 |
the (rectified) value. |
286
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
569 |
|
19020b75d75e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
285
diff
changeset
|
570 |
|
288
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
571 |
\subsubsection*{Records} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
572 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
573 |
Remember we want to tokenize input strings, that means |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
574 |
splitting strings into their ``word'' components. But |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
575 |
furthermore we want to classify each token as being a keyword |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
576 |
or identifier and so on. For this one more feature will be |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
577 |
required, which I call \emph{record}. While values record |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
578 |
precisely how a regular expression matches a string, |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
579 |
records can be used to focus on some particular |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
580 |
parts of the regular expression and forget about others. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
581 |
Let us look at an example. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
582 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
583 |
Suppose you have the regular expression $ab + ac$. Clearly |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
584 |
this regular expression can only recognise two strings. But |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
585 |
suppose you are not interested whether it can recognise $ab$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
586 |
or $ac$, but rather if it matched, then what was the last |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
587 |
character of the matched string\ldots either $b$ or $c$. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
588 |
You can do this by annotating the regular expression with |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
589 |
a record, written $(x:r)$, where $x$ is just an identifier |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
590 |
(in my implementation a plain string) and $r$ is a regular |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
591 |
expression. A record will be regarded as a regular expression. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
592 |
The extended definition in Scala looks as follows: |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
593 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
594 |
{\small\lstinputlisting[language=Scala] |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
595 |
{../progs/app03.scala}} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
596 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
597 |
\noindent Since we regard records as regular expressions |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
598 |
we need to extend the functions $nullable$ and $der$. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
599 |
Similarly $mkeps$ and $inj$ need to be extended and they |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
600 |
sometimes can return a particular value for records. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
601 |
This means we also need to extend the definition of values. |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
602 |
The extended definition in Scala looks as follows: |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
603 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
604 |
{\small\lstinputlisting[language=Scala] |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
605 |
{../progs/app04.scala}} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
606 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
607 |
\noindent Let us now look at the purpose of records more |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
608 |
closely and lets return to our question whether the string |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
609 |
terminated in a $b$ or $c$. We can do this as follows: we |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
610 |
annotate the regular expression $ab + ac$ with a record |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
611 |
as follows |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
612 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
613 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
614 |
$a(x:b) + a(x:c)$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
615 |
\end{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
616 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
617 |
\noindent This regular expression can still only recognise |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
618 |
the strings $ab$ and $ac$, but we can now use a function |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
619 |
that takes a value and returns all records. I call this |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
620 |
function \emph{env} for environment\ldots it builds a list |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
621 |
of identifiers associated with their string. This function |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
622 |
can be defined as follows: |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
623 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
624 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
625 |
\begin{tabular}{lcl} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
626 |
$env(Empty)$ & $\dn$ & $[]$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
627 |
$env(Char(c))$ & $\dn$ & $[]$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
628 |
$env(Left(v))$ & $\dn$ & $env(v)$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
629 |
$env(Right(v))$ & $\dn$ & $env(v)$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
630 |
$env(Seq(v_1,v_2))$& $\dn$ & $env(v_1) \,@\, env(v_2)$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
631 |
$env([v_1,\ldots ,v_n])$ & $\dn$ & |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
632 |
$env(v_1) \,@\ldots @\, env(v_n)$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
633 |
$env(Rec(x:v))$ & $\dn$ & $(x:|v|) :: env(v)$\\ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
634 |
\end{tabular} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
635 |
\end{center} |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
636 |
|
288
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
637 |
\noindent where in the last clause we use the flatten function |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
638 |
defined earlier. The function $env$ ``picks'' out all |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
639 |
underlying strings where a record is given. Since there can be |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
640 |
more than one, the environment will potentially contain |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
641 |
many ``recordings''. If we now postprocess the value |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
642 |
calculated by $lex$ extracting all recordings using $env$, |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
643 |
we can answer the question whether the last element in the |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
644 |
string was an $b$ or a $c$. Lets see this in action: if |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
645 |
we use $ab + ac$ and $ac$ the calculated value will be |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
646 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
647 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
648 |
$Right(Seq(Char(a), Char(c)))$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
649 |
\end{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
650 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
651 |
\noindent If we use instead $a(x:b) + a(x:c)$ and |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
652 |
use the $env$ function to extract the recording for |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
653 |
$x$ we obtain |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
654 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
655 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
656 |
$[(x:c)]$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
657 |
\end{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
658 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
659 |
\noindent If we had given the string $ab$ instead, then the |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
660 |
record would have been $[(x:b)]$. The fun starts if we |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
661 |
iterate this. Consider the regular expression |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
662 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
663 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
664 |
$(a(x:b) + a(y:c))^*$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
665 |
\end{center} |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
666 |
|
288
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
667 |
\noindent and the string $ababacabacab$. This string is |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
668 |
clearly matched by the regular expression, but we are only |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
669 |
interested in the sequence of $b$s and $c$s. Using $env$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
670 |
we obtain |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
671 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
672 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
673 |
$[(x:b), (x:b), (y:c), (x:b), (y:c), (x:b)]$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
674 |
\end{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
675 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
676 |
\noindent While this feature might look silly, it is in fact |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
677 |
quite useful. For example if we want to match the name of |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
678 |
an email we might use the regular expression |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
679 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
680 |
\[ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
681 |
(name: [a\mbox{-}z0\mbox{-}9\_\!\_\,.-]^+)\cdot @\cdot |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
682 |
(domain: [a\mbox{-}z0\mbox{-}9\,.-]^+)\cdot .\cdot |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
683 |
(top\_level: [a\mbox{-}z\,.]^{\{2,6\}}) |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
684 |
\] |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
685 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
686 |
\noindent Then if we match the email address |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
687 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
688 |
\[ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
689 |
\texttt{christian.urban@kcl.ac.uk} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
690 |
\] |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
691 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
692 |
\noindent we can use the $env$ function and find out |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
693 |
what the name, domain and top-level part of the email |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
694 |
address are: |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
695 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
696 |
\begin{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
697 |
$[(name:\texttt{christian.urban}), |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
698 |
(domain:\texttt{kcl}), |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
699 |
(top\_level:\texttt{ac.uk})]$ |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
700 |
\end{center} |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
701 |
|
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
702 |
\noindent As you will see in the next lecture, this is now all |
39aeca14af8c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
287
diff
changeset
|
703 |
we need to tokenise an input string and classify each token. |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
704 |
\end{document} |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
705 |
|
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
706 |
%%% Local Variables: |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
707 |
%%% mode: latex |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
708 |
%%% TeX-master: t |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
709 |
%%% End: |