<- previous index next ->
Conversion algorithm from a NFA to a regular expression. Start with the transition table for the NFA with the following state naming conventions: the first state is 1 or q1 or s1 which is the starting state. states are numbered consecutively, 1, 2, 3, ... n The transition table is a typical NFA where the table entries are sets of states and phi the empty set is allowed. The set F of final states must be known. We call the variable r a regular expression. We can talk about r being the regular expression with i,j subscripts ij Note r is just a (possibly) different regular expression from r 12 53 Because we need multiple columns in a table we are going to build, we also use a superscript in the naming of regular expression. 1 3 k k-1 r r r r are just names of different regular expressions 12 64 1k ij 2 We are going to build a table with n rows and n+1 columns labeled | k=0 | k=1 | k=2 | ... | k=n ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | n r | r | r | r | ... | r Only build column n 11 | 11 | 11 | 11 | | 11 for 1,final state ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | n The final regular expression r | r | r | r | ... | r is then the union, +, of 12 | 12 | 12 | 12 | | 12 the column n ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | n r | r | r | r | ... | r 13 | 13 | 13 | 13 | | 13 ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | r | r | r | r | ... | 21 | 21 | 21 | 21 | | ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | r | r | r | r | ... | 22 | 22 | 22 | 22 | | ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | r | r | r | r | ... | 23 | 23 | 23 | 23 | | ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | r | r | r | r | ... | 31 | 31 | 31 | 31 | | ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | r | r | r | r | ... | 32 | 32 | 32 | 32 | | ----+--------+-------+-------+-----+------ | 0 | 1 | 2 | | r | r | r | r | ... | 33 | 33 | 33 | 33 | | ^ 2 |- Note n rows, all pairs of numbers from 1 to n Now, build the table entries for the k=0 column: / 0 / +{ x | delta(q ,x) = q } i /= j r = / i j ij \ \ +{ x | delta(q ,x) = q } + epsilon i = j \ i j where delta is the transition table function, x is some symbol from sigma the q's are states 0 r could be phi, epsilon, a, 0+1, or a+b+d+epsilon ij notice there are no Kleene Star or concatenation in this column Next, build the k=1 column: 1 0 0 * 0 0 r = r ( r ) r + r note: all items are from the previous column ij i1 11 1j ij Next, build the k=2 column: 2 1 1 * 1 1 r = r ( r ) r + r note: all items are from the previous column ij i2 22 2j ij Then, build the rest of the k=k columns: k k-1 k-1 * k-1 k-1 r = r ( r ) r + r note: all items are from previous column ij ik kk kj ij Finally, for final states p, q, r the regular expression is n n n r + r + r 1p 1q 1r Note that this is from a constructive proof that every NFA has a language for which there is a corresponding regular expression. Some minimization rules for regular expressions These can be applied at every step. Note: phi is the empty set epsilon is the zero length string 0, 1, a, b, c, are symbols in sigma x is a variable or regular expression ( ... )( ... ) is concatenation ( ... ) + ( ... ) is union ( ... )* is the Kleene Closure = Kleene Star (phi)(x) = (x)(phi) = phi (epsilon)(x) = (x)(epsilon) = x (phi) + (x) = (x) + (phi) = x x + x = x (epsilon)* = (epsilon)(epsilon) = epsilon (x)* + (epsilon) = (x)* = x* (x + epsilon)* = x* x* (a+b) + (a+b) = x* (a+b) x* y + y = x* y (x + epsilon)x* = x* (x + epsilon) = x* (x+epsilon)(x+epsilon)* (x+epsilon) = x* Now for an example: Given M=(Q, sigma, delta, q0, F) as delta | a | b | c Q = { q1, q2} --------+------+------+----- sigma = { a, b, c } q1 | {q2} | {q2} | {q1} q0 = q1 --------+------+------+----- F = { q2} q2 | phi | phi | phi --------+------+------+----- | k=0 | k=1 (using e for epsilon) -----+-------------+------------------------------------ r | c + epsilon | (c+e)(c+e)* (c+e) + (c+e) = c* 11 | | -----+-------------+------------------------------------ r | a + b | (c+e)(c+e)* (a+b) + (a+b) = c* (a+b) 12 | | -----+-------------+------------------------------------ r | phi | phi (c+e)* (c+e) + phi = phi 21 | | -----+-------------+------------------------------------ r | epsilon | phi (c+e)* (a+b) + e = e 22 | | -----+-------------+------------------------------------ | k=0 | k=1 | k=2 (using e for epsilon) -----+-------------+----------+------------------------- r | c + epsilon | c* | 11 | | | -----+-------------+----------+------------------------- r | a + b | c* (a+b) | c* (a+b)(e)* (e) + c* (a+b) only final 12 | | | state -----+-------------+----------+------------------------- r | phi | phi | 21 | | | -----+-------------+----------+------------------------- r | epsilon | e | 22 | | | -----+-------------+----------+------------------------- the final regular expression minimizes to c* (a+b) Additional topics include Moore Machines and Mealy Machines
<- previous index next ->