author | Christian Urban <urbanc@in.tum.de> |
Wed, 31 May 2017 09:14:39 +0100 | |
changeset 495 | 7d9d86dc7aa0 |
parent 492 | 39b7ff2cf1bc |
child 497 | c498cb53a9a8 |
permissions | -rw-r--r-- |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
\documentclass{article} |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
2 |
\usepackage{../style} |
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
3 |
\usepackage{../langs} |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
4 |
\usepackage{../graphics} |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
480 | 6 |
|
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
\begin{document} |
480 | 8 |
\fnote{\copyright{} Christian Urban, King's College London, 2014, 2015, 2016, 2017} |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
|
482 | 10 |
\section*{Handout 3 (Finite Automata)} |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
488 | 12 |
|
480 | 13 |
Every formal language and compiler course I know of bombards you first |
14 |
with automata and then to a much, much smaller extend with regular |
|
15 |
expressions. As you can see, this course is turned upside down: |
|
16 |
regular expressions come first. The reason is that regular expressions |
|
17 |
are easier to reason about and the notion of derivatives, although |
|
18 |
already quite old, only became more widely known rather |
|
488 | 19 |
recently. Still, let us in this lecture have a closer look at automata |
480 | 20 |
and their relation to regular expressions. This will help us with |
21 |
understanding why the regular expression matchers in Python, Ruby and |
|
485 | 22 |
Java are so slow with certain regular expressions. On the way we will |
488 | 23 |
also see what are the limitations of regular |
24 |
expressions. Unfortunately, they cannot be used for \emph{everything}. |
|
482 | 25 |
|
26 |
||
27 |
\subsection*{Deterministic Finite Automata} |
|
28 |
||
485 | 29 |
Lets start\ldots the central definition is:\medskip |
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
30 |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
31 |
\noindent |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
32 |
A \emph{deterministic finite automaton} (DFA), say $A$, is |
484 | 33 |
given by a five-tuple written ${\cal A}(\varSigma, Qs, Q_0, F, \delta)$ where |
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
34 |
|
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
35 |
\begin{itemize} |
484 | 36 |
\item $\varSigma$ is an alphabet, |
482 | 37 |
\item $Qs$ is a finite set of states, |
38 |
\item $Q_0 \in Qs$ is the start state, |
|
39 |
\item $F \subseteq Qs$ are the accepting states, and |
|
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
40 |
\item $\delta$ is the transition function. |
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
41 |
\end{itemize} |
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
42 |
|
490 | 43 |
\noindent I am sure you have seen this definition already |
488 | 44 |
before. The transition function determines how to ``transition'' from |
45 |
one state to the next state with respect to a character. We have the |
|
46 |
assumption that these transition functions do not need to be defined |
|
47 |
everywhere: so it can be the case that given a character there is no |
|
48 |
next state, in which case we need to raise a kind of ``failure |
|
483 | 49 |
exception''. That means actually we have \emph{partial} functions as |
495 | 50 |
transitions---see the Scala implementation for DFAs later on. A |
484 | 51 |
typical example of a DFA is |
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
52 |
|
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
53 |
\begin{center} |
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
54 |
\begin{tikzpicture}[>=stealth',very thick,auto, |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
55 |
every state/.style={minimum size=0pt, |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
56 |
inner sep=2pt,draw=blue!50,very thick, |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
57 |
fill=blue!20},scale=2] |
482 | 58 |
\node[state,initial] (Q_0) {$Q_0$}; |
59 |
\node[state] (Q_1) [right=of Q_0] {$Q_1$}; |
|
60 |
\node[state] (Q_2) [below right=of Q_0] {$Q_2$}; |
|
61 |
\node[state] (Q_3) [right=of Q_2] {$Q_3$}; |
|
62 |
\node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$}; |
|
63 |
\path[->] (Q_0) edge node [above] {$a$} (Q_1); |
|
64 |
\path[->] (Q_1) edge node [above] {$a$} (Q_4); |
|
65 |
\path[->] (Q_4) edge [loop right] node {$a, b$} (); |
|
66 |
\path[->] (Q_3) edge node [right] {$a$} (Q_4); |
|
67 |
\path[->] (Q_2) edge node [above] {$a$} (Q_3); |
|
68 |
\path[->] (Q_1) edge node [right] {$b$} (Q_2); |
|
69 |
\path[->] (Q_0) edge node [above] {$b$} (Q_2); |
|
70 |
\path[->] (Q_2) edge [loop left] node {$b$} (); |
|
71 |
\path[->] (Q_3) edge [bend left=95, looseness=1.3] node [below] {$b$} (Q_0); |
|
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
72 |
\end{tikzpicture} |
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
73 |
\end{center} |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
|
482 | 75 |
\noindent In this graphical notation, the accepting state $Q_4$ is |
76 |
indicated with double circles. Note that there can be more than one |
|
77 |
accepting state. It is also possible that a DFA has no accepting |
|
485 | 78 |
state at all, or that the starting state is also an accepting |
482 | 79 |
state. In the case above the transition function is defined everywhere |
80 |
and can also be given as a table as follows: |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
81 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
82 |
\[ |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
83 |
\begin{array}{lcl} |
482 | 84 |
(Q_0, a) &\rightarrow& Q_1\\ |
85 |
(Q_0, b) &\rightarrow& Q_2\\ |
|
86 |
(Q_1, a) &\rightarrow& Q_4\\ |
|
87 |
(Q_1, b) &\rightarrow& Q_2\\ |
|
88 |
(Q_2, a) &\rightarrow& Q_3\\ |
|
89 |
(Q_2, b) &\rightarrow& Q_2\\ |
|
90 |
(Q_3, a) &\rightarrow& Q_4\\ |
|
91 |
(Q_3, b) &\rightarrow& Q_0\\ |
|
92 |
(Q_4, a) &\rightarrow& Q_4\\ |
|
93 |
(Q_4, b) &\rightarrow& Q_4\\ |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
94 |
\end{array} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
95 |
\] |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
96 |
|
495 | 97 |
\noindent |
98 |
Please check that this table represents the same transition function |
|
99 |
as the graph above. |
|
100 |
||
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
101 |
We need to define the notion of what language is accepted by |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
102 |
an automaton. For this we lift the transition function |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
103 |
$\delta$ from characters to strings as follows: |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
104 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
105 |
\[ |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
106 |
\begin{array}{lcl} |
484 | 107 |
\widehat{\delta}(q, []) & \dn & q\\ |
108 |
\widehat{\delta}(q, c\!::\!s) & \dn & \widehat{\delta}(\delta(q, c), s)\\ |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
109 |
\end{array} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
110 |
\] |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
111 |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
112 |
\noindent This lifted transition function is often called |
485 | 113 |
\emph{delta-hat}. Given a string, we start in the starting state and |
114 |
take the first character of the string, follow to the next state, then |
|
115 |
take the second character and so on. Once the string is exhausted and |
|
116 |
we end up in an accepting state, then this string is accepted by the |
|
480 | 117 |
automaton. Otherwise it is not accepted. This also means that if along |
118 |
the way we hit the case where the transition function $\delta$ is not |
|
119 |
defined, we need to raise an error. In our implementation we will deal |
|
485 | 120 |
with this case elegantly by using Scala's \texttt{Try}. Summing up: a |
121 |
string $s$ is in the \emph{language accepted by the automaton} ${\cal |
|
484 | 122 |
A}(\varSigma, Q, Q_0, F, \delta)$ iff |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
123 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
124 |
\[ |
484 | 125 |
\widehat{\delta}(Q_0, s) \in F |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
126 |
\] |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
127 |
|
488 | 128 |
\noindent I let you think about a definition that describes the set of |
490 | 129 |
all strings accepted by a deterministic finite automaton. |
480 | 130 |
|
131 |
\begin{figure}[p] |
|
132 |
\small |
|
490 | 133 |
\lstinputlisting[numbers=left]{../progs/display/dfa.scala} |
482 | 134 |
\caption{A Scala implementation of DFAs using partial functions. |
488 | 135 |
Note some subtleties: \texttt{deltas} implements the delta-hat |
487 | 136 |
construction by lifting the (partial) transition function to lists |
485 | 137 |
of characters. Since \texttt{delta} is given as a partial function, |
138 |
it can obviously go ``wrong'' in which case the \texttt{Try} in |
|
139 |
\texttt{accepts} catches the error and returns \texttt{false}---that |
|
140 |
means the string is not accepted. The example \texttt{delta} in |
|
141 |
Line 28--38 implements the DFA example shown earlier in the |
|
142 |
handout.\label{dfa}} |
|
480 | 143 |
\end{figure} |
144 |
||
485 | 145 |
My take on an implementation of DFAs in Scala is given in |
483 | 146 |
Figure~\ref{dfa}. As you can see, there are many features of the |
482 | 147 |
mathematical definition that are quite closely reflected in the |
148 |
code. In the DFA-class, there is a starting state, called |
|
483 | 149 |
\texttt{start}, with the polymorphic type \texttt{A}. There is a |
150 |
partial function \texttt{delta} for specifying the transitions---these |
|
151 |
partial functions take a state (of polymorphic type \texttt{A}) and an |
|
152 |
input (of polymorphic type \texttt{C}) and produce a new state (of |
|
153 |
type \texttt{A}). For the moment it is OK to assume that \texttt{A} is |
|
154 |
some arbitrary type for states and the input is just characters. (The |
|
485 | 155 |
reason for not having concrete types, but polymorphic types for the |
156 |
states and the input of DFAs will become clearer later on.) |
|
483 | 157 |
|
485 | 158 |
The DFA-class has also an argument for specifying final states. In the |
159 |
implementation it is not a set of states, as in the mathematical |
|
160 |
definition, but a function from states to booleans (this function is |
|
161 |
supposed to return true whenever a state is final; false |
|
162 |
otherwise). While this boolean function is different from the sets of |
|
163 |
states, Scala allows to use sets for such functions (see Line 40 where |
|
164 |
the DFA is initialised). Again it will become clear later on why I use |
|
165 |
functions for final states, rather than sets. |
|
166 |
||
167 |
The most important point in the implementation is that I use Scala's |
|
484 | 168 |
partial functions for representing the transitions; alternatives would |
169 |
have been \texttt{Maps} or even \texttt{Lists}. One of the main |
|
170 |
advantages of using partial functions is that transitions can be quite |
|
171 |
nicely defined by a series of \texttt{case} statements (see Lines 28 |
|
172 |
-- 38 for an example). If you need to represent an automaton with a |
|
173 |
sink state (catch-all-state), you can use Scala's pattern matching and |
|
174 |
write something like |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
175 |
|
490 | 176 |
{\small\begin{lstlisting}[language=Scala] |
480 | 177 |
abstract class State |
178 |
... |
|
179 |
case object Sink extends State |
|
180 |
||
181 |
val delta : (State, Char) :=> State = |
|
182 |
{ case (S0, 'a') => S1 |
|
183 |
case (S1, 'a') => S2 |
|
184 |
case _ => Sink |
|
185 |
} |
|
186 |
\end{lstlisting}} |
|
187 |
||
485 | 188 |
\noindent I let you think what the corresponding DFA looks like in the |
189 |
graph notation. |
|
484 | 190 |
|
485 | 191 |
Finally, I let you ponder whether this is a good implementation of |
192 |
DFAs or not. In doing so I hope you notice that the $\varSigma$ and |
|
193 |
$Qs$ components (the alphabet and the set of finite states, |
|
194 |
respectively) are missing from the class definition. This means that |
|
195 |
the implementation allows you to do some fishy things you are not |
|
196 |
meant to do with DFAs. Which fishy things could that be? |
|
480 | 197 |
|
482 | 198 |
|
199 |
||
200 |
\subsection*{Non-Deterministic Finite Automata} |
|
201 |
||
485 | 202 |
Remember we want to find out what the relation is between regular |
203 |
expressions and automata. To do this with DFAs is a bit unwieldy. |
|
204 |
While with DFAs it is always clear that given a state and a character |
|
205 |
what the next state is (potentially none), it will be convenient to |
|
206 |
relax this restriction. That means we allow states to have several |
|
482 | 207 |
potential successor states. We even allow more than one starting |
484 | 208 |
state. The resulting construction is called a \emph{Non-Deterministic |
485 | 209 |
Finite Automaton} (NFA) given also as a five-tuple ${\cal |
210 |
A}(\varSigma, Qs, Q_{0s}, F, \rho)$ where |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
211 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
212 |
\begin{itemize} |
484 | 213 |
\item $\varSigma$ is an alphabet, |
482 | 214 |
\item $Qs$ is a finite set of states |
215 |
\item $Q_{0s}$ is a set of start states ($Q_{0s} \subseteq Qs$) |
|
216 |
\item $F$ are some accepting states with $F \subseteq Qs$, and |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
217 |
\item $\rho$ is a transition relation. |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
218 |
\end{itemize} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
219 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
220 |
\noindent |
483 | 221 |
A typical example of a NFA is |
482 | 222 |
|
223 |
% A NFA for (ab* + b)*a |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
224 |
\begin{center} |
483 | 225 |
\begin{tikzpicture}[>=stealth',very thick, auto, |
226 |
every state/.style={minimum size=0pt,inner sep=3pt, |
|
227 |
draw=blue!50,very thick,fill=blue!20},scale=2] |
|
482 | 228 |
\node[state,initial] (Q_0) {$Q_0$}; |
229 |
\node[state] (Q_1) [right=of Q_0] {$Q_1$}; |
|
230 |
\node[state, accepting] (Q_2) [right=of Q_1] {$Q_2$}; |
|
231 |
\path[->] (Q_0) edge [loop above] node {$b$} (); |
|
232 |
\path[<-] (Q_0) edge node [below] {$b$} (Q_1); |
|
233 |
\path[->] (Q_0) edge [bend left] node [above] {$a$} (Q_1); |
|
234 |
\path[->] (Q_0) edge [bend right] node [below] {$a$} (Q_2); |
|
235 |
\path[->] (Q_1) edge [loop above] node {$a,b$} (); |
|
236 |
\path[->] (Q_1) edge node [above] {$a$} (Q_2); |
|
237 |
\end{tikzpicture} |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
238 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
239 |
|
482 | 240 |
\noindent |
241 |
This NFA happens to have only one starting state, but in general there |
|
495 | 242 |
could be more than one. Notice that in state $Q_0$ we might go to |
243 |
state $Q_1$ \emph{or} to state $Q_2$ when receiving an $a$. Similarly |
|
244 |
in state $Q_1$ and receiving an $a$, we can stay in state $Q_1$ |
|
245 |
\emph{or} go to $Q_2$. This kind of choice is not allowed with |
|
246 |
DFAs. The downside of this choice in NFAs is that when it comes to |
|
247 |
deciding whether a string is accepted by a NFA we potentially have to |
|
248 |
explore all possibilities. I let you think which strings the above NFA |
|
249 |
accepts. |
|
482 | 250 |
|
251 |
||
485 | 252 |
There are a number of additional points you should note about |
483 | 253 |
NFAs. Every DFA is a NFA, but not vice versa. The $\rho$ in NFAs is a |
254 |
transition \emph{relation} (DFAs have a transition function). The |
|
255 |
difference between a function and a relation is that a function has |
|
256 |
always a single output, while a relation gives, roughly speaking, |
|
257 |
several outputs. Look again at the NFA above: if you are currently in |
|
258 |
the state $Q_1$ and you read a character $b$, then you can transition |
|
259 |
to either $Q_0$ \emph{or} $Q_2$. Which route, or output, you take is |
|
260 |
not determined. This non-determinism can be represented by a |
|
261 |
relation. |
|
482 | 262 |
|
483 | 263 |
My implementation of NFAs in Scala is shown in Figure~\ref{nfa}. |
264 |
Perhaps interestingly, I do not actually use relations for my NFAs, |
|
485 | 265 |
but use transition functions that return sets of states. DFAs have |
266 |
partial transition functions that return a single state; my NFAs |
|
488 | 267 |
return a set of states. I let you think about this representation for |
485 | 268 |
NFA-transitions and how it corresponds to the relations used in the |
487 | 269 |
mathematical definition of NFAs. An example of a transition function |
488 | 270 |
in Scala for the NFA shown above is |
482 | 271 |
|
490 | 272 |
{\small\begin{lstlisting}[language=Scala] |
487 | 273 |
val nfa_delta : (State, Char) :=> Set[State] = |
274 |
{ case (Q0, 'a') => Set(Q1, Q2) |
|
275 |
case (Q0, 'b') => Set(Q0) |
|
276 |
case (Q1, 'a') => Set(Q1, Q2) |
|
277 |
case (Q1, 'b') => Set(Q0, Q1) } |
|
278 |
\end{lstlisting}} |
|
279 |
||
490 | 280 |
Like in the mathematical definition, \texttt{starts} is in |
487 | 281 |
NFAs a set of states; \texttt{fins} is again a function from states to |
485 | 282 |
booleans. The \texttt{next} function calculates the set of next states |
283 |
reachable from a single state \texttt{q} by a character~\texttt{c}. In |
|
284 |
case there is no such state---the partial transition function is |
|
285 |
undefined---the empty set is returned (see function |
|
286 |
\texttt{applyOrElse} in Lines 9 and 10). The function \texttt{nexts} |
|
287 |
just lifts this function to sets of states. |
|
288 |
||
484 | 289 |
\begin{figure}[p] |
482 | 290 |
\small |
490 | 291 |
\lstinputlisting[numbers=left]{../progs/display/nfa.scala} |
485 | 292 |
\caption{A Scala implementation of NFAs using partial functions. |
293 |
Notice that the function \texttt{accepts} implements the |
|
294 |
acceptance of a string in a breath-first search fashion. This can be a costly |
|
295 |
way of deciding whether a string is accepted or not in applications that need to handle |
|
296 |
large NFAs and large inputs.\label{nfa}} |
|
482 | 297 |
\end{figure} |
298 |
||
485 | 299 |
Look very careful at the \texttt{accepts} and \texttt{deltas} |
300 |
functions in NFAs and remember that when accepting a string by a NFA |
|
484 | 301 |
we might have to explore all possible transitions (recall which state |
485 | 302 |
to go to is not unique anymore with NFAs\ldots{}we need to explore |
303 |
potentially all next states). The implementation achieves this |
|
487 | 304 |
exploration through a \emph{breadth-first search}. This is fine for |
485 | 305 |
small NFAs, but can lead to real memory problems when the NFAs are |
306 |
bigger and larger strings need to be processed. As result, some |
|
307 |
regular expression matching engines resort to a \emph{depth-first |
|
308 |
search} with \emph{backtracking} in unsuccessful cases. In our |
|
309 |
implementation we can implement a depth-first version of |
|
310 |
\texttt{accepts} using Scala's \texttt{exists}-function as follows: |
|
483 | 311 |
|
312 |
||
490 | 313 |
{\small\begin{lstlisting}[language=Scala] |
483 | 314 |
def search(q: A, s: List[C]) : Boolean = s match { |
315 |
case Nil => fins(q) |
|
485 | 316 |
case c::cs => next(q, c).exists(search(_, cs)) |
483 | 317 |
} |
318 |
||
485 | 319 |
def accepts2(s: List[C]) : Boolean = |
483 | 320 |
starts.exists(search(_, s)) |
321 |
\end{lstlisting}} |
|
322 |
||
323 |
\noindent |
|
487 | 324 |
This depth-first way of exploration seems to work quite efficiently in |
325 |
many examples and is much less of a strain on memory. The problem is |
|
326 |
that the backtracking can get ``catastrophic'' in some |
|
327 |
examples---remember the catastrophic backtracking from earlier |
|
328 |
lectures. This depth-first search with backtracking is the reason for |
|
329 |
the abysmal performance of some regular expression matchings in Java, |
|
330 |
Ruby and Python. I like to show you this in the next two sections. |
|
482 | 331 |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
332 |
|
490 | 333 |
\subsection*{Epsilon NFAs} |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
334 |
|
485 | 335 |
In order to get an idea what calculations are performed by Java \& |
336 |
friends, we need a method for transforming a regular expression into |
|
337 |
an automaton. This automaton should accept exactly those strings that |
|
338 |
are accepted by the regular expression. The simplest and most |
|
339 |
well-known method for this is called \emph{Thompson Construction}, |
|
340 |
after the Turing Award winner Ken Thompson. This method is by |
|
487 | 341 |
recursion over regular expressions and depends on the non-determinism |
488 | 342 |
in NFAs described in the previous section. You will see shortly why |
487 | 343 |
this construction works well with NFAs, but is not so straightforward |
344 |
with DFAs. |
|
345 |
||
346 |
Unfortunately we are still one step away from our intended target |
|
347 |
though---because this construction uses a version of NFAs that allows |
|
348 |
``silent transitions''. The idea behind silent transitions is that |
|
349 |
they allow us to go from one state to the next without having to |
|
350 |
consume a character. We label such silent transition with the letter |
|
351 |
$\epsilon$ and call the automata $\epsilon$NFAs. Two typical examples |
|
352 |
of $\epsilon$NFAs are: |
|
484 | 353 |
|
354 |
||
485 | 355 |
\begin{center} |
356 |
\begin{tabular}[t]{c@{\hspace{9mm}}c} |
|
357 |
\begin{tikzpicture}[>=stealth',very thick, |
|
358 |
every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},] |
|
359 |
\node[state,initial] (Q_0) {$Q_0$}; |
|
360 |
\node[state] (Q_1) [above=of Q_0] {$Q_1$}; |
|
361 |
\node[state, accepting] (Q_2) [below=of Q_0] {$Q_2$}; |
|
362 |
\path[->] (Q_0) edge node [left] {$\epsilon$} (Q_1); |
|
363 |
\path[->] (Q_0) edge node [left] {$\epsilon$} (Q_2); |
|
364 |
\path[->] (Q_0) edge [loop right] node {$a$} (); |
|
365 |
\path[->] (Q_1) edge [loop right] node {$a$} (); |
|
366 |
\path[->] (Q_2) edge [loop right] node {$b$} (); |
|
367 |
\end{tikzpicture} & |
|
368 |
||
369 |
\raisebox{20mm}{ |
|
370 |
\begin{tikzpicture}[>=stealth',very thick, |
|
371 |
every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},] |
|
372 |
\node[state,initial] (r_1) {$R_1$}; |
|
373 |
\node[state] (r_2) [above=of r_1] {$R_2$}; |
|
374 |
\node[state, accepting] (r_3) [right=of r_1] {$R_3$}; |
|
375 |
\path[->] (r_1) edge node [below] {$b$} (r_3); |
|
376 |
\path[->] (r_2) edge [bend left] node [above] {$a$} (r_3); |
|
377 |
\path[->] (r_1) edge [bend left] node [left] {$\epsilon$} (r_2); |
|
378 |
\path[->] (r_2) edge [bend left] node [right] {$a$} (r_1); |
|
379 |
\end{tikzpicture}} |
|
380 |
\end{tabular} |
|
381 |
\end{center} |
|
382 |
||
383 |
\noindent |
|
487 | 384 |
Consider the $\epsilon$NFA on the left-hand side: the |
385 |
$\epsilon$-transitions mean you do not have to ``consume'' any part of |
|
386 |
the input string, but ``silently'' change to a different state. In |
|
387 |
this example, if you are in the starting state $Q_0$, you can silently |
|
388 |
move either to $Q_1$ or $Q_2$. You can see that once you are in $Q_1$, |
|
389 |
respectively $Q_2$, you cannot ``go back'' to the other states. So it |
|
490 | 390 |
seems allowing $\epsilon$-transitions is a rather substantial |
487 | 391 |
extension to NFAs. On first appearances, $\epsilon$-transitions might |
392 |
even look rather strange, or even silly. To start with, silent |
|
393 |
transitions make the decision whether a string is accepted by an |
|
394 |
automaton even harder: with $\epsilon$NFAs we have to look whether we |
|
395 |
can do first some $\epsilon$-transitions and then do a |
|
396 |
``proper''-transition; and after any ``proper''-transition we again |
|
397 |
have to check whether we can do again some silent transitions. Even |
|
398 |
worse, if there is a silent transition pointing back to the same |
|
399 |
state, then we have to be careful our decision procedure for strings |
|
400 |
does not loop (remember the depth-first search for exploring all |
|
401 |
states). |
|
485 | 402 |
|
403 |
The obvious question is: Do we get anything in return for this hassle |
|
404 |
with silent transitions? Well, we still have to work for it\ldots |
|
405 |
unfortunately. If we were to follow the many textbooks on the |
|
406 |
subject, we would now start with defining what $\epsilon$NFAs |
|
407 |
are---that would require extending the transition relation of |
|
490 | 408 |
NFAs. Next, we would show that the $\epsilon$NFAs are equivalent to |
488 | 409 |
NFAs and so on. Once we have done all this on paper, we would need to |
410 |
implement $\epsilon$NFAs. Lets try to take a shortcut instead. We are |
|
411 |
not really interested in $\epsilon$NFAs; they are only a convenient |
|
412 |
tool for translating regular expressions into automata. So we are not |
|
413 |
going to implementing them explicitly, but translate them immediately |
|
414 |
into NFAs (in a sense $\epsilon$NFAs are just a convenient API for |
|
415 |
lazy people ;o). How does the translation work? Well we have to find |
|
416 |
all transitions of the form |
|
485 | 417 |
|
418 |
\[ |
|
419 |
q\stackrel{\epsilon}{\longrightarrow}\ldots\stackrel{\epsilon}{\longrightarrow} |
|
420 |
\;\stackrel{a}{\longrightarrow}\; |
|
421 |
\stackrel{\epsilon}{\longrightarrow}\ldots\stackrel{\epsilon}{\longrightarrow} q' |
|
422 |
\] |
|
423 |
||
492 | 424 |
\noindent where somewhere in the ``middle'' is an $a$-transition. We |
425 |
replace them with $q \stackrel{a}{\longrightarrow} q'$. Doing this to |
|
426 |
the $\epsilon$NFA on the right-hand side above gives the NFA |
|
485 | 427 |
|
428 |
\begin{center} |
|
429 |
\begin{tikzpicture}[>=stealth',very thick, |
|
430 |
every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},] |
|
431 |
\node[state,initial] (r_1) {$R_1$}; |
|
432 |
\node[state] (r_2) [above=of r_1] {$R_2$}; |
|
433 |
\node[state, accepting] (r_3) [right=of r_1] {$R_3$}; |
|
434 |
\path[->] (r_1) edge node [above] {$b$} (r_3); |
|
435 |
\path[->] (r_2) edge [bend left] node [above] {$a$} (r_3); |
|
436 |
\path[->] (r_1) edge [bend left] node [left] {$a$} (r_2); |
|
437 |
\path[->] (r_2) edge [bend left] node [right] {$a$} (r_1); |
|
438 |
\path[->] (r_1) edge [loop below] node {$a$} (); |
|
439 |
\path[->] (r_1) edge [bend right] node [below] {$a$} (r_3); |
|
440 |
\end{tikzpicture} |
|
441 |
\end{center} |
|
442 |
||
487 | 443 |
\noindent where the single $\epsilon$-transition is replaced by |
444 |
three additional $a$-transitions. Please do the calculations yourself |
|
445 |
and verify that I did not forget any transition. |
|
446 |
||
447 |
So in what follows, whenever we are given an $\epsilon$NFA we will |
|
488 | 448 |
replace it by an equivalent NFA. The Scala code for this translation |
449 |
is given in Figure~\ref{enfa}. The main workhorse in this code is a |
|
450 |
function that calculates a fixpoint of function (Lines 5--10). This |
|
451 |
function is used for ``discovering'' which states are reachable by |
|
487 | 452 |
$\epsilon$-transitions. Once no new state is discovered, a fixpoint is |
453 |
reached. This is used for example when calculating the starting states |
|
454 |
of an equivalent NFA (see Line 36): we start with all starting states |
|
455 |
of the $\epsilon$NFA and then look for all additional states that can |
|
456 |
be reached by $\epsilon$-transitions. We keep on doing this until no |
|
457 |
new state can be reached. This is what the $\epsilon$-closure, named |
|
458 |
in the code \texttt{ecl}, calculates. Similarly, an accepting state of |
|
459 |
the NFA is when we can reach an accepting state of the $\epsilon$NFA |
|
460 |
by $\epsilon$-transitions. |
|
461 |
||
485 | 462 |
|
463 |
\begin{figure}[p] |
|
464 |
\small |
|
490 | 465 |
\lstinputlisting[numbers=left]{../progs/display/enfa.scala} |
485 | 466 |
|
467 |
\caption{A Scala function that translates $\epsilon$NFA into NFAs. The |
|
490 | 468 |
transition function of $\epsilon$NFA takes as input an \texttt{Option[C]}. |
485 | 469 |
\texttt{None} stands for an $\epsilon$-transition; \texttt{Some(c)} |
488 | 470 |
for a ``proper'' transition consuming a character. The functions in |
471 |
Lines 18--26 calculate |
|
485 | 472 |
all states reachable by one or more $\epsilon$-transition for a given |
491 | 473 |
set of states. The NFA is constructed in Lines 36--38. |
474 |
Note the interesting commands in Lines 5 and 6: their purpose is |
|
475 |
to ensure that \texttt{fixpT} is the tail-recursive version of |
|
476 |
the fixpoint construction; otherwise we would quickly get a |
|
477 |
stack-overflow exception, even on small examples, due to limitations |
|
478 |
of the JVM. |
|
479 |
\label{enfa}} |
|
485 | 480 |
\end{figure} |
481 |
||
487 | 482 |
Also look carefully how the transitions of $\epsilon$NFAs are |
483 |
implemented. The additional possibility of performing silent |
|
484 |
transitions is encoded by using \texttt{Option[C]} as the type for the |
|
490 | 485 |
``input''. The \texttt{Some}s stand for ``proper'' transitions where |
487 | 486 |
a character is consumed; \texttt{None} stands for |
487 |
$\epsilon$-transitions. The transition functions for the two |
|
488 |
$\epsilon$NFAs from the beginning of this section can be defined as |
|
485 | 489 |
|
490 | 490 |
{\small\begin{lstlisting}[language=Scala] |
487 | 491 |
val enfa_trans1 : (State, Option[Char]) :=> Set[State] = |
492 |
{ case (Q0, Some('a')) => Set(Q0) |
|
493 |
case (Q0, None) => Set(Q1, Q2) |
|
494 |
case (Q1, Some('a')) => Set(Q1) |
|
495 |
case (Q2, Some('b')) => Set(Q2) } |
|
496 |
||
497 |
val enfa_trans2 : (State, Option[Char]) :=> Set[State] = |
|
498 |
{ case (R1, Some('b')) => Set(R3) |
|
499 |
case (R1, None) => Set(R2) |
|
500 |
case (R2, Some('a')) => Set(R1, R3) } |
|
501 |
\end{lstlisting}} |
|
502 |
||
503 |
\noindent |
|
504 |
I hope you agree now with my earlier statement that the $\epsilon$NFAs |
|
505 |
are just an API for NFAs. |
|
506 |
||
490 | 507 |
\subsection*{Thompson Construction} |
487 | 508 |
|
509 |
Having the translation of $\epsilon$NFAs to NFAs in place, we can |
|
510 |
finally return to the problem of translating regular expressions into |
|
511 |
equivalent NFAs. Recall that by equivalent we mean that the NFAs |
|
485 | 512 |
recognise the same language. Consider the simple regular expressions |
513 |
$\ZERO$, $\ONE$ and $c$. They can be translated into equivalent NFAs |
|
514 |
as follows: |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
515 |
|
488 | 516 |
\begin{equation}\mbox{ |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
517 |
\begin{tabular}[t]{l@{\hspace{10mm}}l} |
444
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
349
diff
changeset
|
518 |
\raisebox{1mm}{$\ZERO$} & |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
519 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 520 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
521 |
\end{tikzpicture}\\\\ |
444
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
349
diff
changeset
|
522 |
\raisebox{1mm}{$\ONE$} & |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
523 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 524 |
\node[state, initial, accepting] (Q_0) {$\mbox{}$}; |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
525 |
\end{tikzpicture}\\\\ |
487 | 526 |
\raisebox{3mm}{$c$} & |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
527 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 528 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
529 |
\node[state, accepting] (Q_1) [right=of Q_0] {$\mbox{}$}; |
|
530 |
\path[->] (Q_0) edge node [below] {$c$} (Q_1); |
|
487 | 531 |
\end{tikzpicture}\\ |
488 | 532 |
\end{tabular}}\label{simplecases} |
533 |
\end{equation} |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
534 |
|
487 | 535 |
\noindent |
536 |
I let you think whether the NFAs can match exactly those strings the |
|
537 |
regular expressions can match. To do this translation in code we need |
|
495 | 538 |
a way to construct states ``programatically''...and as an additional |
539 |
constraint Scala needs to recognise that these states are being distinct. |
|
487 | 540 |
For this I implemented in Figure~\ref{thompson1} a class |
541 |
\texttt{TState} that includes a counter and a companion object that |
|
488 | 542 |
increases this counter whenever a new state is created.\footnote{You might |
543 |
have to read up what \emph{companion objects} do in Scala.} |
|
487 | 544 |
|
485 | 545 |
\begin{figure}[p] |
546 |
\small |
|
490 | 547 |
\lstinputlisting[numbers=left]{../progs/display/thompson1.scala} |
487 | 548 |
\caption{The first part of the Thompson Construction. Lines 7--16 |
488 | 549 |
implement a way of how to create new states that are all |
487 | 550 |
distinct by virtue of a counter. This counter is |
551 |
increased in the companion object of \texttt{TState} |
|
552 |
whenever a new state is created. The code in Lines 24--40 |
|
488 | 553 |
constructs NFAs for the simple regular expressions $\ZERO$, $\ONE$ and $c$. |
495 | 554 |
Compare this code with the pictures given in \eqref{simplecases} on |
555 |
Page~\pageref{simplecases}. |
|
487 | 556 |
\label{thompson1}} |
485 | 557 |
\end{figure} |
558 |
||
487 | 559 |
\begin{figure}[p] |
560 |
\small |
|
490 | 561 |
\lstinputlisting[numbers=left]{../progs/display/thompson2.scala} |
487 | 562 |
\caption{The second part of the Thompson Construction implementing |
490 | 563 |
the composition of NFAs according to $\cdot$, $+$ and ${}^*$. |
487 | 564 |
The implicit class about rich partial functions |
565 |
implements the infix operation \texttt{+++} which |
|
566 |
combines an $\epsilon$NFA transition with a NFA transition |
|
495 | 567 |
(both are given as partial functions---but with different type!).\label{thompson2}} |
487 | 568 |
\end{figure} |
485 | 569 |
|
488 | 570 |
The case for the sequence regular expression $r_1 \cdot r_2$ is a bit more |
489 | 571 |
complicated: Say, we are given by recursion two NFAs representing the regular |
488 | 572 |
expressions $r_1$ and $r_2$ respectively. |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
573 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
574 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
575 |
\begin{tikzpicture}[node distance=3mm, |
488 | 576 |
>=stealth',very thick, |
577 |
every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
|
482 | 578 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
488 | 579 |
\node[state, initial] (Q_01) [below=1mm of Q_0] {$\mbox{}$}; |
580 |
\node[state, initial] (Q_02) [above=1mm of Q_0] {$\mbox{}$}; |
|
581 |
\node (R_1) [right=of Q_0] {$\ldots$}; |
|
582 |
\node[state, accepting] (T_1) [right=of R_1] {$\mbox{}$}; |
|
583 |
\node[state, accepting] (T_2) [above=of T_1] {$\mbox{}$}; |
|
584 |
\node[state, accepting] (T_3) [below=of T_1] {$\mbox{}$}; |
|
585 |
||
586 |
\node (A_0) [right=2.5cm of T_1] {$\mbox{}$}; |
|
587 |
\node[state, initial] (A_01) [above=1mm of A_0] {$\mbox{}$}; |
|
588 |
\node[state, initial] (A_02) [below=1mm of A_0] {$\mbox{}$}; |
|
589 |
||
590 |
\node (b_1) [right=of A_0] {$\ldots$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
591 |
\node[state, accepting] (c_1) [right=of b_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
592 |
\node[state, accepting] (c_2) [above=of c_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
593 |
\node[state, accepting] (c_3) [below=of c_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
594 |
\begin{pgfonlayer}{background} |
488 | 595 |
\node (1) [rounded corners, inner sep=1mm, thick, |
596 |
draw=black!60, fill=black!20, fit= (Q_0) (R_1) (T_1) (T_2) (T_3)] {}; |
|
597 |
\node (2) [rounded corners, inner sep=1mm, thick, |
|
598 |
draw=black!60, fill=black!20, fit= (A_0) (b_1) (c_1) (c_2) (c_3)] {}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
599 |
\node [yshift=2mm] at (1.north) {$r_1$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
600 |
\node [yshift=2mm] at (2.north) {$r_2$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
601 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
602 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
603 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
604 |
|
488 | 605 |
\noindent The first NFA has some accepting states and the second some |
489 | 606 |
starting states. We obtain an $\epsilon$NFA for $r_1\cdot r_2$ by |
607 |
connecting the accepting states of the first NFA with |
|
608 |
$\epsilon$-transitions to the starting states of the second |
|
609 |
automaton. By doing so we make the accepting states of the first NFA |
|
610 |
to be non-accepting like so: |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
611 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
612 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
613 |
\begin{tikzpicture}[node distance=3mm, |
488 | 614 |
>=stealth',very thick, |
615 |
every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
|
482 | 616 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
488 | 617 |
\node[state, initial] (Q_01) [below=1mm of Q_0] {$\mbox{}$}; |
618 |
\node[state, initial] (Q_02) [above=1mm of Q_0] {$\mbox{}$}; |
|
482 | 619 |
\node (r_1) [right=of Q_0] {$\ldots$}; |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
620 |
\node[state] (t_1) [right=of r_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
621 |
\node[state] (t_2) [above=of t_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
622 |
\node[state] (t_3) [below=of t_1] {$\mbox{}$}; |
488 | 623 |
|
624 |
\node (A_0) [right=2.5cm of t_1] {$\mbox{}$}; |
|
625 |
\node[state] (A_01) [above=1mm of A_0] {$\mbox{}$}; |
|
626 |
\node[state] (A_02) [below=1mm of A_0] {$\mbox{}$}; |
|
627 |
||
628 |
\node (b_1) [right=of A_0] {$\ldots$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
629 |
\node[state, accepting] (c_1) [right=of b_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
630 |
\node[state, accepting] (c_2) [above=of c_1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
631 |
\node[state, accepting] (c_3) [below=of c_1] {$\mbox{}$}; |
488 | 632 |
\path[->] (t_1) edge (A_01); |
492 | 633 |
\path[->] (t_2) edge node [above] {$\epsilon$s} (A_01); |
488 | 634 |
\path[->] (t_3) edge (A_01); |
635 |
\path[->] (t_1) edge (A_02); |
|
636 |
\path[->] (t_2) edge (A_02); |
|
492 | 637 |
\path[->] (t_3) edge node [below] {$\epsilon$s} (A_02); |
488 | 638 |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
639 |
\begin{pgfonlayer}{background} |
488 | 640 |
\node (3) [rounded corners, inner sep=1mm, thick, |
641 |
draw=black!60, fill=black!20, fit= (Q_0) (c_1) (c_2) (c_3)] {}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
642 |
\node [yshift=2mm] at (3.north) {$r_1\cdot r_2$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
643 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
644 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
645 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
646 |
|
489 | 647 |
\noindent The idea behind this construction is that the start of any |
648 |
string is first recognised by the first NFA, then we silently change |
|
649 |
to the second NFA; the ending of the string is recognised by the |
|
650 |
second NFA...just like matching of a string by the regular expression |
|
490 | 651 |
$r_1\cdot r_2$. The Scala code for this construction is given in |
489 | 652 |
Figure~\ref{thompson2} in Lines 16--23. The starting states of the |
653 |
$\epsilon$NFA are the starting states of the first NFA (corresponding |
|
654 |
to $r_1$); the accepting function is the accepting function of the |
|
655 |
second NFA (corresponding to $r_2$). The new transition function is |
|
656 |
all the ``old'' transitions plus the $\epsilon$-transitions connecting |
|
657 |
the accepting states of the first NFA to the starting states of the |
|
490 | 658 |
first NFA (Lines 18 and 19). The $\epsilon$NFA is then immediately |
489 | 659 |
translated in a NFA. |
660 |
||
661 |
||
490 | 662 |
The case for the alternative regular expression $r_1 + r_2$ is |
663 |
slightly different: We are given by recursion two NFAs representing |
|
664 |
$r_1$ and $r_2$ respectively. Each NFA has some starting states and |
|
665 |
some accepting states. We obtain a NFA for the regular expression $r_1 |
|
666 |
+ r_2$ by composing the transition functions (this crucially depends |
|
495 | 667 |
on knowing that the states of each component NFA are distinct---recall |
668 |
we implemented for this to hold some bespoke code for states). We also |
|
669 |
need to combine the starting states and accepting functions |
|
670 |
appropriately. |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
671 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
672 |
\begin{center} |
490 | 673 |
\begin{tabular}[t]{ccc} |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
674 |
\begin{tikzpicture}[node distance=3mm, |
488 | 675 |
>=stealth',very thick, |
490 | 676 |
every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20}, |
677 |
baseline=(current bounding box.center)] |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
678 |
\node at (0,0) (1) {$\mbox{}$}; |
489 | 679 |
\node (2) [above=10mm of 1] {}; |
680 |
\node[state, initial] (4) [above=1mm of 2] {$\mbox{}$}; |
|
681 |
\node[state, initial] (5) [below=1mm of 2] {$\mbox{}$}; |
|
682 |
\node[state, initial] (3) [below=10mm of 1] {$\mbox{}$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
683 |
|
489 | 684 |
\node (a) [right=of 2] {$\ldots\,$}; |
685 |
\node (a1) [right=of a] {$$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
686 |
\node[state, accepting] (a2) [above=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
687 |
\node[state, accepting] (a3) [below=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
688 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
689 |
\node (b) [right=of 3] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
690 |
\node[state, accepting] (b1) [right=of b] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
691 |
\node[state, accepting] (b2) [above=of b1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
692 |
\node[state, accepting] (b3) [below=of b1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
693 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
694 |
\node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
695 |
\node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (3) (b1) (b2) (b3)] {}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
696 |
\node [yshift=3mm] at (1.north) {$r_1$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
697 |
\node [yshift=3mm] at (2.north) {$r_2$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
698 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
699 |
\end{tikzpicture} |
490 | 700 |
& |
701 |
\mbox{}\qquad\tikz{\draw[>=stealth,line width=2mm,->] (0,0) -- (1, 0)}\quad\mbox{} |
|
702 |
& |
|
489 | 703 |
\begin{tikzpicture}[node distance=3mm, |
704 |
>=stealth',very thick, |
|
490 | 705 |
every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20}, |
706 |
baseline=(current bounding box.center)] |
|
489 | 707 |
\node at (0,0) (1) {$\mbox{}$}; |
708 |
\node (2) [above=10mm of 1] {$$}; |
|
709 |
\node[state, initial] (4) [above=1mm of 2] {$\mbox{}$}; |
|
710 |
\node[state, initial] (5) [below=1mm of 2] {$\mbox{}$}; |
|
711 |
\node[state, initial] (3) [below=10mm of 1] {$\mbox{}$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
712 |
|
489 | 713 |
\node (a) [right=of 2] {$\ldots\,$}; |
714 |
\node (a1) [right=of a] {$$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
715 |
\node[state, accepting] (a2) [above=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
716 |
\node[state, accepting] (a3) [below=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
717 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
718 |
\node (b) [right=of 3] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
719 |
\node[state, accepting] (b1) [right=of b] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
720 |
\node[state, accepting] (b2) [above=of b1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
721 |
\node[state, accepting] (b3) [below=of b1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
722 |
|
489 | 723 |
%\path[->] (1) edge node [above] {$\epsilon$} (2); |
724 |
%\path[->] (1) edge node [below] {$\epsilon$} (3); |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
725 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
726 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
727 |
\node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3) (b2) (b3)] {}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
728 |
\node [yshift=3mm] at (3.north) {$r_1+ r_2$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
729 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
730 |
\end{tikzpicture} |
490 | 731 |
\end{tabular} |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
732 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
733 |
|
489 | 734 |
\noindent The code for this construction is in Figure~\ref{thompson2} |
490 | 735 |
in Lines 25--33. |
736 |
||
737 |
Finally for the $*$-case we have a NFA for $r$ and connect its |
|
738 |
accepting states to a new starting state via |
|
739 |
$\epsilon$-transitions. This new starting state is also an accepting |
|
740 |
state, because $r^*$ can recognise the empty string. |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
741 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
742 |
\begin{center} |
495 | 743 |
\begin{tabular}[b]{@{}ccc@{}} |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
744 |
\begin{tikzpicture}[node distance=3mm, |
490 | 745 |
>=stealth',very thick, |
746 |
every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20}, |
|
747 |
baseline=(current bounding box.north)] |
|
495 | 748 |
\node (2) {$\mbox{}$}; |
749 |
\node[state, initial] (4) [above=1mm of 2] {$\mbox{}$}; |
|
750 |
\node[state, initial] (5) [below=1mm of 2] {$\mbox{}$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
751 |
\node (a) [right=of 2] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
752 |
\node[state, accepting] (a1) [right=of a] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
753 |
\node[state, accepting] (a2) [above=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
754 |
\node[state, accepting] (a3) [below=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
755 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
756 |
\node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
757 |
\node [yshift=3mm] at (1.north) {$r$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
758 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
759 |
\end{tikzpicture} |
490 | 760 |
& |
761 |
\raisebox{-16mm}{\;\tikz{\draw[>=stealth,line width=2mm,->] (0,0) -- (1, 0)}} |
|
762 |
& |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
763 |
\begin{tikzpicture}[node distance=3mm, |
489 | 764 |
>=stealth',very thick, |
490 | 765 |
every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20}, |
766 |
baseline=(current bounding box.north)] |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
767 |
\node at (0,0) [state, initial,accepting] (1) {$\mbox{}$}; |
495 | 768 |
\node (2) [right=16mm of 1] {$\mbox{}$}; |
769 |
\node[state] (4) [above=1mm of 2] {$\mbox{}$}; |
|
770 |
\node[state] (5) [below=1mm of 2] {$\mbox{}$}; |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
771 |
\node (a) [right=of 2] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
772 |
\node[state] (a1) [right=of a] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
773 |
\node[state] (a2) [above=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
774 |
\node[state] (a3) [below=of a1] {$\mbox{}$}; |
495 | 775 |
\path[->] (1) edge node [below] {$\epsilon$} (4); |
776 |
\path[->] (1) edge node [below] {$\epsilon$} (5); |
|
777 |
\path[->] (a1) edge [bend left=45] node [below] {$\epsilon$} (1); |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
778 |
\path[->] (a2) edge [bend right] node [below] {$\epsilon$} (1); |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
779 |
\path[->] (a3) edge [bend left=45] node [below] {$\epsilon$} (1); |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
780 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
781 |
\node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3)] {}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
782 |
\node [yshift=3mm] at (2.north) {$r^*$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
783 |
\end{pgfonlayer} |
490 | 784 |
\end{tikzpicture} |
785 |
\end{tabular} |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
786 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
787 |
|
490 | 788 |
\noindent |
789 |
The corresponding code is in Figure~\ref{thompson2} in Lines 35--43) |
|
489 | 790 |
|
490 | 791 |
To sum up, you can see in the sequence and star cases the need of |
489 | 792 |
having silent $\epsilon$-transitions. Similarly the alternative case |
490 | 793 |
shows the need of the NFA-nondeterminism. It seems awkward to form the |
489 | 794 |
`alternative' composition of two DFAs, because DFA do not allow |
795 |
several starting and successor states. All these constructions can now |
|
796 |
be put together in the following recursive function: |
|
797 |
||
798 |
||
490 | 799 |
{\small\begin{lstlisting}[language=Scala] |
800 |
def thompson(r: Rexp) : NFAt = r match { |
|
488 | 801 |
case ZERO => NFA_ZERO() |
802 |
case ONE => NFA_ONE() |
|
803 |
case CHAR(c) => NFA_CHAR(c) |
|
804 |
case ALT(r1, r2) => NFA_ALT(thompson(r1), thompson(r2)) |
|
805 |
case SEQ(r1, r2) => NFA_SEQ(thompson(r1), thompson(r2)) |
|
806 |
case STAR(r1) => NFA_STAR(thompson(r1)) |
|
807 |
} |
|
808 |
\end{lstlisting}} |
|
809 |
||
489 | 810 |
\noindent |
490 | 811 |
It calculates a NFA from a regular expressions. At last we can run |
812 |
NFAs for the our evil regular expression examples. The graph on the |
|
813 |
left shows that when translating a regular expression such as |
|
814 |
$a^{\{n\}}$ into a NFA, the size can blow up and then even the |
|
815 |
relative fast (on small examples) breadth-first search can be |
|
816 |
slow. The graph on the right shows that with `evil' regular |
|
817 |
expressions the depth-first search can be abysmally slow. Even if the |
|
818 |
graphs not completely overlap with the curves of Python, Ruby and |
|
819 |
Java, they are similar enough. OK\ldots now you know why regular |
|
820 |
expression matchers in those languages are so slow. |
|
489 | 821 |
|
488 | 822 |
|
823 |
\begin{center} |
|
824 |
\begin{tabular}{@{\hspace{-1mm}}c@{\hspace{1mm}}c@{}} |
|
825 |
\begin{tikzpicture} |
|
826 |
\begin{axis}[ |
|
490 | 827 |
title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings |
489 | 828 |
$\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$}, |
490 | 829 |
title style={yshift=-2ex}, |
489 | 830 |
xlabel={$n$}, |
831 |
x label style={at={(1.05,0.0)}}, |
|
832 |
ylabel={time in secs}, |
|
833 |
enlargelimits=false, |
|
834 |
xtick={0,5,...,30}, |
|
835 |
xmax=33, |
|
836 |
ymax=35, |
|
837 |
ytick={0,5,...,30}, |
|
838 |
scaled ticks=false, |
|
839 |
axis lines=left, |
|
840 |
width=5.5cm, |
|
490 | 841 |
height=4cm, |
842 |
legend entries={Python,Ruby, breadth-first NFA}, |
|
843 |
legend style={at={(0.5,-0.25)},anchor=north,font=\small}, |
|
489 | 844 |
legend cell align=left] |
845 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python.data}; |
|
846 |
\addplot[brown,mark=triangle*, mark options={fill=white}] table {re-ruby.data}; |
|
847 |
% breath-first search in NFAs |
|
848 |
\addplot[red,mark=*, mark options={fill=white}] table { |
|
849 |
1 0.00586 |
|
850 |
2 0.01209 |
|
851 |
3 0.03076 |
|
852 |
4 0.08269 |
|
853 |
5 0.12881 |
|
854 |
6 0.25146 |
|
855 |
7 0.51377 |
|
856 |
8 0.89079 |
|
857 |
9 1.62802 |
|
858 |
10 3.05326 |
|
859 |
11 5.92437 |
|
860 |
12 11.67863 |
|
861 |
13 24.00568 |
|
862 |
}; |
|
863 |
\end{axis} |
|
864 |
\end{tikzpicture} |
|
865 |
& |
|
866 |
\begin{tikzpicture} |
|
867 |
\begin{axis}[ |
|
490 | 868 |
title={Graph: $(a^*)^* \cdot b$ and strings |
488 | 869 |
$\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$}, |
490 | 870 |
title style={yshift=-2ex}, |
488 | 871 |
xlabel={$n$}, |
872 |
x label style={at={(1.05,0.0)}}, |
|
873 |
ylabel={time in secs}, |
|
874 |
enlargelimits=false, |
|
875 |
xtick={0,5,...,30}, |
|
876 |
xmax=33, |
|
877 |
ymax=35, |
|
878 |
ytick={0,5,...,30}, |
|
879 |
scaled ticks=false, |
|
880 |
axis lines=left, |
|
881 |
width=5.5cm, |
|
490 | 882 |
height=4cm, |
883 |
legend entries={Python, Java, depth-first NFA}, |
|
884 |
legend style={at={(0.5,-0.25)},anchor=north,font=\small}, |
|
488 | 885 |
legend cell align=left] |
886 |
\addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data}; |
|
887 |
\addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data}; |
|
888 |
% depth-first search in NFAs |
|
889 |
\addplot[red,mark=*, mark options={fill=white}] table { |
|
890 |
1 0.00605 |
|
891 |
2 0.03086 |
|
892 |
3 0.11994 |
|
893 |
4 0.45389 |
|
894 |
5 2.06192 |
|
895 |
6 8.04894 |
|
896 |
7 32.63549 |
|
897 |
}; |
|
898 |
\end{axis} |
|
899 |
\end{tikzpicture} |
|
900 |
\end{tabular} |
|
901 |
\end{center} |
|
902 |
||
903 |
||
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
904 |
|
490 | 905 |
\subsection*{Subset Construction} |
906 |
||
491 | 907 |
Of course, some developers of regular expression matchers are aware of |
908 |
these problems with sluggish NFAs and try to address them. One common |
|
909 |
technique for alleviating the problem I like to show you in this |
|
910 |
section. This will also explain why we insisted on polymorphic types in |
|
911 |
our DFA code (remember I used \texttt{A} and \texttt{C} for the types |
|
912 |
of states and the input, see Figure~\ref{dfa} on |
|
913 |
Page~\pageref{dfa}).\bigskip |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
914 |
|
490 | 915 |
\noindent |
491 | 916 |
To start remember that we did not bother with defining and |
917 |
implementing $\epsilon$NFAs: we immediately translated them into |
|
490 | 918 |
equivalent NFAs. Equivalent in the sense of accepting the same |
919 |
language (though we only claimed this and did not prove it |
|
920 |
rigorously). Remember also that NFAs have non-deterministic |
|
921 |
transitions defined as a relation or implemented as function returning |
|
922 |
sets of states. This non-determinism is crucial for the Thompson |
|
923 |
Construction to work (recall the cases for $\cdot$, $+$ and |
|
924 |
${}^*$). But this non-determinism makes it harder with NFAs to decide |
|
491 | 925 |
when a string is accepted or not; whereas such a decision is rather |
490 | 926 |
straightforward with DFAs: recall their transition function is a |
491 | 927 |
\emph{function} that returns a single state. So with DFAs we do not |
928 |
have to search at all. What is perhaps interesting is the fact that |
|
929 |
for every NFA we can find a DFA that also recognises the same |
|
930 |
language. This might sound a bit paradoxical: NFA $\rightarrow$ |
|
931 |
decision of acceptance hard; DFA $\rightarrow$ decision easy. But this |
|
932 |
\emph{is} true\ldots but of course there is always a caveat---nothing |
|
933 |
ever is for free in life. |
|
488 | 934 |
|
491 | 935 |
There are actually a number of methods for transforming a NFA into |
936 |
an equivalent DFA, but the most famous one is the \emph{subset |
|
490 | 937 |
construction}. Consider the following NFA where the states are |
491 | 938 |
labelled with $0$, $1$ and $2$. |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
939 |
|
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
940 |
\begin{center} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
941 |
\begin{tabular}{c@{\hspace{10mm}}c} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
942 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
943 |
every state/.style={minimum size=0pt, |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
944 |
draw=blue!50,very thick,fill=blue!20}, |
490 | 945 |
baseline=(current bounding box.center)] |
482 | 946 |
\node[state,initial] (Q_0) {$0$}; |
490 | 947 |
\node[state] (Q_1) [below=of Q_0] {$1$}; |
948 |
\node[state, accepting] (Q_2) [below=of Q_1] {$2$}; |
|
949 |
||
950 |
\path[->] (Q_0) edge node [right] {$b$} (Q_1); |
|
951 |
\path[->] (Q_1) edge node [right] {$a,b$} (Q_2); |
|
952 |
\path[->] (Q_0) edge [loop above] node {$a, b$} (); |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
953 |
\end{tikzpicture} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
954 |
& |
490 | 955 |
\begin{tabular}{r|ll} |
956 |
states & $a$ & $b$\\ |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
957 |
\hline |
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
958 |
$\{\}\phantom{\star}$ & $\{\}$ & $\{\}$\\ |
490 | 959 |
start: $\{0\}\phantom{\star}$ & $\{0\}$ & $\{0,1\}$\\ |
960 |
$\{1\}\phantom{\star}$ & $\{2\}$ & $\{2\}$\\ |
|
961 |
$\{2\}\star$ & $\{\}$ & $\{\}$\\ |
|
962 |
$\{0,1\}\phantom{\star}$ & $\{0,2\}$ & $\{0,1,2\}$\\ |
|
963 |
$\{0,2\}\star$ & $\{0\}$ & $\{0,1\}$\\ |
|
964 |
$\{1,2\}\star$ & $\{2\}$ & $\{2\}$\\ |
|
965 |
$\{0,1,2\}\star$ & $\{0,2\}$ & $\{0,1,2\}$\\ |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
966 |
\end{tabular} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
967 |
\end{tabular} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
968 |
\end{center} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
969 |
|
490 | 970 |
\noindent The states of the corresponding DFA are given by generating |
491 | 971 |
all subsets of the set $\{0,1,2\}$ (seen in the states column |
490 | 972 |
in the table on the right). The other columns define the transition |
491 | 973 |
function for the DFA for inputs $a$ and $b$. The first row states that |
490 | 974 |
$\{\}$ is the sink state which has transitions for $a$ and $b$ to |
975 |
itself. The next three lines are calculated as follows: |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
976 |
|
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
977 |
\begin{itemize} |
490 | 978 |
\item Suppose you calculate the entry for the $a$-transition for state |
979 |
$\{0\}$. Look for all states in the NFA that can be reached by such |
|
980 |
a transition from this state; this is only state $0$; therefore from |
|
981 |
state $\{0\}$ we can go to state $\{0\}$ via an $a$-transition. |
|
982 |
\item Do the same for the $b$-transition; you can reach states $0$ and |
|
983 |
$1$ in the NFA; therefore in the DFA we can go from state $\{0\}$ to |
|
984 |
state $\{0,1\}$ via an $b$-transition. |
|
985 |
\item Continue with the states $\{1\}$ and $\{2\}$. |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
986 |
\end{itemize} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
987 |
|
491 | 988 |
\noindent |
989 |
Once you filled in the transitions for `simple' states $\{0\}$ |
|
990 |
.. $\{2\}$, you only have to build the union for the compound states |
|
991 |
$\{0,1\}$, $\{0,2\}$ and so on. For example for $\{0,1\}$ you take the |
|
992 |
union of Line $\{0\}$ and Line $\{1\}$, which gives $\{0,2\}$ for $a$, |
|
993 |
and $\{0,1,2\}$ for $b$. And so on. |
|
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
994 |
|
491 | 995 |
The starting state of the DFA can be calculated from the starting |
996 |
states of the NFA, that is in this case $\{0\}$. But in general there |
|
997 |
can of course be many starting states in the NFA and you would take |
|
998 |
the corresponding subset as \emph{the} starting state of the DFA. |
|
999 |
||
1000 |
The accepting states in the DFA are given by all sets that contain a |
|
1001 |
$2$, which is the only accpting state in this NFA. But again in |
|
1002 |
general if the subset contains any accepting state from the NFA, then |
|
1003 |
the corresponding state in the DFA is accepting as well. This |
|
1004 |
completes the subset construction. The corresponding DFA for the NFA |
|
1005 |
shown above is: |
|
1006 |
||
1007 |
\begin{equation} |
|
490 | 1008 |
\begin{tikzpicture}[scale=0.8,>=stealth',very thick, |
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1009 |
every state/.style={minimum size=0pt, |
491 | 1010 |
draw=blue!50,very thick,fill=blue!20}, |
1011 |
baseline=(current bounding box.center)] |
|
490 | 1012 |
\node[state,initial] (q0) {$0$}; |
1013 |
\node[state] (q01) [right=of q0] {$0,1$}; |
|
1014 |
\node[state,accepting] (q02) [below=of q01] {$0,2$}; |
|
1015 |
\node[state,accepting] (q012) [right=of q02] {$0,1,2$}; |
|
1016 |
\node[state] (q1) [below=0.5cm of q0] {$1$}; |
|
1017 |
\node[state,accepting] (q2) [below=1cm of q1] {$2$}; |
|
1018 |
\node[state] (qn) [below left=1cm of q2] {$\{\}$}; |
|
1019 |
\node[state,accepting] (q12) [below right=1cm of q2] {$1,2$}; |
|
1020 |
||
1021 |
\path[->] (q0) edge node [above] {$b$} (q01); |
|
1022 |
\path[->] (q01) edge node [above] {$b$} (q012); |
|
1023 |
\path[->] (q0) edge [loop above] node {$a$} (); |
|
1024 |
\path[->] (q012) edge [loop right] node {$b$} (); |
|
1025 |
\path[->] (q012) edge node [below] {$a$} (q02); |
|
1026 |
\path[->] (q02) edge node [below] {$a$} (q0); |
|
1027 |
\path[->] (q01) edge [bend left] node [left] {$a$} (q02); |
|
1028 |
\path[->] (q02) edge [bend left] node [right] {$b$} (q01); |
|
1029 |
\path[->] (q1) edge node [left] {$a,b$} (q2); |
|
1030 |
\path[->] (q12) edge node [right] {$a, b$} (q2); |
|
1031 |
\path[->] (q2) edge node [right] {$a, b$} (qn); |
|
1032 |
\path[->] (qn) edge [loop left] node {$a,b$} (); |
|
491 | 1033 |
\end{tikzpicture}\label{subsetdfa} |
1034 |
\end{equation} |
|
490 | 1035 |
|
1036 |
\noindent |
|
1037 |
Please check that this is indeed a DFA. The big question is whether |
|
491 | 1038 |
this DFA can recognise the same language as the NFA we started with? |
490 | 1039 |
I let you ponder about this question. |
1040 |
||
1041 |
||
491 | 1042 |
There are also two points to note: One is that very often in the |
1043 |
subset construction the resulting DFA contains a number of ``dead'' |
|
1044 |
states that are never reachable from the starting state. This is |
|
1045 |
obvious in the example, where state $\{1\}$, $\{2\}$, $\{1,2\}$ and |
|
1046 |
$\{\}$ can never be reached from the starting state. But this might |
|
1047 |
not always be as obvious as that. In effect the DFA in this example is |
|
1048 |
not a \emph{minimal} DFA (more about this in a minute). Such dead |
|
1049 |
states can be safely removed without changing the language that is |
|
1050 |
recognised by the DFA. Another point is that in some cases, however, |
|
1051 |
the subset construction produces a DFA that does \emph{not} contain |
|
1052 |
any dead states\ldots{}this means it calculates a minimal DFA. Which |
|
1053 |
in turn means that in some cases the number of states can by going |
|
1054 |
from NFAs to DFAs exponentially increase, namely by $2^n$ (which is |
|
1055 |
the number of subsets you can form for sets of $n$ states). This blow |
|
1056 |
up in the number of states in the DFA is again bad news for how |
|
1057 |
quickly you can decide whether a string is accepted by a DFA or |
|
1058 |
not. So the caveat with DFAs is that they might make the task of |
|
1059 |
finding the next state trival, but might require $2^n$ times as many |
|
1060 |
states then a NFA.\bigskip |
|
490 | 1061 |
|
491 | 1062 |
\noindent |
1063 |
To conclude this section, how conveniently we can |
|
1064 |
implement the subset construction with our versions of NFAs and |
|
1065 |
DFAs? Very conveninetly. The code is just: |
|
490 | 1066 |
|
1067 |
{\small\begin{lstlisting}[language=Scala] |
|
1068 |
def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = { |
|
1069 |
DFA(nfa.starts, |
|
1070 |
{ case (qs, c) => nfa.nexts(qs, c) }, |
|
1071 |
_.exists(nfa.fins)) |
|
1072 |
} |
|
1073 |
\end{lstlisting}} |
|
1074 |
||
491 | 1075 |
\noindent |
1076 |
The interesting point in this code is that the state type of the |
|
1077 |
calculated DFA is \texttt{Set[A]}. Think carefully that this works out |
|
1078 |
correctly. |
|
490 | 1079 |
|
491 | 1080 |
The DFA is then given by three components: the starting states, the |
1081 |
transition function and the accepting-states function. The starting |
|
1082 |
states are a set in the given NFA, but a single state in the DFA. The |
|
1083 |
transition function, given the state \texttt{qs} and input \texttt{c}, |
|
1084 |
needs to produce the next state: this is the set of all NFA states |
|
1085 |
that are reachable from each state in \texttt{qs}. The function |
|
1086 |
\texttt{nexts} from the NFA class already calculates this for us. The |
|
1087 |
accepting-states function for the DFA is true henevner at least one |
|
1088 |
state in the subset is accepting (that is true) in the NFA.\medskip |
|
1089 |
||
1090 |
\noindent |
|
495 | 1091 |
You might be able to spend some quality time tinkering with this code |
1092 |
and time to ponder about it. Then you will probably notice that it is |
|
1093 |
actually a bit silly. The whole point of translating the NFA into a |
|
1094 |
DFA via the subset construction is to make the decision of whether a |
|
1095 |
string is accepted or not faster. Given the code above, the generated |
|
1096 |
DFA will be exactly as fast, or as slow, as the NFA we started with |
|
1097 |
(actually it will even be a tiny bit slower). The reason is that we |
|
1098 |
just re-use the \texttt{nexts} function from the NFA. This function |
|
1099 |
implements the non-deterministic breadth-first search. You might be |
|
1100 |
thinking: This is cheating! \ldots{} Well, not quite as you will see |
|
1101 |
later, but in terms of speed we still need to work a bit in order to |
|
1102 |
get sometimes(!) a faster DFA. Let's do this next. |
|
490 | 1103 |
|
1104 |
\subsection*{DFA Minimisation} |
|
1105 |
||
491 | 1106 |
As seen in \eqref{subsetdfa}, the subset construction from NFA to a |
1107 |
DFA can result in a rather ``inefficient'' DFA. Meaning there are |
|
1108 |
states that are not needed. There are two kinds of such unneeded |
|
495 | 1109 |
states: \emph{unreachable} states and \emph{non-distinguishable} |
491 | 1110 |
states. The first kind of states can just be removed without affecting |
1111 |
the language that can be recognised (after all they are |
|
1112 |
unreachable). The second kind can also be recognised and thus a DFA |
|
1113 |
can be \emph{minimised} by the following algorithm: |
|
490 | 1114 |
|
1115 |
\begin{enumerate} |
|
1116 |
\item Take all pairs $(q, p)$ with $q \not= p$ |
|
1117 |
\item Mark all pairs that accepting and non-accepting states |
|
1118 |
\item For all unmarked pairs $(q, p)$ and all characters $c$ |
|
1119 |
test whether |
|
1120 |
||
1121 |
\begin{center} |
|
1122 |
$(\delta(q, c), \delta(p,c))$ |
|
1123 |
\end{center} |
|
1124 |
||
1125 |
are marked. If there is one, then also mark $(q, p)$. |
|
1126 |
\item Repeat last step until no change. |
|
1127 |
\item All unmarked pairs can be merged. |
|
1128 |
\end{enumerate} |
|
1129 |
||
491 | 1130 |
\noindent Unfortunately, once we throw away all unreachable states in |
1131 |
\eqref{subsetdfa}, all remaining states are needed. In order to |
|
1132 |
illustrate the minimisation algorithm, consider the following DFA. |
|
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1133 |
|
490 | 1134 |
\begin{center} |
1135 |
\begin{tikzpicture}[>=stealth',very thick,auto, |
|
1136 |
every state/.style={minimum size=0pt, |
|
1137 |
inner sep=2pt,draw=blue!50,very thick, |
|
1138 |
fill=blue!20}] |
|
1139 |
\node[state,initial] (Q_0) {$Q_0$}; |
|
1140 |
\node[state] (Q_1) [right=of Q_0] {$Q_1$}; |
|
1141 |
\node[state] (Q_2) [below right=of Q_0] {$Q_2$}; |
|
1142 |
\node[state] (Q_3) [right=of Q_2] {$Q_3$}; |
|
1143 |
\node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$}; |
|
1144 |
\path[->] (Q_0) edge node [above] {$a$} (Q_1); |
|
1145 |
\path[->] (Q_1) edge node [above] {$a$} (Q_4); |
|
1146 |
\path[->] (Q_4) edge [loop right] node {$a, b$} (); |
|
1147 |
\path[->] (Q_3) edge node [right] {$a$} (Q_4); |
|
1148 |
\path[->] (Q_2) edge node [above] {$a$} (Q_3); |
|
1149 |
\path[->] (Q_1) edge node [right] {$b$} (Q_2); |
|
1150 |
\path[->] (Q_0) edge node [above] {$b$} (Q_2); |
|
1151 |
\path[->] (Q_2) edge [loop left] node {$b$} (); |
|
1152 |
\path[->] (Q_3) edge [bend left=95, looseness=1.3] node |
|
1153 |
[below] {$b$} (Q_0); |
|
1154 |
\end{tikzpicture} |
|
1155 |
\end{center} |
|
1156 |
||
1157 |
\noindent In Step 1 and 2 we consider essentially a triangle |
|
1158 |
of the form |
|
1159 |
||
1160 |
\begin{center} |
|
1161 |
\begin{tikzpicture}[scale=0.6,line width=0.8mm] |
|
1162 |
\draw (0,0) -- (4,0); |
|
1163 |
\draw (0,1) -- (4,1); |
|
1164 |
\draw (0,2) -- (3,2); |
|
1165 |
\draw (0,3) -- (2,3); |
|
1166 |
\draw (0,4) -- (1,4); |
|
1167 |
||
1168 |
\draw (0,0) -- (0, 4); |
|
1169 |
\draw (1,0) -- (1, 4); |
|
1170 |
\draw (2,0) -- (2, 3); |
|
1171 |
\draw (3,0) -- (3, 2); |
|
1172 |
\draw (4,0) -- (4, 1); |
|
1173 |
||
1174 |
\draw (0.5,-0.5) node {$Q_0$}; |
|
1175 |
\draw (1.5,-0.5) node {$Q_1$}; |
|
1176 |
\draw (2.5,-0.5) node {$Q_2$}; |
|
1177 |
\draw (3.5,-0.5) node {$Q_3$}; |
|
1178 |
||
1179 |
\draw (-0.5, 3.5) node {$Q_1$}; |
|
1180 |
\draw (-0.5, 2.5) node {$Q_2$}; |
|
1181 |
\draw (-0.5, 1.5) node {$Q_3$}; |
|
1182 |
\draw (-0.5, 0.5) node {$Q_4$}; |
|
1183 |
||
1184 |
\draw (0.5,0.5) node {\large$\star$}; |
|
1185 |
\draw (1.5,0.5) node {\large$\star$}; |
|
1186 |
\draw (2.5,0.5) node {\large$\star$}; |
|
1187 |
\draw (3.5,0.5) node {\large$\star$}; |
|
1188 |
\end{tikzpicture} |
|
1189 |
\end{center} |
|
1190 |
||
1191 |
\noindent where the lower row is filled with stars, because in |
|
1192 |
the corresponding pairs there is always one state that is |
|
1193 |
accepting ($Q_4$) and a state that is non-accepting (the other |
|
1194 |
states). |
|
1195 |
||
491 | 1196 |
In Step 3 we need to fill in more stars according whether |
490 | 1197 |
one of the next-state pairs are marked. We have to do this |
1198 |
for every unmarked field until there is no change anymore. |
|
1199 |
This gives the triangle |
|
1200 |
||
1201 |
\begin{center} |
|
1202 |
\begin{tikzpicture}[scale=0.6,line width=0.8mm] |
|
1203 |
\draw (0,0) -- (4,0); |
|
1204 |
\draw (0,1) -- (4,1); |
|
1205 |
\draw (0,2) -- (3,2); |
|
1206 |
\draw (0,3) -- (2,3); |
|
1207 |
\draw (0,4) -- (1,4); |
|
1208 |
||
1209 |
\draw (0,0) -- (0, 4); |
|
1210 |
\draw (1,0) -- (1, 4); |
|
1211 |
\draw (2,0) -- (2, 3); |
|
1212 |
\draw (3,0) -- (3, 2); |
|
1213 |
\draw (4,0) -- (4, 1); |
|
1214 |
||
1215 |
\draw (0.5,-0.5) node {$Q_0$}; |
|
1216 |
\draw (1.5,-0.5) node {$Q_1$}; |
|
1217 |
\draw (2.5,-0.5) node {$Q_2$}; |
|
1218 |
\draw (3.5,-0.5) node {$Q_3$}; |
|
1219 |
||
1220 |
\draw (-0.5, 3.5) node {$Q_1$}; |
|
1221 |
\draw (-0.5, 2.5) node {$Q_2$}; |
|
1222 |
\draw (-0.5, 1.5) node {$Q_3$}; |
|
1223 |
\draw (-0.5, 0.5) node {$Q_4$}; |
|
1224 |
||
1225 |
\draw (0.5,0.5) node {\large$\star$}; |
|
1226 |
\draw (1.5,0.5) node {\large$\star$}; |
|
1227 |
\draw (2.5,0.5) node {\large$\star$}; |
|
1228 |
\draw (3.5,0.5) node {\large$\star$}; |
|
1229 |
\draw (0.5,1.5) node {\large$\star$}; |
|
1230 |
\draw (2.5,1.5) node {\large$\star$}; |
|
1231 |
\draw (0.5,3.5) node {\large$\star$}; |
|
1232 |
\draw (1.5,2.5) node {\large$\star$}; |
|
1233 |
\end{tikzpicture} |
|
1234 |
\end{center} |
|
1235 |
||
1236 |
\noindent which means states $Q_0$ and $Q_2$, as well as $Q_1$ |
|
1237 |
and $Q_3$ can be merged. This gives the following minimal DFA |
|
1238 |
||
1239 |
\begin{center} |
|
1240 |
\begin{tikzpicture}[>=stealth',very thick,auto, |
|
1241 |
every state/.style={minimum size=0pt, |
|
1242 |
inner sep=2pt,draw=blue!50,very thick, |
|
1243 |
fill=blue!20}] |
|
1244 |
\node[state,initial] (Q_02) {$Q_{0, 2}$}; |
|
1245 |
\node[state] (Q_13) [right=of Q_02] {$Q_{1, 3}$}; |
|
1246 |
\node[state, accepting] (Q_4) [right=of Q_13] |
|
1247 |
{$Q_{4\phantom{,0}}$}; |
|
1248 |
\path[->] (Q_02) edge [bend left] node [above] {$a$} (Q_13); |
|
1249 |
\path[->] (Q_13) edge [bend left] node [below] {$b$} (Q_02); |
|
1250 |
\path[->] (Q_02) edge [loop below] node {$b$} (); |
|
1251 |
\path[->] (Q_13) edge node [above] {$a$} (Q_4); |
|
1252 |
\path[->] (Q_4) edge [loop above] node {$a, b$} (); |
|
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1253 |
\end{tikzpicture} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1254 |
\end{center} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1255 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1256 |
|
492 | 1257 |
By the way, we are not bothering with implementing the above |
1258 |
minimisation algorith: while up to now all the transformations used |
|
1259 |
some clever composition of functions, the minimisation algorithm |
|
1260 |
cannot be implemented by just composing some functions. For this we |
|
1261 |
would require a more concrete representation of the transition |
|
1262 |
function (like maps). If we did this, however, then many advantages of |
|
1263 |
the functions would be thrown away. So the compromise is to not being |
|
1264 |
able to minimise (easily) our DFAs. |
|
1265 |
||
490 | 1266 |
\subsection*{Brzozowski's Method} |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1267 |
|
495 | 1268 |
I know this handout is already a long, long rant: but after all it is |
1269 |
a topic that has been researched for more than 60 years. If you |
|
1270 |
reflect on what you have read so far, the story is that you can take a |
|
1271 |
regular expression, translate it via the Thompson Construction into an |
|
491 | 1272 |
$\epsilon$NFA, then translate it into a NFA by removing all |
1273 |
$\epsilon$-transitions, and then via the subset construction obtain a |
|
1274 |
DFA. In all steps we made sure the language, or which strings can be |
|
495 | 1275 |
recognised, stays the same. Of couse we should have proved this in |
1276 |
each step, but let us cut corners here. After the last section, we |
|
1277 |
can even minimise the DFA (maybe not in code). But again we made sure |
|
1278 |
the same language is recognised. You might be wondering: Can we go |
|
1279 |
into the other direction? Can we go from a DFA and obtain a regular |
|
1280 |
expression that can recognise the same language as the DFA?\medskip |
|
491 | 1281 |
|
1282 |
\noindent |
|
1283 |
The answer is yes. Again there are several methods for calculating a |
|
1284 |
regular expression for a DFA. I will show you Brzozowski's method |
|
1285 |
because it calculates a regular expression using quite familiar |
|
1286 |
transformations for solving equational systems. Consider the DFA: |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1287 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1288 |
\begin{center} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1289 |
\begin{tikzpicture}[scale=1.5,>=stealth',very thick,auto, |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1290 |
every state/.style={minimum size=0pt, |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1291 |
inner sep=2pt,draw=blue!50,very thick, |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1292 |
fill=blue!20}] |
482 | 1293 |
\node[state, initial] (q0) at ( 0,1) {$Q_0$}; |
1294 |
\node[state] (q1) at ( 1,1) {$Q_1$}; |
|
1295 |
\node[state, accepting] (q2) at ( 2,1) {$Q_2$}; |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1296 |
\path[->] (q0) edge[bend left] node[above] {$a$} (q1) |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1297 |
(q1) edge[bend left] node[above] {$b$} (q0) |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1298 |
(q2) edge[bend left=50] node[below] {$b$} (q0) |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1299 |
(q1) edge node[above] {$a$} (q2) |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1300 |
(q2) edge [loop right] node {$a$} () |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1301 |
(q0) edge [loop below] node {$b$} (); |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1302 |
\end{tikzpicture} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1303 |
\end{center} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1304 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1305 |
\noindent for which we can set up the following equational |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1306 |
system |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1307 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1308 |
\begin{eqnarray} |
482 | 1309 |
Q_0 & = & \ONE + Q_0\,b + Q_1\,b + Q_2\,b\\ |
1310 |
Q_1 & = & Q_0\,a\\ |
|
1311 |
Q_2 & = & Q_1\,a + Q_2\,a |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1312 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1313 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1314 |
\noindent There is an equation for each node in the DFA. Let |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1315 |
us have a look how the right-hand sides of the equations are |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1316 |
constructed. First have a look at the second equation: the |
482 | 1317 |
left-hand side is $Q_1$ and the right-hand side $Q_0\,a$. The |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1318 |
right-hand side is essentially all possible ways how to end up |
482 | 1319 |
in node $Q_1$. There is only one incoming edge from $Q_0$ consuming |
322
698ed1c96cd0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
318
diff
changeset
|
1320 |
an $a$. Therefore the right hand side is this |
482 | 1321 |
state followed by character---in this case $Q_0\,a$. Now lets |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1322 |
have a look at the third equation: there are two incoming |
482 | 1323 |
edges for $Q_2$. Therefore we have two terms, namely $Q_1\,a$ and |
1324 |
$Q_2\,a$. These terms are separated by $+$. The first states |
|
1325 |
that if in state $Q_1$ consuming an $a$ will bring you to |
|
485 | 1326 |
$Q_2$, and the second that being in $Q_2$ and consuming an $a$ |
482 | 1327 |
will make you stay in $Q_2$. The right-hand side of the |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1328 |
first equation is constructed similarly: there are three |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1329 |
incoming edges, therefore there are three terms. There is |
444
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
349
diff
changeset
|
1330 |
one exception in that we also ``add'' $\ONE$ to the |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1331 |
first equation, because it corresponds to the starting state |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1332 |
in the DFA. |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1333 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1334 |
Having constructed the equational system, the question is |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1335 |
how to solve it? Remarkably the rules are very similar to |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1336 |
solving usual linear equational systems. For example the |
482 | 1337 |
second equation does not contain the variable $Q_1$ on the |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1338 |
right-hand side of the equation. We can therefore eliminate |
482 | 1339 |
$Q_1$ from the system by just substituting this equation |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1340 |
into the other two. This gives |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1341 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1342 |
\begin{eqnarray} |
482 | 1343 |
Q_0 & = & \ONE + Q_0\,b + Q_0\,a\,b + Q_2\,b\\ |
1344 |
Q_2 & = & Q_0\,a\,a + Q_2\,a |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1345 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1346 |
|
485 | 1347 |
\noindent where in Equation (4) we have two occurrences |
482 | 1348 |
of $Q_0$. Like the laws about $+$ and $\cdot$, we can simplify |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1349 |
Equation (4) to obtain the following two equations: |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1350 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1351 |
\begin{eqnarray} |
482 | 1352 |
Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_2\,b\\ |
1353 |
Q_2 & = & Q_0\,a\,a + Q_2\,a |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1354 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1355 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1356 |
\noindent Unfortunately we cannot make any more progress with |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1357 |
substituting equations, because both (6) and (7) contain the |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1358 |
variable on the left-hand side also on the right-hand side. |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1359 |
Here we need to now use a law that is different from the usual |
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1360 |
laws about linear equations. It is called \emph{Arden's rule}. |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1361 |
It states that if an equation is of the form $q = q\,r + s$ |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1362 |
then it can be transformed to $q = s\, r^*$. Since we can |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1363 |
assume $+$ is symmetric, Equation (7) is of that form: $s$ is |
482 | 1364 |
$Q_0\,a\,a$ and $r$ is $a$. That means we can transform |
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1365 |
(7) to obtain the two new equations |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1366 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1367 |
\begin{eqnarray} |
482 | 1368 |
Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_2\,b\\ |
1369 |
Q_2 & = & Q_0\,a\,a\,(a^*) |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1370 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1371 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1372 |
\noindent Now again we can substitute the second equation into |
482 | 1373 |
the first in order to eliminate the variable $Q_2$. |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1374 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1375 |
\begin{eqnarray} |
482 | 1376 |
Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_0\,a\,a\,(a^*)\,b |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1377 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1378 |
|
482 | 1379 |
\noindent Pulling $Q_0$ out as a single factor gives: |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1380 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1381 |
\begin{eqnarray} |
482 | 1382 |
Q_0 & = & \ONE + Q_0\,(b + a\,b + a\,a\,(a^*)\,b) |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1383 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1384 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1385 |
\noindent This equation is again of the form so that we can |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1386 |
apply Arden's rule ($r$ is $b + a\,b + a\,a\,(a^*)\,b$ and $s$ |
482 | 1387 |
is $\ONE$). This gives as solution for $Q_0$ the following |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1388 |
regular expression: |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1389 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1390 |
\begin{eqnarray} |
482 | 1391 |
Q_0 & = & \ONE\,(b + a\,b + a\,a\,(a^*)\,b)^* |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1392 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1393 |
|
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1394 |
\noindent Since this is a regular expression, we can simplify |
444
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
349
diff
changeset
|
1395 |
away the $\ONE$ to obtain the slightly simpler regular |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1396 |
expression |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1397 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1398 |
\begin{eqnarray} |
482 | 1399 |
Q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^* |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1400 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1401 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1402 |
\noindent |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1403 |
Now we can unwind this process and obtain the solutions |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1404 |
for the other equations. This gives: |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1405 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1406 |
\begin{eqnarray} |
482 | 1407 |
Q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\\ |
1408 |
Q_1 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\,a\\ |
|
1409 |
Q_2 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\,a\,a\,(a)^* |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1410 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1411 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1412 |
\noindent Finally, we only need to ``add'' up the equations |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1413 |
which correspond to a terminal state. In our running example, |
482 | 1414 |
this is just $Q_2$. Consequently, a regular expression |
491 | 1415 |
that recognises the same language as the DFA is |
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1416 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1417 |
\[ |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1418 |
(b + a\,b + a\,a\,(a^*)\,b)^*\,a\,a\,(a)^* |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1419 |
\] |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1420 |
|
491 | 1421 |
\noindent You can somewhat crosscheck your solution by taking a string |
1422 |
the regular expression can match and and see whether it can be matched |
|
1423 |
by the DFA. One string for example is $aaa$ and \emph{voila} this |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1424 |
string is also matched by the automaton. |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1425 |
|
491 | 1426 |
We should prove that Brzozowski's method really produces an equivalent |
1427 |
regular expression. But for the purposes of this module, we omit |
|
1428 |
this. I guess you are relieved. |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1429 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1430 |
|
490 | 1431 |
\subsection*{Regular Languages} |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1432 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1433 |
Given the constructions in the previous sections we obtain |
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1434 |
the following overall picture: |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1435 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1436 |
\begin{center} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1437 |
\begin{tikzpicture} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1438 |
\node (rexp) {\bf Regexps}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1439 |
\node (nfa) [right=of rexp] {\bf NFAs}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1440 |
\node (dfa) [right=of nfa] {\bf DFAs}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1441 |
\node (mdfa) [right=of dfa] {\bf\begin{tabular}{c}minimal\\ DFAs\end{tabular}}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1442 |
\path[->,line width=1mm] (rexp) edge node [above=4mm, black] {\begin{tabular}{c@{\hspace{9mm}}}Thompson's\\[-1mm] construction\end{tabular}} (nfa); |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1443 |
\path[->,line width=1mm] (nfa) edge node [above=4mm, black] {\begin{tabular}{c}subset\\[-1mm] construction\end{tabular}}(dfa); |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1444 |
\path[->,line width=1mm] (dfa) edge node [below=5mm, black] {minimisation} (mdfa); |
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1445 |
\path[->,line width=1mm] (dfa) edge [bend left=45] node [below] {\begin{tabular}{l}Brzozowski's\\ method\end{tabular}} (rexp); |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1446 |
\end{tikzpicture} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1447 |
\end{center} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1448 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1449 |
\noindent By going from regular expressions over NFAs to DFAs, |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1450 |
we can always ensure that for every regular expression there |
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1451 |
exists a NFA and a DFA that can recognise the same language. |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1452 |
Although we did not prove this fact. Similarly by going from |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1453 |
DFAs to regular expressions, we can make sure for every DFA |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1454 |
there exists a regular expression that can recognise the same |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1455 |
language. Again we did not prove this fact. |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1456 |
|
491 | 1457 |
The fundamental conclusion we can draw is that automata and regular |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1458 |
expressions can recognise the same set of languages: |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1459 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1460 |
\begin{quote} A language is \emph{regular} iff there exists a |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1461 |
regular expression that recognises all its strings. |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1462 |
\end{quote} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1463 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1464 |
\noindent or equivalently |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1465 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1466 |
\begin{quote} A language is \emph{regular} iff there exists an |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1467 |
automaton that recognises all its strings. |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1468 |
\end{quote} |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
1469 |
|
491 | 1470 |
\noindent Note that this is not a stement for a particular language |
1471 |
(that is a particular set of strings), but about a large class of |
|
1472 |
languages, namely the regular ones. |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
1473 |
|
491 | 1474 |
As a consequence for deciding whether a string is recognised by a |
1475 |
regular expression, we could use our algorithm based on derivatives or |
|
1476 |
NFAs or DFAs. But let us quickly look at what the differences mean in |
|
1477 |
computational terms. Translating a regular expression into a NFA gives |
|
1478 |
us an automaton that has $O(n)$ states---that means the size of the |
|
1479 |
NFA grows linearly with the size of the regular expression. The |
|
1480 |
problem with NFAs is that the problem of deciding whether a string is |
|
1481 |
accepted or not is computationally not cheap. Remember with NFAs we |
|
1482 |
have potentially many next states even for the same input and also |
|
1483 |
have the silent $\epsilon$-transitions. If we want to find a path from |
|
1484 |
the starting state of a NFA to an accepting state, we need to consider |
|
1485 |
all possibilities. In Ruby, Python and Java this is done by a |
|
1486 |
depth-first search, which in turn means that if a ``wrong'' choice is |
|
1487 |
made, the algorithm has to backtrack and thus explore all potential |
|
1488 |
candidates. This is exactly the reason why Ruby, Python and Java are |
|
1489 |
so slow for evil regular expressions. An alternative to the |
|
1490 |
potentially slow depth-first search is to explore the search space in |
|
1491 |
a breadth-first fashion, but this might incur a big memory penalty. |
|
1492 |
||
1493 |
To avoid the problems with NFAs, we can translate them into DFAs. With |
|
1494 |
DFAs the problem of deciding whether a string is recognised or not is |
|
1495 |
much simpler, because in each state it is completely determined what |
|
1496 |
the next state will be for a given input. So no search is needed. The |
|
1497 |
problem with this is that the translation to DFAs can explode |
|
1498 |
exponentially the number of states. Therefore when this route is |
|
1499 |
taken, we definitely need to minimise the resulting DFAs in order to |
|
1500 |
have an acceptable memory and runtime behaviour. But remember the |
|
1501 |
subset construction in the worst case explodes the number of states by |
|
1502 |
$2^n$. Effectively also the translation to DFAs can incur a big |
|
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1503 |
runtime penalty. |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1504 |
|
491 | 1505 |
But this does not mean that everything is bad with automata. Recall |
1506 |
the problem of finding a regular expressions for the language that is |
|
1507 |
\emph{not} recognised by a regular expression. In our implementation |
|
1508 |
we added explicitly such a regular expressions because they are useful |
|
1509 |
for recognising comments. But in principle we did not need to. The |
|
1510 |
argument for this is as follows: take a regular expression, translate |
|
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1511 |
it into a NFA and then a DFA that both recognise the same |
491 | 1512 |
language. Once you have the DFA it is very easy to construct the |
1513 |
automaton for the language not recognised by a DFA. If the DFA is |
|
1514 |
completed (this is important!), then you just need to exchange the |
|
1515 |
accepting and non-accepting states. You can then translate this DFA |
|
1516 |
back into a regular expression and that will be the regular expression |
|
1517 |
that can match all strings the original regular expression could |
|
1518 |
\emph{not} match. |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
1519 |
|
491 | 1520 |
It is also interesting that not all languages are regular. The most |
1521 |
well-known example of a language that is not regular consists of all |
|
1522 |
the strings of the form |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1523 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1524 |
\[a^n\,b^n\] |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1525 |
|
491 | 1526 |
\noindent meaning strings that have the same number of $a$s and |
1527 |
$b$s. You can try, but you cannot find a regular expression for this |
|
1528 |
language and also not an automaton. One can actually prove that there |
|
1529 |
is no regular expression nor automaton for this language, but again |
|
1530 |
that would lead us too far afield for what we want to do in this |
|
1531 |
module. |
|
270
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
1532 |
|
492 | 1533 |
|
1534 |
\subsection*{Where Have Derivatives Gone?} |
|
1535 |
||
495 | 1536 |
By now you are probably fed up with this text. It is now way too long |
492 | 1537 |
for one lecture, but there is still one aspect of the |
495 | 1538 |
automata-regular-expression-connection I like to describe. Perhaps by |
1539 |
now you are asking yourself: Where have the derivatives gone? Did we |
|
1540 |
just forget them? Well, they have a place in the picture of |
|
1541 |
calculating a DFA from the regular expression. |
|
492 | 1542 |
|
1543 |
To be done |
|
1544 |
||
495 | 1545 |
\begin{center} |
1546 |
\begin{tikzpicture} |
|
1547 |
[level distance=25mm,very thick,auto, |
|
1548 |
level 1/.style={sibling distance=30mm}, |
|
1549 |
level 2/.style={sibling distance=15mm}, |
|
1550 |
every node/.style={minimum size=30pt, |
|
1551 |
inner sep=0pt,circle,draw=blue!50,very thick, |
|
1552 |
fill=blue!20}] |
|
1553 |
\node {$r$} [grow=right] |
|
1554 |
child[->] {node (cn) {$d_{c_n}$} |
|
1555 |
child { node {$dd_{c_nc_n}$}} |
|
1556 |
child { node {$dd_{c_nc_1}$}} |
|
1557 |
%edge from parent node[left] {$c_n$} |
|
1558 |
} |
|
1559 |
child[->] {node (c1) {$d_{c_1}$} |
|
1560 |
child { node {$dd_{c_1c_n}$}} |
|
1561 |
child { node {$dd_{c_1c_1}$}} |
|
1562 |
%edge from parent node[left] {$c_1$} |
|
1563 |
}; |
|
1564 |
%%\draw (cn) -- (c1) node {\vdots}; |
|
1565 |
\end{tikzpicture} |
|
1566 |
\end{center} |
|
492 | 1567 |
|
490 | 1568 |
%\section*{Further Reading} |
270
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
1569 |
|
490 | 1570 |
%Compare what a ``human expert'' would create as an automaton for the |
1571 |
%regular expression $a\cdot (b + c)^*$ and what the Thomson |
|
1572 |
%algorithm generates. |
|
325
794c599cee53
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
324
diff
changeset
|
1573 |
|
794c599cee53
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
324
diff
changeset
|
1574 |
%http://www.inf.ed.ac.uk/teaching/courses/ct/ |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1575 |
\end{document} |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1576 |
|
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1577 |
%%% Local Variables: |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1578 |
%%% mode: latex |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1579 |
%%% TeX-master: t |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1580 |
%%% End: |
482 | 1581 |
|
1582 |