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