| author | Christian Urban <urbanc@in.tum.de> | 
| Tue, 12 Feb 2019 21:23:00 +0000 | |
| changeset 618 | 1c7cca56fadf | 
| parent 578 | e3cb8adb2a92 | 
| child 662 | 7f7098f0b5f0 | 
| permissions | -rw-r--r-- | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1 | \documentclass{article}
 | 
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 2 | \usepackage{../style}
 | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 3 | \usepackage{../langs}
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 4 | \usepackage{../graphics}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 5 | |
| 480 | 6 | |
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 7 | \begin{document}
 | 
| 480 | 8 | \fnote{\copyright{} Christian Urban, King's College London, 2014, 2015, 2016, 2017}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 9 | |
| 482 | 10 | \section*{Handout 3 (Finite Automata)}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 11 | |
| 488 | 12 | |
| 480 | 13 | Every formal language and compiler course I know of bombards you first | 
| 14 | with automata and then to a much, much smaller extend with regular | |
| 15 | expressions. As you can see, this course is turned upside down: | |
| 16 | regular expressions come first. The reason is that regular expressions | |
| 17 | are easier to reason about and the notion of derivatives, although | |
| 18 | already quite old, only became more widely known rather | |
| 488 | 19 | recently. Still, let us in this lecture have a closer look at automata | 
| 480 | 20 | and their relation to regular expressions. This will help us with | 
| 21 | understanding why the regular expression matchers in Python, Ruby and | |
| 485 | 22 | Java are so slow with certain regular expressions. On the way we will | 
| 488 | 23 | also see what are the limitations of regular | 
| 24 | expressions. Unfortunately, they cannot be used for \emph{everything}.
 | |
| 482 | 25 | |
| 26 | ||
| 27 | \subsection*{Deterministic Finite Automata}
 | |
| 28 | ||
| 485 | 29 | Lets start\ldots the central definition is:\medskip | 
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 30 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 31 | \noindent | 
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 32 | A \emph{deterministic finite automaton} (DFA), say $A$, is
 | 
| 484 | 33 | given by a five-tuple written ${\cal A}(\varSigma, Qs, Q_0, F, \delta)$ where
 | 
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 34 | |
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 35 | \begin{itemize}
 | 
| 484 | 36 | \item $\varSigma$ is an alphabet, | 
| 482 | 37 | \item $Qs$ is a finite set of states, | 
| 38 | \item $Q_0 \in Qs$ is the start state, | |
| 39 | \item $F \subseteq Qs$ are the accepting states, and | |
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 40 | \item $\delta$ is the transition function. | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 41 | \end{itemize}
 | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 42 | |
| 490 | 43 | \noindent I am sure you have seen this definition already | 
| 488 | 44 | before. The transition function determines how to ``transition'' from | 
| 45 | one state to the next state with respect to a character. We have the | |
| 46 | assumption that these transition functions do not need to be defined | |
| 47 | everywhere: so it can be the case that given a character there is no | |
| 48 | next state, in which case we need to raise a kind of ``failure | |
| 483 | 49 | exception''.  That means actually we have \emph{partial} functions as
 | 
| 495 | 50 | transitions---see the Scala implementation for DFAs later on. A | 
| 484 | 51 | typical example of a DFA is | 
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 52 | |
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 53 | \begin{center}
 | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 54 | \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 55 |                     every state/.style={minimum size=0pt,
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 56 | inner sep=2pt,draw=blue!50,very thick, | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 57 | fill=blue!20},scale=2] | 
| 482 | 58 | \node[state,initial]  (Q_0)  {$Q_0$};
 | 
| 59 | \node[state] (Q_1) [right=of Q_0] {$Q_1$};
 | |
| 60 | \node[state] (Q_2) [below right=of Q_0] {$Q_2$};
 | |
| 61 | \node[state] (Q_3) [right=of Q_2] {$Q_3$};
 | |
| 62 | \node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$};
 | |
| 63 | \path[->] (Q_0) edge node [above]  {$a$} (Q_1);
 | |
| 64 | \path[->] (Q_1) edge node [above]  {$a$} (Q_4);
 | |
| 65 | \path[->] (Q_4) edge [loop right] node  {$a, b$} ();
 | |
| 66 | \path[->] (Q_3) edge node [right]  {$a$} (Q_4);
 | |
| 67 | \path[->] (Q_2) edge node [above]  {$a$} (Q_3);
 | |
| 68 | \path[->] (Q_1) edge node [right]  {$b$} (Q_2);
 | |
| 69 | \path[->] (Q_0) edge node [above]  {$b$} (Q_2);
 | |
| 70 | \path[->] (Q_2) edge [loop left] node  {$b$} ();
 | |
| 71 | \path[->] (Q_3) edge [bend left=95, looseness=1.3] node [below]  {$b$} (Q_0);
 | |
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 72 | \end{tikzpicture}
 | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 73 | \end{center}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 74 | |
| 482 | 75 | \noindent In this graphical notation, the accepting state $Q_4$ is | 
| 76 | indicated with double circles. Note that there can be more than one | |
| 77 | accepting state. It is also possible that a DFA has no accepting | |
| 485 | 78 | state at all, or that the starting state is also an accepting | 
| 482 | 79 | state. In the case above the transition function is defined everywhere | 
| 80 | and can also be given as a table as follows: | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 81 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 82 | \[ | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 83 | \begin{array}{lcl}
 | 
| 482 | 84 | (Q_0, a) &\rightarrow& Q_1\\ | 
| 85 | (Q_0, b) &\rightarrow& Q_2\\ | |
| 86 | (Q_1, a) &\rightarrow& Q_4\\ | |
| 87 | (Q_1, b) &\rightarrow& Q_2\\ | |
| 88 | (Q_2, a) &\rightarrow& Q_3\\ | |
| 89 | (Q_2, b) &\rightarrow& Q_2\\ | |
| 90 | (Q_3, a) &\rightarrow& Q_4\\ | |
| 91 | (Q_3, b) &\rightarrow& Q_0\\ | |
| 92 | (Q_4, a) &\rightarrow& Q_4\\ | |
| 93 | (Q_4, b) &\rightarrow& Q_4\\ | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 94 | \end{array}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 95 | \] | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 96 | |
| 495 | 97 | \noindent | 
| 98 | Please check that this table represents the same transition function | |
| 99 | as the graph above. | |
| 100 | ||
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 101 | We need to define the notion of what language is accepted by | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 102 | an automaton. For this we lift the transition function | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 103 | $\delta$ from characters to strings as follows: | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 104 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 105 | \[ | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 106 | \begin{array}{lcl}
 | 
| 484 | 107 | \widehat{\delta}(q, [])        & \dn & q\\
 | 
| 108 | \widehat{\delta}(q, c\!::\!s) & \dn & \widehat{\delta}(\delta(q, c), s)\\
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 109 | \end{array}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 110 | \] | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 111 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 112 | \noindent This lifted transition function is often called | 
| 485 | 113 | \emph{delta-hat}. Given a string, we start in the starting state and
 | 
| 114 | take the first character of the string, follow to the next state, then | |
| 115 | take the second character and so on. Once the string is exhausted and | |
| 116 | we end up in an accepting state, then this string is accepted by the | |
| 480 | 117 | automaton. Otherwise it is not accepted. This also means that if along | 
| 118 | the way we hit the case where the transition function $\delta$ is not | |
| 119 | defined, we need to raise an error. In our implementation we will deal | |
| 485 | 120 | with this case elegantly by using Scala's \texttt{Try}.  Summing up: a
 | 
| 121 | string $s$ is in the \emph{language accepted by the automaton} ${\cal
 | |
| 484 | 122 | A}(\varSigma, Q, Q_0, F, \delta)$ iff | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 123 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 124 | \[ | 
| 484 | 125 | \widehat{\delta}(Q_0, s) \in F 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 126 | \] | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 127 | |
| 488 | 128 | \noindent I let you think about a definition that describes the set of | 
| 490 | 129 | all strings accepted by a deterministic finite automaton. | 
| 480 | 130 | |
| 131 | \begin{figure}[p]
 | |
| 132 | \small | |
| 490 | 133 | \lstinputlisting[numbers=left]{../progs/display/dfa.scala}
 | 
| 572 | 134 | \caption{An implementation of DFAs in Scala using partial functions.
 | 
| 488 | 135 |   Note some subtleties: \texttt{deltas} implements the delta-hat
 | 
| 487 | 136 | construction by lifting the (partial) transition function to lists | 
| 485 | 137 |   of characters. Since \texttt{delta} is given as a partial function,
 | 
| 138 |   it can obviously go ``wrong'' in which case the \texttt{Try} in
 | |
| 139 |   \texttt{accepts} catches the error and returns \texttt{false}---that
 | |
| 140 |   means the string is not accepted.  The example \texttt{delta} in
 | |
| 141 | Line 28--38 implements the DFA example shown earlier in the | |
| 142 |   handout.\label{dfa}}
 | |
| 480 | 143 | \end{figure}
 | 
| 144 | ||
| 485 | 145 | My take on an implementation of DFAs in Scala is given in | 
| 483 | 146 | Figure~\ref{dfa}. As you can see, there are many features of the
 | 
| 482 | 147 | mathematical definition that are quite closely reflected in the | 
| 148 | code. In the DFA-class, there is a starting state, called | |
| 483 | 149 | \texttt{start}, with the polymorphic type \texttt{A}.  There is a
 | 
| 150 | partial function \texttt{delta} for specifying the transitions---these
 | |
| 151 | partial functions take a state (of polymorphic type \texttt{A}) and an
 | |
| 152 | input (of polymorphic type \texttt{C}) and produce a new state (of
 | |
| 153 | type \texttt{A}). For the moment it is OK to assume that \texttt{A} is
 | |
| 154 | some arbitrary type for states and the input is just characters. (The | |
| 485 | 155 | reason for not having concrete types, but polymorphic types for the | 
| 156 | states and the input of DFAs will become clearer later on.) | |
| 483 | 157 | |
| 485 | 158 | The DFA-class has also an argument for specifying final states. In the | 
| 159 | implementation it is not a set of states, as in the mathematical | |
| 160 | definition, but a function from states to booleans (this function is | |
| 161 | supposed to return true whenever a state is final; false | |
| 162 | otherwise). While this boolean function is different from the sets of | |
| 572 | 163 | states, Scala allows us to use sets for such functions (see Line 40 where | 
| 485 | 164 | the DFA is initialised). Again it will become clear later on why I use | 
| 165 | functions for final states, rather than sets. | |
| 166 | ||
| 167 | The most important point in the implementation is that I use Scala's | |
| 484 | 168 | partial functions for representing the transitions; alternatives would | 
| 169 | have been \texttt{Maps} or even \texttt{Lists}. One of the main
 | |
| 170 | advantages of using partial functions is that transitions can be quite | |
| 171 | nicely defined by a series of \texttt{case} statements (see Lines 28
 | |
| 172 | -- 38 for an example). If you need to represent an automaton with a | |
| 173 | sink state (catch-all-state), you can use Scala's pattern matching and | |
| 174 | write something like | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 175 | |
| 490 | 176 | {\small\begin{lstlisting}[language=Scala]
 | 
| 480 | 177 | abstract class State | 
| 178 | ... | |
| 179 | case object Sink extends State | |
| 180 | ||
| 181 | val delta : (State, Char) :=> State = | |
| 182 |   { case (S0, 'a') => S1
 | |
| 183 | case (S1, 'a') => S2 | |
| 184 | case _ => Sink | |
| 185 | } | |
| 186 | \end{lstlisting}}  
 | |
| 187 | ||
| 485 | 188 | \noindent I let you think what the corresponding DFA looks like in the | 
| 497 | 189 | graph notation. Also, I suggest you to tinker with the Scala code in | 
| 190 | order to define the DFA that does not accept any string at all. | |
| 484 | 191 | |
| 485 | 192 | Finally, I let you ponder whether this is a good implementation of | 
| 193 | DFAs or not. In doing so I hope you notice that the $\varSigma$ and | |
| 572 | 194 | $Qs$ components (the alphabet and the set of \emph{finite} states,
 | 
| 485 | 195 | respectively) are missing from the class definition. This means that | 
| 572 | 196 | the implementation allows you to do some ``fishy'' things you are not | 
| 485 | 197 | meant to do with DFAs. Which fishy things could that be? | 
| 480 | 198 | |
| 482 | 199 | |
| 200 | ||
| 201 | \subsection*{Non-Deterministic Finite Automata}
 | |
| 202 | ||
| 485 | 203 | Remember we want to find out what the relation is between regular | 
| 204 | expressions and automata. To do this with DFAs is a bit unwieldy. | |
| 205 | While with DFAs it is always clear that given a state and a character | |
| 206 | what the next state is (potentially none), it will be convenient to | |
| 207 | relax this restriction. That means we allow states to have several | |
| 482 | 208 | potential successor states. We even allow more than one starting | 
| 484 | 209 | state. The resulting construction is called a \emph{Non-Deterministic
 | 
| 485 | 210 |   Finite Automaton} (NFA) given also as a five-tuple ${\cal
 | 
| 211 |   A}(\varSigma, Qs, Q_{0s}, F, \rho)$ where
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 212 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 213 | \begin{itemize}
 | 
| 484 | 214 | \item $\varSigma$ is an alphabet, | 
| 482 | 215 | \item $Qs$ is a finite set of states | 
| 216 | \item $Q_{0s}$ is a set of start states ($Q_{0s} \subseteq Qs$)
 | |
| 217 | \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: 
142diff
changeset | 218 | \item $\rho$ is a transition relation. | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 219 | \end{itemize}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 220 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 221 | \noindent | 
| 483 | 222 | A typical example of a NFA is | 
| 482 | 223 | |
| 224 | % A NFA for (ab* + b)*a | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 225 | \begin{center}
 | 
| 483 | 226 | \begin{tikzpicture}[>=stealth',very thick, auto,
 | 
| 227 |     every state/.style={minimum size=0pt,inner sep=3pt,
 | |
| 228 | draw=blue!50,very thick,fill=blue!20},scale=2] | |
| 482 | 229 | \node[state,initial]  (Q_0)  {$Q_0$};
 | 
| 230 | \node[state] (Q_1) [right=of Q_0] {$Q_1$};
 | |
| 231 | \node[state, accepting] (Q_2) [right=of Q_1] {$Q_2$};
 | |
| 232 | \path[->] (Q_0) edge [loop above] node  {$b$} ();
 | |
| 233 | \path[<-] (Q_0) edge node [below]  {$b$} (Q_1);
 | |
| 234 | \path[->] (Q_0) edge [bend left] node [above]  {$a$} (Q_1);
 | |
| 235 | \path[->] (Q_0) edge [bend right] node [below]  {$a$} (Q_2);
 | |
| 236 | \path[->] (Q_1) edge [loop above] node  {$a,b$} ();
 | |
| 237 | \path[->] (Q_1) edge node  [above] {$a$} (Q_2);
 | |
| 238 | \end{tikzpicture}
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 239 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 240 | |
| 482 | 241 | \noindent | 
| 242 | This NFA happens to have only one starting state, but in general there | |
| 495 | 243 | could be more than one. Notice that in state $Q_0$ we might go to | 
| 244 | state $Q_1$ \emph{or} to state $Q_2$ when receiving an $a$. Similarly
 | |
| 245 | in state $Q_1$ and receiving an $a$, we can stay in state $Q_1$ | |
| 246 | \emph{or} go to $Q_2$. This kind of choice is not allowed with
 | |
| 247 | DFAs. The downside of this choice in NFAs is that when it comes to | |
| 248 | deciding whether a string is accepted by a NFA we potentially have to | |
| 249 | explore all possibilities. I let you think which strings the above NFA | |
| 250 | accepts. | |
| 482 | 251 | |
| 252 | ||
| 485 | 253 | There are a number of additional points you should note about | 
| 483 | 254 | NFAs. Every DFA is a NFA, but not vice versa. The $\rho$ in NFAs is a | 
| 255 | transition \emph{relation} (DFAs have a transition function). The
 | |
| 256 | difference between a function and a relation is that a function has | |
| 257 | always a single output, while a relation gives, roughly speaking, | |
| 258 | several outputs. Look again at the NFA above: if you are currently in | |
| 259 | the state $Q_1$ and you read a character $b$, then you can transition | |
| 260 | to either $Q_0$ \emph{or} $Q_2$. Which route, or output, you take is
 | |
| 261 | not determined. This non-determinism can be represented by a | |
| 262 | relation. | |
| 482 | 263 | |
| 483 | 264 | My implementation of NFAs in Scala is shown in Figure~\ref{nfa}.
 | 
| 265 | Perhaps interestingly, I do not actually use relations for my NFAs, | |
| 485 | 266 | but use transition functions that return sets of states. DFAs have | 
| 267 | partial transition functions that return a single state; my NFAs | |
| 488 | 268 | return a set of states. I let you think about this representation for | 
| 485 | 269 | NFA-transitions and how it corresponds to the relations used in the | 
| 487 | 270 | mathematical definition of NFAs. An example of a transition function | 
| 488 | 271 | in Scala for the NFA shown above is | 
| 482 | 272 | |
| 490 | 273 | {\small\begin{lstlisting}[language=Scala]
 | 
| 487 | 274 | val nfa_delta : (State, Char) :=> Set[State] = | 
| 275 |   { case (Q0, 'a') => Set(Q1, Q2)
 | |
| 276 | case (Q0, 'b') => Set(Q0) | |
| 277 | case (Q1, 'a') => Set(Q1, Q2) | |
| 278 | case (Q1, 'b') => Set(Q0, Q1) } | |
| 279 | \end{lstlisting}}  
 | |
| 280 | ||
| 490 | 281 | Like in the mathematical definition, \texttt{starts} is in
 | 
| 487 | 282 | NFAs a set of states; \texttt{fins} is again a function from states to
 | 
| 485 | 283 | booleans. The \texttt{next} function calculates the set of next states
 | 
| 284 | reachable from a single state \texttt{q} by a character~\texttt{c}. In
 | |
| 285 | case there is no such state---the partial transition function is | |
| 286 | undefined---the empty set is returned (see function | |
| 287 | \texttt{applyOrElse} in Lines 9 and 10). The function \texttt{nexts}
 | |
| 288 | just lifts this function to sets of states. | |
| 289 | ||
| 484 | 290 | \begin{figure}[p]
 | 
| 482 | 291 | \small | 
| 490 | 292 | \lstinputlisting[numbers=left]{../progs/display/nfa.scala}
 | 
| 485 | 293 | \caption{A Scala implementation of NFAs using partial functions.
 | 
| 294 |   Notice that the function \texttt{accepts} implements the
 | |
| 556 | 295 | acceptance of a string in a breadth-first search fashion. This can be a costly | 
| 485 | 296 | way of deciding whether a string is accepted or not in applications that need to handle | 
| 297 |   large NFAs and large inputs.\label{nfa}}
 | |
| 482 | 298 | \end{figure}
 | 
| 299 | ||
| 485 | 300 | Look very careful at the \texttt{accepts} and \texttt{deltas}
 | 
| 301 | functions in NFAs and remember that when accepting a string by a NFA | |
| 484 | 302 | we might have to explore all possible transitions (recall which state | 
| 485 | 303 | to go to is not unique anymore with NFAs\ldots{}we need to explore
 | 
| 304 | potentially all next states). The implementation achieves this | |
| 487 | 305 | exploration through a \emph{breadth-first search}. This is fine for
 | 
| 485 | 306 | small NFAs, but can lead to real memory problems when the NFAs are | 
| 307 | bigger and larger strings need to be processed. As result, some | |
| 308 | regular expression matching engines resort to a \emph{depth-first
 | |
| 309 |   search} with \emph{backtracking} in unsuccessful cases. In our
 | |
| 310 | implementation we can implement a depth-first version of | |
| 311 | \texttt{accepts} using Scala's \texttt{exists}-function as follows:
 | |
| 483 | 312 | |
| 313 | ||
| 490 | 314 | {\small\begin{lstlisting}[language=Scala]
 | 
| 483 | 315 | def search(q: A, s: List[C]) : Boolean = s match {
 | 
| 316 | case Nil => fins(q) | |
| 485 | 317 | case c::cs => next(q, c).exists(search(_, cs)) | 
| 483 | 318 | } | 
| 319 | ||
| 485 | 320 | def accepts2(s: List[C]) : Boolean = | 
| 483 | 321 | starts.exists(search(_, s)) | 
| 322 | \end{lstlisting}}
 | |
| 323 | ||
| 324 | \noindent | |
| 487 | 325 | This depth-first way of exploration seems to work quite efficiently in | 
| 326 | many examples and is much less of a strain on memory. The problem is | |
| 327 | that the backtracking can get ``catastrophic'' in some | |
| 328 | examples---remember the catastrophic backtracking from earlier | |
| 329 | lectures. This depth-first search with backtracking is the reason for | |
| 330 | the abysmal performance of some regular expression matchings in Java, | |
| 331 | Ruby and Python. I like to show you this in the next two sections. | |
| 482 | 332 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 333 | |
| 490 | 334 | \subsection*{Epsilon NFAs}
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 335 | |
| 485 | 336 | In order to get an idea what calculations are performed by Java \& | 
| 337 | friends, we need a method for transforming a regular expression into | |
| 338 | an automaton. This automaton should accept exactly those strings that | |
| 339 | are accepted by the regular expression. The simplest and most | |
| 340 | well-known method for this is called \emph{Thompson Construction},
 | |
| 341 | after the Turing Award winner Ken Thompson. This method is by | |
| 487 | 342 | recursion over regular expressions and depends on the non-determinism | 
| 488 | 343 | in NFAs described in the previous section. You will see shortly why | 
| 487 | 344 | this construction works well with NFAs, but is not so straightforward | 
| 345 | with DFAs. | |
| 346 | ||
| 347 | Unfortunately we are still one step away from our intended target | |
| 348 | though---because this construction uses a version of NFAs that allows | |
| 349 | ``silent transitions''. The idea behind silent transitions is that | |
| 350 | they allow us to go from one state to the next without having to | |
| 351 | consume a character. We label such silent transition with the letter | |
| 352 | $\epsilon$ and call the automata $\epsilon$NFAs. Two typical examples | |
| 353 | of $\epsilon$NFAs are: | |
| 484 | 354 | |
| 355 | ||
| 485 | 356 | \begin{center}
 | 
| 357 | \begin{tabular}[t]{c@{\hspace{9mm}}c}
 | |
| 358 | \begin{tikzpicture}[>=stealth',very thick,
 | |
| 359 |     every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 360 | \node[state,initial]  (Q_0)  {$Q_0$};
 | |
| 361 | \node[state] (Q_1) [above=of Q_0] {$Q_1$};
 | |
| 362 | \node[state, accepting] (Q_2) [below=of Q_0] {$Q_2$};
 | |
| 363 | \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_1);
 | |
| 364 | \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_2);
 | |
| 365 | \path[->] (Q_0) edge [loop right] node  {$a$} ();
 | |
| 366 | \path[->] (Q_1) edge [loop right] node  {$a$} ();
 | |
| 367 | \path[->] (Q_2) edge [loop right] node  {$b$} ();
 | |
| 368 | \end{tikzpicture} &
 | |
| 369 | ||
| 370 | \raisebox{20mm}{
 | |
| 371 | \begin{tikzpicture}[>=stealth',very thick,
 | |
| 372 |     every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 373 | \node[state,initial]  (r_1)  {$R_1$};
 | |
| 374 | \node[state] (r_2) [above=of r_1] {$R_2$};
 | |
| 375 | \node[state, accepting] (r_3) [right=of r_1] {$R_3$};
 | |
| 376 | \path[->] (r_1) edge node [below]  {$b$} (r_3);
 | |
| 377 | \path[->] (r_2) edge [bend left] node [above]  {$a$} (r_3);
 | |
| 378 | \path[->] (r_1) edge [bend left] node  [left] {$\epsilon$} (r_2);
 | |
| 379 | \path[->] (r_2) edge [bend left] node  [right] {$a$} (r_1);
 | |
| 380 | \end{tikzpicture}}
 | |
| 381 | \end{tabular}
 | |
| 382 | \end{center}
 | |
| 383 | ||
| 384 | \noindent | |
| 487 | 385 | Consider the $\epsilon$NFA on the left-hand side: the | 
| 386 | $\epsilon$-transitions mean you do not have to ``consume'' any part of | |
| 387 | the input string, but ``silently'' change to a different state. In | |
| 388 | this example, if you are in the starting state $Q_0$, you can silently | |
| 389 | move either to $Q_1$ or $Q_2$. You can see that once you are in $Q_1$, | |
| 390 | respectively $Q_2$, you cannot ``go back'' to the other states. So it | |
| 490 | 391 | seems allowing $\epsilon$-transitions is a rather substantial | 
| 487 | 392 | extension to NFAs. On first appearances, $\epsilon$-transitions might | 
| 393 | even look rather strange, or even silly. To start with, silent | |
| 394 | transitions make the decision whether a string is accepted by an | |
| 395 | automaton even harder: with $\epsilon$NFAs we have to look whether we | |
| 396 | can do first some $\epsilon$-transitions and then do a | |
| 397 | ``proper''-transition; and after any ``proper''-transition we again | |
| 398 | have to check whether we can do again some silent transitions. Even | |
| 399 | worse, if there is a silent transition pointing back to the same | |
| 400 | state, then we have to be careful our decision procedure for strings | |
| 401 | does not loop (remember the depth-first search for exploring all | |
| 402 | states). | |
| 485 | 403 | |
| 404 | The obvious question is: Do we get anything in return for this hassle | |
| 405 | with silent transitions? Well, we still have to work for it\ldots | |
| 406 | unfortunately. If we were to follow the many textbooks on the | |
| 407 | subject, we would now start with defining what $\epsilon$NFAs | |
| 408 | are---that would require extending the transition relation of | |
| 490 | 409 | NFAs. Next, we would show that the $\epsilon$NFAs are equivalent to | 
| 488 | 410 | NFAs and so on. Once we have done all this on paper, we would need to | 
| 411 | implement $\epsilon$NFAs. Lets try to take a shortcut instead. We are | |
| 412 | not really interested in $\epsilon$NFAs; they are only a convenient | |
| 413 | tool for translating regular expressions into automata. So we are not | |
| 414 | going to implementing them explicitly, but translate them immediately | |
| 415 | into NFAs (in a sense $\epsilon$NFAs are just a convenient API for | |
| 416 | lazy people ;o). How does the translation work? Well we have to find | |
| 417 | all transitions of the form | |
| 485 | 418 | |
| 419 | \[ | |
| 420 | q\stackrel{\epsilon}{\longrightarrow}\ldots\stackrel{\epsilon}{\longrightarrow}
 | |
| 421 | \;\stackrel{a}{\longrightarrow}\;
 | |
| 422 | \stackrel{\epsilon}{\longrightarrow}\ldots\stackrel{\epsilon}{\longrightarrow} q'
 | |
| 423 | \] | |
| 424 | ||
| 492 | 425 | \noindent where somewhere in the ``middle'' is an $a$-transition. We | 
| 426 | replace them with $q \stackrel{a}{\longrightarrow} q'$. Doing this to
 | |
| 427 | the $\epsilon$NFA on the right-hand side above gives the NFA | |
| 485 | 428 | |
| 429 | \begin{center}
 | |
| 430 | \begin{tikzpicture}[>=stealth',very thick,
 | |
| 431 |     every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 432 | \node[state,initial]  (r_1)  {$R_1$};
 | |
| 433 | \node[state] (r_2) [above=of r_1] {$R_2$};
 | |
| 434 | \node[state, accepting] (r_3) [right=of r_1] {$R_3$};
 | |
| 435 | \path[->] (r_1) edge node [above]  {$b$} (r_3);
 | |
| 436 | \path[->] (r_2) edge [bend left] node [above]  {$a$} (r_3);
 | |
| 437 | \path[->] (r_1) edge [bend left] node  [left] {$a$} (r_2);
 | |
| 438 | \path[->] (r_2) edge [bend left] node  [right] {$a$} (r_1);
 | |
| 439 | \path[->] (r_1) edge [loop below] node  {$a$} ();
 | |
| 440 | \path[->] (r_1) edge [bend right] node [below]  {$a$} (r_3);
 | |
| 441 | \end{tikzpicture}
 | |
| 442 | \end{center}
 | |
| 443 | ||
| 487 | 444 | \noindent where the single $\epsilon$-transition is replaced by | 
| 445 | three additional $a$-transitions. Please do the calculations yourself | |
| 446 | and verify that I did not forget any transition. | |
| 447 | ||
| 448 | So in what follows, whenever we are given an $\epsilon$NFA we will | |
| 488 | 449 | replace it by an equivalent NFA. The Scala code for this translation | 
| 450 | is given in Figure~\ref{enfa}. The main workhorse in this code is a
 | |
| 451 | function that calculates a fixpoint of function (Lines 5--10). This | |
| 452 | function is used for ``discovering'' which states are reachable by | |
| 487 | 453 | $\epsilon$-transitions. Once no new state is discovered, a fixpoint is | 
| 454 | reached. This is used for example when calculating the starting states | |
| 455 | of an equivalent NFA (see Line 36): we start with all starting states | |
| 456 | of the $\epsilon$NFA and then look for all additional states that can | |
| 457 | be reached by $\epsilon$-transitions. We keep on doing this until no | |
| 458 | new state can be reached. This is what the $\epsilon$-closure, named | |
| 459 | in the code \texttt{ecl}, calculates. Similarly, an accepting state of
 | |
| 460 | the NFA is when we can reach an accepting state of the $\epsilon$NFA | |
| 461 | by $\epsilon$-transitions. | |
| 462 | ||
| 485 | 463 | |
| 464 | \begin{figure}[p]
 | |
| 465 | \small | |
| 490 | 466 | \lstinputlisting[numbers=left]{../progs/display/enfa.scala}
 | 
| 485 | 467 | |
| 468 | \caption{A Scala function that translates $\epsilon$NFA into NFAs. The
 | |
| 490 | 469 |   transition function of $\epsilon$NFA takes as input an \texttt{Option[C]}.
 | 
| 485 | 470 |   \texttt{None} stands for an $\epsilon$-transition; \texttt{Some(c)}
 | 
| 488 | 471 | for a ``proper'' transition consuming a character. The functions in | 
| 472 | Lines 18--26 calculate | |
| 485 | 473 | all states reachable by one or more $\epsilon$-transition for a given | 
| 491 | 474 | set of states. The NFA is constructed in Lines 36--38. | 
| 475 | Note the interesting commands in Lines 5 and 6: their purpose is | |
| 476 |   to ensure that \texttt{fixpT} is the tail-recursive version of
 | |
| 477 | the fixpoint construction; otherwise we would quickly get a | |
| 478 | stack-overflow exception, even on small examples, due to limitations | |
| 479 | of the JVM. | |
| 480 |   \label{enfa}}
 | |
| 485 | 481 | \end{figure}
 | 
| 482 | ||
| 487 | 483 | Also look carefully how the transitions of $\epsilon$NFAs are | 
| 484 | implemented. The additional possibility of performing silent | |
| 485 | transitions is encoded by using \texttt{Option[C]} as the type for the
 | |
| 490 | 486 | ``input''. The \texttt{Some}s stand for ``proper'' transitions where
 | 
| 487 | 487 | a character is consumed; \texttt{None} stands for
 | 
| 488 | $\epsilon$-transitions. The transition functions for the two | |
| 489 | $\epsilon$NFAs from the beginning of this section can be defined as | |
| 485 | 490 | |
| 490 | 491 | {\small\begin{lstlisting}[language=Scala]
 | 
| 487 | 492 | val enfa_trans1 : (State, Option[Char]) :=> Set[State] = | 
| 493 |   { case (Q0, Some('a')) => Set(Q0)
 | |
| 494 | case (Q0, None) => Set(Q1, Q2) | |
| 495 |     case (Q1, Some('a')) => Set(Q1)
 | |
| 496 |     case (Q2, Some('b')) => Set(Q2) }
 | |
| 497 | ||
| 498 | val enfa_trans2 : (State, Option[Char]) :=> Set[State] = | |
| 499 |   { case (R1, Some('b')) => Set(R3)
 | |
| 500 | case (R1, None) => Set(R2) | |
| 501 |     case (R2, Some('a')) => Set(R1, R3) }
 | |
| 502 | \end{lstlisting}}
 | |
| 503 | ||
| 504 | \noindent | |
| 505 | I hope you agree now with my earlier statement that the $\epsilon$NFAs | |
| 506 | are just an API for NFAs. | |
| 507 | ||
| 490 | 508 | \subsection*{Thompson Construction}
 | 
| 487 | 509 | |
| 510 | Having the translation of $\epsilon$NFAs to NFAs in place, we can | |
| 511 | finally return to the problem of translating regular expressions into | |
| 512 | equivalent NFAs. Recall that by equivalent we mean that the NFAs | |
| 485 | 513 | recognise the same language. Consider the simple regular expressions | 
| 514 | $\ZERO$, $\ONE$ and $c$. They can be translated into equivalent NFAs | |
| 515 | as follows: | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 516 | |
| 488 | 517 | \begin{equation}\mbox{
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 518 | \begin{tabular}[t]{l@{\hspace{10mm}}l}
 | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
349diff
changeset | 519 | \raisebox{1mm}{$\ZERO$} & 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 520 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 521 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 522 | \end{tikzpicture}\\\\
 | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
349diff
changeset | 523 | \raisebox{1mm}{$\ONE$} & 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 524 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 525 | \node[state, initial, accepting]  (Q_0)  {$\mbox{}$};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 526 | \end{tikzpicture}\\\\
 | 
| 487 | 527 | \raisebox{3mm}{$c$} & 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 528 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 529 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 530 | \node[state, accepting]  (Q_1)  [right=of Q_0] {$\mbox{}$};
 | |
| 531 | \path[->] (Q_0) edge node [below]  {$c$} (Q_1);
 | |
| 487 | 532 | \end{tikzpicture}\\
 | 
| 488 | 533 | \end{tabular}}\label{simplecases}
 | 
| 534 | \end{equation}
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 535 | |
| 487 | 536 | \noindent | 
| 537 | I let you think whether the NFAs can match exactly those strings the | |
| 538 | regular expressions can match. To do this translation in code we need | |
| 495 | 539 | a way to construct states ``programatically''...and as an additional | 
| 540 | constraint Scala needs to recognise that these states are being distinct. | |
| 487 | 541 | For this I implemented in Figure~\ref{thompson1} a class
 | 
| 542 | \texttt{TState} that includes a counter and a companion object that
 | |
| 488 | 543 | increases this counter whenever a new state is created.\footnote{You might
 | 
| 544 |   have to read up what \emph{companion objects} do in Scala.}
 | |
| 487 | 545 | |
| 485 | 546 | \begin{figure}[p]
 | 
| 547 | \small | |
| 490 | 548 | \lstinputlisting[numbers=left]{../progs/display/thompson1.scala}
 | 
| 487 | 549 | \caption{The first part of the Thompson Construction. Lines 7--16
 | 
| 488 | 550 | implement a way of how to create new states that are all | 
| 487 | 551 | distinct by virtue of a counter. This counter is | 
| 552 |   increased in the companion object of \texttt{TState}
 | |
| 553 | whenever a new state is created. The code in Lines 24--40 | |
| 488 | 554 | constructs NFAs for the simple regular expressions $\ZERO$, $\ONE$ and $c$. | 
| 495 | 555 |   Compare this code with the pictures given in \eqref{simplecases} on
 | 
| 556 |   Page~\pageref{simplecases}.
 | |
| 487 | 557 |   \label{thompson1}}
 | 
| 485 | 558 | \end{figure}
 | 
| 559 | ||
| 487 | 560 | \begin{figure}[p]
 | 
| 561 | \small | |
| 490 | 562 | \lstinputlisting[numbers=left]{../progs/display/thompson2.scala}
 | 
| 487 | 563 | \caption{The second part of the Thompson Construction implementing
 | 
| 490 | 564 |   the composition of NFAs according to $\cdot$, $+$ and ${}^*$.
 | 
| 487 | 565 | The implicit class about rich partial functions | 
| 566 |   implements the infix operation \texttt{+++} which
 | |
| 567 | combines an $\epsilon$NFA transition with a NFA transition | |
| 495 | 568 |   (both are given as partial functions---but with different type!).\label{thompson2}}
 | 
| 487 | 569 | \end{figure}
 | 
| 485 | 570 | |
| 488 | 571 | The case for the sequence regular expression $r_1 \cdot r_2$ is a bit more | 
| 489 | 572 | complicated: Say, we are given by recursion two NFAs representing the regular | 
| 488 | 573 | expressions $r_1$ and $r_2$ respectively. | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 574 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 575 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 576 | \begin{tikzpicture}[node distance=3mm,
 | 
| 488 | 577 | >=stealth',very thick, | 
| 578 |     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 482 | 579 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 488 | 580 | \node[state, initial]  (Q_01) [below=1mm of Q_0] {$\mbox{}$};  
 | 
| 581 | \node[state, initial]  (Q_02) [above=1mm of Q_0] {$\mbox{}$};  
 | |
| 582 | \node (R_1)  [right=of Q_0] {$\ldots$};
 | |
| 583 | \node[state, accepting]  (T_1)  [right=of R_1] {$\mbox{}$};
 | |
| 584 | \node[state, accepting]  (T_2)  [above=of T_1] {$\mbox{}$};
 | |
| 585 | \node[state, accepting]  (T_3)  [below=of T_1] {$\mbox{}$};
 | |
| 586 | ||
| 587 | \node (A_0)  [right=2.5cm of T_1] {$\mbox{}$};
 | |
| 588 | \node[state, initial]  (A_01)  [above=1mm of A_0] {$\mbox{}$};
 | |
| 589 | \node[state, initial]  (A_02)  [below=1mm of A_0] {$\mbox{}$};
 | |
| 590 | ||
| 591 | \node (b_1)  [right=of A_0] {$\ldots$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 592 | \node[state, accepting]  (c_1)  [right=of b_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 593 | \node[state, accepting]  (c_2)  [above=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 594 | \node[state, accepting]  (c_3)  [below=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 595 | \begin{pgfonlayer}{background}
 | 
| 488 | 596 | \node (1) [rounded corners, inner sep=1mm, thick, | 
| 597 |     draw=black!60, fill=black!20, fit= (Q_0) (R_1) (T_1) (T_2) (T_3)] {};
 | |
| 598 | \node (2) [rounded corners, inner sep=1mm, thick, | |
| 599 |     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: 
142diff
changeset | 600 | \node [yshift=2mm] at (1.north) {$r_1$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 601 | \node [yshift=2mm] at (2.north) {$r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 602 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 603 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 604 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 605 | |
| 488 | 606 | \noindent The first NFA has some accepting states and the second some | 
| 489 | 607 | starting states. We obtain an $\epsilon$NFA for $r_1\cdot r_2$ by | 
| 608 | connecting the accepting states of the first NFA with | |
| 609 | $\epsilon$-transitions to the starting states of the second | |
| 610 | automaton. By doing so we make the accepting states of the first NFA | |
| 611 | to be non-accepting like so: | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 612 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 613 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 614 | \begin{tikzpicture}[node distance=3mm,
 | 
| 488 | 615 | >=stealth',very thick, | 
| 616 |     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 482 | 617 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 488 | 618 | \node[state, initial]  (Q_01) [below=1mm of Q_0] {$\mbox{}$};  
 | 
| 619 | \node[state, initial]  (Q_02) [above=1mm of Q_0] {$\mbox{}$};  
 | |
| 482 | 620 | \node (r_1)  [right=of Q_0] {$\ldots$};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 621 | \node[state]  (t_1)  [right=of r_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 622 | \node[state]  (t_2)  [above=of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 623 | \node[state]  (t_3)  [below=of t_1] {$\mbox{}$};
 | 
| 488 | 624 | |
| 625 | \node  (A_0)  [right=2.5cm of t_1] {$\mbox{}$};
 | |
| 626 | \node[state]  (A_01)  [above=1mm of A_0] {$\mbox{}$};
 | |
| 627 | \node[state]  (A_02)  [below=1mm of A_0] {$\mbox{}$};
 | |
| 628 | ||
| 629 | \node (b_1)  [right=of A_0] {$\ldots$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 630 | \node[state, accepting]  (c_1)  [right=of b_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 631 | \node[state, accepting]  (c_2)  [above=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 632 | \node[state, accepting]  (c_3)  [below=of c_1] {$\mbox{}$};
 | 
| 488 | 633 | \path[->] (t_1) edge (A_01); | 
| 492 | 634 | \path[->] (t_2) edge node [above]  {$\epsilon$s} (A_01);
 | 
| 488 | 635 | \path[->] (t_3) edge (A_01); | 
| 636 | \path[->] (t_1) edge (A_02); | |
| 637 | \path[->] (t_2) edge (A_02); | |
| 492 | 638 | \path[->] (t_3) edge node [below]  {$\epsilon$s} (A_02);
 | 
| 488 | 639 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 640 | \begin{pgfonlayer}{background}
 | 
| 488 | 641 | \node (3) [rounded corners, inner sep=1mm, thick, | 
| 642 |     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: 
142diff
changeset | 643 | \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: 
142diff
changeset | 644 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 645 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 646 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 647 | |
| 489 | 648 | \noindent The idea behind this construction is that the start of any | 
| 649 | string is first recognised by the first NFA, then we silently change | |
| 650 | to the second NFA; the ending of the string is recognised by the | |
| 651 | second NFA...just like matching of a string by the regular expression | |
| 490 | 652 | $r_1\cdot r_2$. The Scala code for this construction is given in | 
| 489 | 653 | Figure~\ref{thompson2} in Lines 16--23. The starting states of the
 | 
| 654 | $\epsilon$NFA are the starting states of the first NFA (corresponding | |
| 655 | to $r_1$); the accepting function is the accepting function of the | |
| 656 | second NFA (corresponding to $r_2$). The new transition function is | |
| 657 | all the ``old'' transitions plus the $\epsilon$-transitions connecting | |
| 658 | the accepting states of the first NFA to the starting states of the | |
| 490 | 659 | first NFA (Lines 18 and 19). The $\epsilon$NFA is then immediately | 
| 489 | 660 | translated in a NFA. | 
| 661 | ||
| 662 | ||
| 490 | 663 | The case for the alternative regular expression $r_1 + r_2$ is | 
| 664 | slightly different: We are given by recursion two NFAs representing | |
| 665 | $r_1$ and $r_2$ respectively. Each NFA has some starting states and | |
| 666 | some accepting states. We obtain a NFA for the regular expression $r_1 | |
| 667 | + r_2$ by composing the transition functions (this crucially depends | |
| 495 | 668 | on knowing that the states of each component NFA are distinct---recall | 
| 669 | we implemented for this to hold some bespoke code for states). We also | |
| 670 | need to combine the starting states and accepting functions | |
| 671 | appropriately. | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 672 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 673 | \begin{center}
 | 
| 490 | 674 | \begin{tabular}[t]{ccc}
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 675 | \begin{tikzpicture}[node distance=3mm,
 | 
| 488 | 676 | >=stealth',very thick, | 
| 490 | 677 |     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
 | 
| 678 | baseline=(current bounding box.center)] | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 679 | \node at (0,0)  (1)  {$\mbox{}$};
 | 
| 489 | 680 | \node  (2)  [above=10mm of 1] {};
 | 
| 681 | \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
 | |
| 682 | \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};
 | |
| 683 | \node[state, initial]  (3)  [below=10mm of 1] {$\mbox{}$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 684 | |
| 489 | 685 | \node (a)  [right=of 2] {$\ldots\,$};
 | 
| 686 | \node  (a1)  [right=of a] {$$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 687 | \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 688 | \node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 689 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 690 | \node (b)  [right=of 3] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 691 | \node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 692 | \node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 693 | \node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 694 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 695 | \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: 
142diff
changeset | 696 | \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: 
142diff
changeset | 697 | \node [yshift=3mm] at (1.north) {$r_1$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 698 | \node [yshift=3mm] at (2.north) {$r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 699 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 700 | \end{tikzpicture}
 | 
| 490 | 701 | & | 
| 702 | \mbox{}\qquad\tikz{\draw[>=stealth,line width=2mm,->] (0,0) -- (1, 0)}\quad\mbox{}
 | |
| 703 | & | |
| 489 | 704 | \begin{tikzpicture}[node distance=3mm,
 | 
| 705 | >=stealth',very thick, | |
| 490 | 706 |     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
 | 
| 707 | baseline=(current bounding box.center)] | |
| 489 | 708 | \node at (0,0) (1)  {$\mbox{}$};
 | 
| 709 | \node (2)  [above=10mm of 1] {$$};
 | |
| 710 | \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
 | |
| 711 | \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};
 | |
| 712 | \node[state, initial]  (3)  [below=10mm of 1] {$\mbox{}$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 713 | |
| 489 | 714 | \node (a)  [right=of 2] {$\ldots\,$};
 | 
| 715 | \node (a1)  [right=of a] {$$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 716 | \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 717 | \node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 718 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 719 | \node (b)  [right=of 3] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 720 | \node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 721 | \node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 722 | \node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 723 | |
| 489 | 724 | %\path[->] (1) edge node [above]  {$\epsilon$} (2);
 | 
| 725 | %\path[->] (1) edge node [below]  {$\epsilon$} (3);
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 726 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 727 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 728 | \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: 
142diff
changeset | 729 | \node [yshift=3mm] at (3.north) {$r_1+ r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 730 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 731 | \end{tikzpicture}
 | 
| 490 | 732 | \end{tabular}
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 733 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 734 | |
| 489 | 735 | \noindent The code for this construction is in Figure~\ref{thompson2}
 | 
| 490 | 736 | in Lines 25--33. | 
| 737 | ||
| 738 | Finally for the $*$-case we have a NFA for $r$ and connect its | |
| 739 | accepting states to a new starting state via | |
| 740 | $\epsilon$-transitions. This new starting state is also an accepting | |
| 741 | state, because $r^*$ can recognise the empty string. | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 742 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 743 | \begin{center}
 | 
| 495 | 744 | \begin{tabular}[b]{@{}ccc@{}}  
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 745 | \begin{tikzpicture}[node distance=3mm,
 | 
| 490 | 746 | >=stealth',very thick, | 
| 747 |     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
 | |
| 748 | baseline=(current bounding box.north)] | |
| 495 | 749 | \node (2)  {$\mbox{}$};
 | 
| 750 | \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
 | |
| 751 | \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};  
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 752 | \node (a)  [right=of 2] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 753 | \node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 754 | \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 755 | \node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 756 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 757 | \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: 
142diff
changeset | 758 | \node [yshift=3mm] at (1.north) {$r$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 759 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 760 | \end{tikzpicture}
 | 
| 490 | 761 | & | 
| 762 | \raisebox{-16mm}{\;\tikz{\draw[>=stealth,line width=2mm,->] (0,0) -- (1, 0)}}
 | |
| 763 | & | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 764 | \begin{tikzpicture}[node distance=3mm,
 | 
| 489 | 765 | >=stealth',very thick, | 
| 490 | 766 |     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
 | 
| 767 | baseline=(current bounding box.north)] | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 768 | \node at (0,0) [state, initial,accepting]  (1)  {$\mbox{}$};
 | 
| 495 | 769 | \node (2)  [right=16mm of 1] {$\mbox{}$};
 | 
| 770 | \node[state]  (4)  [above=1mm of 2] {$\mbox{}$};
 | |
| 771 | \node[state]  (5)  [below=1mm of 2] {$\mbox{}$};  
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 772 | \node (a)  [right=of 2] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 773 | \node[state]  (a1)  [right=of a] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 774 | \node[state]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 775 | \node[state]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 495 | 776 | \path[->] (1) edge node [below]  {$\epsilon$} (4);
 | 
| 777 | \path[->] (1) edge node [below]  {$\epsilon$} (5);
 | |
| 778 | \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: 
142diff
changeset | 779 | \path[->] (a2) edge [bend right] node [below]  {$\epsilon$} (1);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 780 | \path[->] (a3) edge [bend left=45] node [below]  {$\epsilon$} (1);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 781 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 782 | \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: 
142diff
changeset | 783 | \node [yshift=3mm] at (2.north) {$r^*$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 784 | \end{pgfonlayer}
 | 
| 490 | 785 | \end{tikzpicture}    
 | 
| 786 | \end{tabular}
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 787 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 788 | |
| 490 | 789 | \noindent | 
| 790 | The corresponding code is in Figure~\ref{thompson2} in Lines 35--43)
 | |
| 489 | 791 | |
| 490 | 792 | To sum up, you can see in the sequence and star cases the need of | 
| 489 | 793 | having silent $\epsilon$-transitions. Similarly the alternative case | 
| 490 | 794 | shows the need of the NFA-nondeterminism. It seems awkward to form the | 
| 489 | 795 | `alternative' composition of two DFAs, because DFA do not allow | 
| 796 | several starting and successor states. All these constructions can now | |
| 797 | be put together in the following recursive function: | |
| 798 | ||
| 799 | ||
| 490 | 800 | {\small\begin{lstlisting}[language=Scala]
 | 
| 801 | def thompson(r: Rexp) : NFAt = r match {
 | |
| 488 | 802 | case ZERO => NFA_ZERO() | 
| 803 | case ONE => NFA_ONE() | |
| 804 | case CHAR(c) => NFA_CHAR(c) | |
| 805 | case ALT(r1, r2) => NFA_ALT(thompson(r1), thompson(r2)) | |
| 806 | case SEQ(r1, r2) => NFA_SEQ(thompson(r1), thompson(r2)) | |
| 807 | case STAR(r1) => NFA_STAR(thompson(r1)) | |
| 808 | } | |
| 809 | \end{lstlisting}}
 | |
| 810 | ||
| 489 | 811 | \noindent | 
| 490 | 812 | It calculates a NFA from a regular expressions. At last we can run | 
| 813 | NFAs for the our evil regular expression examples. The graph on the | |
| 814 | left shows that when translating a regular expression such as | |
| 815 | $a^{\{n\}}$ into a NFA, the size can blow up and then even the
 | |
| 816 | relative fast (on small examples) breadth-first search can be | |
| 817 | slow. The graph on the right shows that with `evil' regular | |
| 818 | expressions the depth-first search can be abysmally slow. Even if the | |
| 819 | graphs not completely overlap with the curves of Python, Ruby and | |
| 820 | Java, they are similar enough. OK\ldots now you know why regular | |
| 821 | expression matchers in those languages are so slow. | |
| 489 | 822 | |
| 488 | 823 | |
| 824 | \begin{center}
 | |
| 825 | \begin{tabular}{@{\hspace{-1mm}}c@{\hspace{1mm}}c@{}}  
 | |
| 826 | \begin{tikzpicture}
 | |
| 827 | \begin{axis}[
 | |
| 490 | 828 |     title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings 
 | 
| 489 | 829 |            $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
 | 
| 490 | 830 |     title style={yshift=-2ex},
 | 
| 489 | 831 |     xlabel={$n$},
 | 
| 832 |     x label style={at={(1.05,0.0)}},
 | |
| 833 |     ylabel={time in secs},
 | |
| 834 | enlargelimits=false, | |
| 835 |     xtick={0,5,...,30},
 | |
| 836 | xmax=33, | |
| 837 | ymax=35, | |
| 838 |     ytick={0,5,...,30},
 | |
| 839 | scaled ticks=false, | |
| 840 | axis lines=left, | |
| 841 | width=5.5cm, | |
| 490 | 842 | height=4cm, | 
| 843 |     legend entries={Python,Ruby, breadth-first NFA},
 | |
| 844 |     legend style={at={(0.5,-0.25)},anchor=north,font=\small},
 | |
| 489 | 845 | legend cell align=left] | 
| 846 |   \addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
 | |
| 847 |   \addplot[brown,mark=triangle*, mark options={fill=white}] table {re-ruby.data};
 | |
| 848 | % breath-first search in NFAs | |
| 849 |   \addplot[red,mark=*, mark options={fill=white}] table {
 | |
| 850 | 1 0.00586 | |
| 851 | 2 0.01209 | |
| 852 | 3 0.03076 | |
| 853 | 4 0.08269 | |
| 854 | 5 0.12881 | |
| 855 | 6 0.25146 | |
| 856 | 7 0.51377 | |
| 857 | 8 0.89079 | |
| 858 | 9 1.62802 | |
| 859 | 10 3.05326 | |
| 860 | 11 5.92437 | |
| 861 | 12 11.67863 | |
| 862 | 13 24.00568 | |
| 863 | }; | |
| 864 | \end{axis}
 | |
| 865 | \end{tikzpicture}
 | |
| 866 | & | |
| 867 | \begin{tikzpicture}
 | |
| 868 | \begin{axis}[
 | |
| 490 | 869 |     title={Graph: $(a^*)^* \cdot b$ and strings 
 | 
| 488 | 870 |            $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
 | 
| 490 | 871 |     title style={yshift=-2ex},
 | 
| 488 | 872 |     xlabel={$n$},
 | 
| 873 |     x label style={at={(1.05,0.0)}},
 | |
| 874 |     ylabel={time in secs},
 | |
| 875 | enlargelimits=false, | |
| 876 |     xtick={0,5,...,30},
 | |
| 877 | xmax=33, | |
| 878 | ymax=35, | |
| 879 |     ytick={0,5,...,30},
 | |
| 880 | scaled ticks=false, | |
| 881 | axis lines=left, | |
| 882 | width=5.5cm, | |
| 490 | 883 | height=4cm, | 
| 884 |     legend entries={Python, Java, depth-first NFA},
 | |
| 885 |     legend style={at={(0.5,-0.25)},anchor=north,font=\small},
 | |
| 488 | 886 | legend cell align=left] | 
| 887 |   \addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
 | |
| 888 |   \addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};  
 | |
| 889 | % depth-first search in NFAs | |
| 890 |   \addplot[red,mark=*, mark options={fill=white}] table {
 | |
| 891 | 1 0.00605 | |
| 892 | 2 0.03086 | |
| 893 | 3 0.11994 | |
| 894 | 4 0.45389 | |
| 895 | 5 2.06192 | |
| 896 | 6 8.04894 | |
| 897 | 7 32.63549 | |
| 898 | }; | |
| 899 | \end{axis}
 | |
| 900 | \end{tikzpicture}
 | |
| 901 | \end{tabular}
 | |
| 902 | \end{center}
 | |
| 903 | ||
| 904 | ||
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 905 | |
| 490 | 906 | \subsection*{Subset Construction}
 | 
| 907 | ||
| 491 | 908 | Of course, some developers of regular expression matchers are aware of | 
| 909 | these problems with sluggish NFAs and try to address them. One common | |
| 910 | technique for alleviating the problem I like to show you in this | |
| 911 | section. This will also explain why we insisted on polymorphic types in | |
| 912 | our DFA code (remember I used \texttt{A} and \texttt{C} for the types
 | |
| 913 | of states and the input, see Figure~\ref{dfa} on
 | |
| 914 | Page~\pageref{dfa}).\bigskip
 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 915 | |
| 490 | 916 | \noindent | 
| 491 | 917 | To start remember that we did not bother with defining and | 
| 918 | implementing $\epsilon$NFAs: we immediately translated them into | |
| 490 | 919 | equivalent NFAs. Equivalent in the sense of accepting the same | 
| 920 | language (though we only claimed this and did not prove it | |
| 921 | rigorously). Remember also that NFAs have non-deterministic | |
| 922 | transitions defined as a relation or implemented as function returning | |
| 923 | sets of states. This non-determinism is crucial for the Thompson | |
| 924 | Construction to work (recall the cases for $\cdot$, $+$ and | |
| 925 | ${}^*$). But this non-determinism makes it harder with NFAs to decide
 | |
| 491 | 926 | when a string is accepted or not; whereas such a decision is rather | 
| 490 | 927 | straightforward with DFAs: recall their transition function is a | 
| 491 | 928 | \emph{function} that returns a single state. So with DFAs we do not
 | 
| 929 | have to search at all. What is perhaps interesting is the fact that | |
| 930 | for every NFA we can find a DFA that also recognises the same | |
| 931 | language. This might sound a bit paradoxical: NFA $\rightarrow$ | |
| 932 | decision of acceptance hard; DFA $\rightarrow$ decision easy. But this | |
| 933 | \emph{is} true\ldots but of course there is always a caveat---nothing
 | |
| 934 | 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: 
251diff
changeset | 940 | |
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 941 | \begin{center}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 942 | \begin{tabular}{c@{\hspace{10mm}}c}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 943 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 944 |                     every state/.style={minimum size=0pt,
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
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: 
251diff
changeset | 954 | \end{tikzpicture}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
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: 
251diff
changeset | 958 | \hline | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
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: 
251diff
changeset | 967 | \end{tabular}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 968 | \end{tabular}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 969 | \end{center}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
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: 
251diff
changeset | 977 | |
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
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: 
251diff
changeset | 987 | \end{itemize}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
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: 
333diff
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 | |
| 1002 | $2$, which is the only accpting state in this NFA. But again in | |
| 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: 
333diff
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 | |
| 1060 | finding the next state trival, but might require $2^n$ times as many | |
| 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
 | |
| 1088 | accepting-states function for the DFA is true henevner at least one | |
| 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: 
333diff
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: 
333diff
changeset | 1254 | \end{tikzpicture}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1255 | \end{center}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1256 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1257 | |
| 492 | 1258 | By the way, we are not bothering with implementing the above | 
| 1259 | minimisation algorith: while up to now all the transformations used | |
| 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: 
268diff
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: 
270diff
changeset | 1288 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1289 | \begin{center}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1291 |                     every state/.style={minimum size=0pt,
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1292 | inner sep=2pt,draw=blue!50,very thick, | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
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: 
270diff
changeset | 1298 |             (q1) edge[bend left] node[above] {$b$} (q0)
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1300 |             (q1) edge node[above] {$a$} (q2)
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1301 |             (q2) edge [loop right] node {$a$} ()
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1302 |             (q0) edge [loop below] node {$b$} ();
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1303 | \end{tikzpicture}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1304 | \end{center}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1305 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1307 | system | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1308 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1313 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1314 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
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: 
270diff
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: 
270diff
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: 
318diff
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: 
270diff
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: 
270diff
changeset | 1329 | first equation is constructed similarly: there are three | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
349diff
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: 
270diff
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: 
270diff
changeset | 1333 | in the DFA. | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1334 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1335 | Having constructed the equational system, the question is | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
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: 
270diff
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: 
270diff
changeset | 1341 | into the other two. This gives | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1342 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1346 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1350 | Equation (4) to obtain the following two equations: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1351 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1355 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1356 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
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: 
270diff
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: 
344diff
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: 
344diff
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: 
344diff
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: 
270diff
changeset | 1367 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1371 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1372 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1375 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1378 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1381 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1384 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1385 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
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: 
270diff
changeset | 1389 | regular expression: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1390 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1393 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1394 | |
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
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: 
349diff
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: 
270diff
changeset | 1397 | expression | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1398 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1401 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1402 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1403 | \noindent | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1405 | for the other equations. This gives: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1406 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1411 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1412 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
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: 
270diff
changeset | 1417 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1418 | \[ | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1420 | \] | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
270diff
changeset | 1425 | string is also matched by the automaton. | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
268diff
changeset | 1430 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1431 | |
| 490 | 1432 | \subsection*{Regular Languages}
 | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1433 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
344diff
changeset | 1435 | the following overall picture: | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1436 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1437 | \begin{center}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1438 | \begin{tikzpicture}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1439 | \node (rexp)  {\bf Regexps};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1440 | \node (nfa) [right=of rexp] {\bf NFAs};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1441 | \node (dfa) [right=of nfa] {\bf DFAs};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
268diff
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: 
268diff
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: 
268diff
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: 
333diff
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: 
268diff
changeset | 1447 | \end{tikzpicture}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1448 | \end{center}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1449 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
268diff
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: 
344diff
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: 
268diff
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: 
268diff
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: 
268diff
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: 
268diff
changeset | 1456 | language. Again we did not prove this fact. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
268diff
changeset | 1459 | expressions can recognise the same set of languages: | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1460 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
268diff
changeset | 1462 | regular expression that recognises all its strings. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1463 | \end{quote}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1464 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1465 | \noindent or equivalently | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1466 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
268diff
changeset | 1468 | automaton that recognises all its strings. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1469 | \end{quote}
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
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: 
251diff
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: 
344diff
changeset | 1504 | runtime penalty. | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
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: 
344diff
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: 
251diff
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: 
270diff
changeset | 1524 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1525 | \[a^n\,b^n\] | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
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: 
269diff
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: 
269diff
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: 
324diff
changeset | 1577 | |
| 
794c599cee53
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
324diff
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 |