182 |
182 |
183 \subsubsection*{Thompson Construction} |
183 \subsubsection*{Thompson Construction} |
184 |
184 |
185 The reason for introducing NFAs is that there is a relatively |
185 The reason for introducing NFAs is that there is a relatively |
186 simple (recursive) translation of regular expressions into |
186 simple (recursive) translation of regular expressions into |
187 NFAs. Consider the simple regular expressions $\varnothing$, |
187 NFAs. Consider the simple regular expressions $\ZERO$, |
188 $\epsilon$ and $c$. They can be translated as follows: |
188 $\ONE$ and $c$. They can be translated as follows: |
189 |
189 |
190 \begin{center} |
190 \begin{center} |
191 \begin{tabular}[t]{l@{\hspace{10mm}}l} |
191 \begin{tabular}[t]{l@{\hspace{10mm}}l} |
192 \raisebox{1mm}{$\varnothing$} & |
192 \raisebox{1mm}{$\ZERO$} & |
193 \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
193 \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
194 \node[state, initial] (q_0) {$\mbox{}$}; |
194 \node[state, initial] (q_0) {$\mbox{}$}; |
195 \end{tikzpicture}\\\\ |
195 \end{tikzpicture}\\\\ |
196 \raisebox{1mm}{$\epsilon$} & |
196 \raisebox{1mm}{$\ONE$} & |
197 \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
197 \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
198 \node[state, initial, accepting] (q_0) {$\mbox{}$}; |
198 \node[state, initial, accepting] (q_0) {$\mbox{}$}; |
199 \end{tikzpicture}\\\\ |
199 \end{tikzpicture}\\\\ |
200 \raisebox{2mm}{$c$} & |
200 \raisebox{2mm}{$c$} & |
201 \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
201 \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
567 that if in state $q_1$ consuming an $a$ will bring you to |
567 that if in state $q_1$ consuming an $a$ will bring you to |
568 $q_2$, and the secont that being in $q_2$ and consuming an $a$ |
568 $q_2$, and the secont that being in $q_2$ and consuming an $a$ |
569 will make you stay in $q_2$. The right-hand side of the |
569 will make you stay in $q_2$. The right-hand side of the |
570 first equation is constructed similarly: there are three |
570 first equation is constructed similarly: there are three |
571 incoming edges, therefore there are three terms. There is |
571 incoming edges, therefore there are three terms. There is |
572 one exception in that we also ``add'' $\epsilon$ to the |
572 one exception in that we also ``add'' $\ONE$ to the |
573 first equation, because it corresponds to the starting state |
573 first equation, because it corresponds to the starting state |
574 in the DFA. |
574 in the DFA. |
575 |
575 |
576 Having constructed the equational system, the question is |
576 Having constructed the equational system, the question is |
577 how to solve it? Remarkably the rules are very similar to |
577 how to solve it? Remarkably the rules are very similar to |
580 right-hand side of the equation. We can therefore eliminate |
580 right-hand side of the equation. We can therefore eliminate |
581 $q_1$ from the system by just substituting this equation |
581 $q_1$ from the system by just substituting this equation |
582 into the other two. This gives |
582 into the other two. This gives |
583 |
583 |
584 \begin{eqnarray} |
584 \begin{eqnarray} |
585 q_0 & = & \epsilon + q_0\,b + q_0\,a\,b + q_2\,b\\ |
585 q_0 & = & \ONE + q_0\,b + q_0\,a\,b + q_2\,b\\ |
586 q_2 & = & q_0\,a\,a + q_2\,a |
586 q_2 & = & q_0\,a\,a + q_2\,a |
587 \end{eqnarray} |
587 \end{eqnarray} |
588 |
588 |
589 \noindent where in Equation (4) we have two occurences |
589 \noindent where in Equation (4) we have two occurences |
590 of $q_0$. Like the laws about $+$ and $\cdot$, we can simplify |
590 of $q_0$. Like the laws about $+$ and $\cdot$, we can simplify |
591 Equation (4) to obtain the following two equations: |
591 Equation (4) to obtain the following two equations: |
592 |
592 |
593 \begin{eqnarray} |
593 \begin{eqnarray} |
594 q_0 & = & \epsilon + q_0\,(b + a\,b) + q_2\,b\\ |
594 q_0 & = & \ONE + q_0\,(b + a\,b) + q_2\,b\\ |
595 q_2 & = & q_0\,a\,a + q_2\,a |
595 q_2 & = & q_0\,a\,a + q_2\,a |
596 \end{eqnarray} |
596 \end{eqnarray} |
597 |
597 |
598 \noindent Unfortunately we cannot make any more progress with |
598 \noindent Unfortunately we cannot make any more progress with |
599 substituting equations, because both (6) and (7) contain the |
599 substituting equations, because both (6) and (7) contain the |
605 assume $+$ is symmetric, Equation (7) is of that form: $s$ is |
605 assume $+$ is symmetric, Equation (7) is of that form: $s$ is |
606 $q_0\,a\,a$ and $r$ is $a$. That means we can transform |
606 $q_0\,a\,a$ and $r$ is $a$. That means we can transform |
607 (7) to obtain the two new equations |
607 (7) to obtain the two new equations |
608 |
608 |
609 \begin{eqnarray} |
609 \begin{eqnarray} |
610 q_0 & = & \epsilon + q_0\,(b + a\,b) + q_2\,b\\ |
610 q_0 & = & \ONE + q_0\,(b + a\,b) + q_2\,b\\ |
611 q_2 & = & q_0\,a\,a\,(a^*) |
611 q_2 & = & q_0\,a\,a\,(a^*) |
612 \end{eqnarray} |
612 \end{eqnarray} |
613 |
613 |
614 \noindent Now again we can substitute the second equation into |
614 \noindent Now again we can substitute the second equation into |
615 the first in order to eliminate the variable $q_2$. |
615 the first in order to eliminate the variable $q_2$. |
616 |
616 |
617 \begin{eqnarray} |
617 \begin{eqnarray} |
618 q_0 & = & \epsilon + q_0\,(b + a\,b) + q_0\,a\,a\,(a^*)\,b |
618 q_0 & = & \ONE + q_0\,(b + a\,b) + q_0\,a\,a\,(a^*)\,b |
619 \end{eqnarray} |
619 \end{eqnarray} |
620 |
620 |
621 \noindent Pulling $q_0$ out as a single factor gives: |
621 \noindent Pulling $q_0$ out as a single factor gives: |
622 |
622 |
623 \begin{eqnarray} |
623 \begin{eqnarray} |
624 q_0 & = & \epsilon + q_0\,(b + a\,b + a\,a\,(a^*)\,b) |
624 q_0 & = & \ONE + q_0\,(b + a\,b + a\,a\,(a^*)\,b) |
625 \end{eqnarray} |
625 \end{eqnarray} |
626 |
626 |
627 \noindent This equation is again of the form so that we can |
627 \noindent This equation is again of the form so that we can |
628 apply Arden's rule ($r$ is $b + a\,b + a\,a\,(a^*)\,b$ and $s$ |
628 apply Arden's rule ($r$ is $b + a\,b + a\,a\,(a^*)\,b$ and $s$ |
629 is $\epsilon$). This gives as solution for $q_0$ the following |
629 is $\ONE$). This gives as solution for $q_0$ the following |
630 regular expression: |
630 regular expression: |
631 |
631 |
632 \begin{eqnarray} |
632 \begin{eqnarray} |
633 q_0 & = & \epsilon\,(b + a\,b + a\,a\,(a^*)\,b)^* |
633 q_0 & = & \ONE\,(b + a\,b + a\,a\,(a^*)\,b)^* |
634 \end{eqnarray} |
634 \end{eqnarray} |
635 |
635 |
636 \noindent Since this is a regular expression, we can simplify |
636 \noindent Since this is a regular expression, we can simplify |
637 away the $\epsilon$ to obtain the slightly simpler regular |
637 away the $\ONE$ to obtain the slightly simpler regular |
638 expression |
638 expression |
639 |
639 |
640 \begin{eqnarray} |
640 \begin{eqnarray} |
641 q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^* |
641 q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^* |
642 \end{eqnarray} |
642 \end{eqnarray} |