Download presentation
Presentation is loading. Please wait.
Published byJoy Turner Modified over 8 years ago
1
Converting Regular Expressions to NFAs Empty string is a regular expression denoting { } a is a regular expression denoting {a} for any a in i f start i a f Thompson’s Construction
2
Converting Regular Expressions to NFAs If P and Q are regular expressions with NFAs N p, N q : P | Q (union) PQ (concatenation) NpNp NqNq if start NqNq NpNp i f
3
Converting Regular Expressions to NFAs If Q is a regular expression with NFA N q : Q* (closure) NqNq f i start
4
Example (ab* | a*b)* Starting with: 43 a*b b a start ab* 12 a b start 12 a 43 56 ab* | a*b a b b start
5
Example (ab* | a*b)* 12 a 43 56 ab* | a*b a b b start 12 a 43 56 (ab* | a*b)* 78 b b a start
6
Properties of Construction 1.N(r) has #of states 2*(#symbols + #operators) of r 2.N(r) has exactly one start and one accepting state 3.Each state of N(r) has at most one outgoing edge a or at most two outgoing -transition Let r be a regular expression, with NFA N(r), then
7
Detailed Example r 13 r 12 r5r5 r3r3 r 11 r4r4 r9r9 r 10 r8r8 r7r7 r6r6 r0r0 r1r1 r2r2 b * c a a | ( ) b | * c (ab*c) | (a(b|c*)) Parse Tree for this regular expression: What is the NFA? Let’s construct it !
8
Detailed Example – Construction(1) r3:r3: a r0:r0: b r2:r2: c b r1:r1:r 4 : r 1 r 2 b c r 5 : r 3 r 4 b ac (ab*c) | (a(b|c*))
9
Detailed Example – Construction(2) r 11 : a r7:r7: b r6:r6: c c r 9 : r 7 | r 8 b r 10 : r 9 c r8:r8: c r 12 : r 11 r 10 b a (ab*c) | (a(b|c*))
10
Detailed Example – Final Step r 13 : r 5 | r 12 b ac c b a 1 6543 8 2 10 9121314 11 15 7 16 17
11
Converting NFAs to DFAs (subset construction) Idea: Each state in the new DFA will correspond to some set of states from the NFA. The DFA will be in state {s 0,s 1,…} after input if the NFA could be in any of these states for the same input. Input: NFA N with state set S N, alphabet , start state s N, final states F N, transition function T N : S N x { U } S N Output: DFA D with state set S D, alphabet , start state s D = -closure(s N ), final states F D, transition function T D : S D x S D
12
Terminology: -closure -closure(T) = T + all NFA states reachable from any state in T using only transitions. 1 5 2 43 b a b b a -closure({1,2,5}) = {1,2,5} -closure({4}) = {1,4} -closure({3}) = {1,3,4} -closure({3,5}) = {1,3,4,5}
13
Illustrating Conversion – An Example First we calculate: -closure(0) (i.e., state 0) -closure(0) = {0, 1, 2, 4, 7} (all states reachable from 0 on -moves) Let A={0, 1, 2, 4, 7} be a state of new DFA, D. 01 23 54 6789 10 a a b b b start Start with NFA: (a | b)*abb
14
Conversion Example – continued (1) b : -closure(move(A,b)) = -closure(move({0,1,2,4,7},b)) adds {5} ( since move(4,b)=5) From this we have : -closure({5}) = {1,2,4,5,6,7} (since 5 6 1 4, 6 7, and 1 2 all by -moves) Let C={1,2,4,5,6,7} be a new state. Define Dtran[A,b] = C. 2 nd, we calculate : a : -closure(move(A,a)) and b : -closure(move(A,b)) a : -closure(move(A,a)) = -closure(move({0,1,2,4,7},a))} adds {3,8} ( since move(2,a)=3 and move(7,a)=8) From this we have : -closure({3,8}) = {1,2,3,4,6,7,8} (since 3 6 1 4, 6 7, and 1 2 all by -moves) Let B={1,2,3,4,6,7,8} be a new state. Define Dtran[A,a] = B.
15
Conversion Example – continued (2) 3 rd, we calculate for state B on {a,b} a : -closure(move(B,a)) = -closure(move({1,2,3,4,6,7,8},a))} = {1,2,3,4,6,7,8} = B Define Dtran[B,a] = B. b : -closure(move(B,b)) = -closure(move({1,2,3,4,6,7,8},b))} = {1,2,4,5,6,7,9} = D Define Dtran[B,b] = D. 4 th, we calculate for state C on {a,b} a : -closure(move(C,a)) = -closure(move({1,2,4,5,6,7},a))} = {1,2,3,4,6,7,8} = B Define Dtran[C,a] = B. b : -closure(move(C,b)) = -closure(move({1,2,4,5,6,7},b))} = {1,2,4,5,6,7} = C Define Dtran[C,b] = C.
16
Conversion Example – continued (3) 5 th, we calculate for state D on {a,b} a : -closure(move(D,a)) = -closure(move({1,2,4,5,6,7,9},a))} = {1,2,3,4,6,7,8} = B Define Dtran[D,a] = B. b : -closure(move(D,b)) = -closure(move({1,2,4,5,6,7,9},b))} = {1,2,4,5,6,7,10} = E Define Dtran[D,b] = E. Finally, we calculate for state E on {a,b} a : -closure(move(E,a)) = -closure(move({1,2,4,5,6,7,10},a))} = {1,2,3,4,6,7,8} = B Define Dtran[E,a] = B. b : -closure(move(E,b)) = -closure(move({1,2,4,5,6,7,10},b))} = {1,2,4,5,6,7} = C Define Dtran[E,b] = C.
17
Conversion Example – continued (4) Dstates Input Symbol ab A B C B B D C B C E B C D B E A C BDE startbb b b b a a a a This gives the transition table Dtran for the DFA of:
18
Algorithm For Subset Construction push all states in T onto stack; initialize -closure(T) to T; while stack is not empty do begin pop t, the top element, off the stack; for each state u with edge from t to u labeled do if u is not in -closure(T) do begin add u to -closure(T) ; push u onto stack end computing the -closure
19
Algorithm For Subset Construction – (2) initially, -closure(s 0 ) is only (unmarked) state in Dstates; while there is unmarked state T in Dstates do begin mark T; for each input symbol a do begin U := -closure(move(T,a)); if U is not in Dstates then add U as an unmarked state to Dstates; Dtran[T,a] := U end
20
Example 2: Subset Construction NFA 1 5 2 43 b a b a,b start NFA N with State set S N = {1,2,3,4,5}, Alphabet = {a,b} Start state s N =1, Final states F N ={5}, Transition function T N : S N x { } S N ab 13-2 255, 4- 3-4- 455- 5---
21
Example 2: Subset Construction NFA 1,2 T -closure(move(T, a)) -closure(move(T, b)) {1,2} 1 5 2 43 b a b a,b start
22
Example 2: Subset Construction NFA 1,24,5 3,5 b a T -closure(move(T, a)) -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5} {4,5} 1 5 2 43 b a b a,b start
23
Example 2: Subset Construction 1 5 2 43 b a b a,b NFA 1,24,5 3,54 b a b T -closure(move(T, a)) -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5} {4} start
24
Example 2: Subset Construction 1 5 2 43 b a b a,b NFA 1,24,55 3,54 a,bb a b T -closure(move(T, a)) -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5}{5} {4} {5} start
25
Example 2: Subset Construction 1 5 2 43 b a b a,b NFA 1,24,55 3,54 a,b b a b T -closure(move(T, a)) -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5}{5} {4}{5} -- start
26
Example 2: Subset Construction 1 5 2 43 b a b a,b NFA T -closure(move(T, a)) -closure(move(T, b)) {1,2}{3,5}{4,5} {3,5}-{4} {4,5}{5} {4}{5} {5}{5}-- All final states since the NFA final state is included start 1,24,55 3,54 a,b b a b start
27
Example 3: Subset Construction 1 5 2 43 b a b b a NFA start
28
Example 3: Subset Construction 1 5 2 43 b a b b a 1 1,3,4 2 1,4,5 a b b NFADFA 1,3, 4,5 a a a b b b start
29
Example 4: Subset Construction 12 54 b 1,2,4 NFADFA a b a 5 3,43,5 4 3 3 b b a b a b b a start
30
Converting DFAs to REs 1.Combine serial links by concatenation 2.Combine parallel links by alternation 3.Remove self-loops by Kleene closure 4.Select a node (other than initial or final) for removal. Replace it with a set of equivalent links whose path expressions correspond to the in and out links 5.Repeat steps 1-4 until the graph consists of a single link between the entry and exit nodes.
31
Example 12345 67 d a b c d 0 da d b b c 12345 67 da|b|cd 0 da d b b|c parallel edges become alternation start a b c
32
Example 345 d (a|b|c) d d 0 a b (b|c) d 12345 67 da|b|cd 0 da d b b|c serial edges become concatenation start
33
Example 345 d (a|b|c) d d 0 a b (b|c) d 345 d (a|b|c) d d 0 a b(b|c)da Find paths that can be “shortened” start
34
Example 345 d (a|b|c) d d 0 a b(b|c)da 345 d (a|b|c) d (b(b|c)da)*d 0 a 5 d (a|b|c) d(b(b|c)da)*d 0 a eliminate self-loops serial edges become concatenation start
35
Relationship among RE, NFA, DFA The set of strings recognized by an NFA can be described by a Regular Expression. The set of strings described by a Regular Expression can be recognized by an NFA. The set of strings recognized by an DFA can be described by a Regular Expression. The set of strings described by a Regular Expression can be recognized by an DFA. DFAs, NFAs, and Regular Expressions all have the same “power”. They describe “Regular Sets” (“Regular Languages”) The DFA may have a lot more states than the NFA.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.