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