Conversions between NFAs and REs CS 350 — Fall 2018 gilray.org/classes/fall2018/cs350/
Converting REs to NFAs
q0 q1 ℒ(Ø) ℒ(𝜖) q0 a q0 q1 ℒ(a)
|𝐹|=1 As we build up a corresponding NFA for an RE, we want to enforce that: |𝐹|=1
ℒ( 𝑒 0 ) 𝑒 0
ℒ( 𝑒 0 𝑒 1 ) 𝑒 0 𝑒 1
ℒ( 𝑒 0 𝑒 1 ) 𝑒 0 𝜖 𝑒 1
ℒ( 𝑒 0 𝑒 1 )=ℒ( 𝑄 ′ , Σ ′ , 𝛿 ′ , 𝑞 00 ,{ 𝑞 1𝐹 }) where ℒ( 𝑒 0 )=ℒ( 𝑄 0 , Σ 0 , 𝛿 0 , 𝑞 00 ,{ 𝑞 0𝐹 }) ℒ( 𝑒 1 )=ℒ( 𝑄 1 , Σ 1 , 𝛿 1 , 𝑞 10 ,{ 𝑞 1𝐹 }) 𝑄 ′ = 𝑄 0 ∪ 𝑄 1 Σ ′ = Σ 0 ∪ Σ 1 𝛿 ′ = 𝛿 0 ∪ 𝛿 1 ∪{( 𝑞 0𝐹 ,𝜖, 𝑞 10 )}
ℒ( 𝑒 0 | 𝑒 1 ) 𝑒 0 𝑒 1
ℒ( 𝑒 0 | 𝑒 1 ) 𝑒 0 𝜖 𝜖 𝑒 1 𝜖 𝜖
ℒ( 𝑒 0 | 𝑒 1 )=ℒ( 𝑄 ′ , Σ ′ , 𝛿 ′ , 𝑞 0 ,{ 𝑞 𝐹 }) where ℒ( 𝑒 0 )=ℒ( 𝑄 0 , Σ 0 , 𝛿 0 , 𝑞 00 ,{ 𝑞 0𝐹 }) ℒ( 𝑒 1 )=ℒ( 𝑄 1 , Σ 1 , 𝛿 1 , 𝑞 10 ,{ 𝑞 1𝐹 }) 𝑄 ′ = 𝑄 0 ∪ 𝑄 1 ∪{ 𝑞 0 , 𝑞 𝐹 } Σ ′ = Σ 0 ∪ Σ 1 𝛿 ′ = 𝛿 0 ∪ 𝛿 1 ∪{( 𝑞 0 ,𝜖, 𝑞 00 ),( 𝑞 0 ,𝜖, 𝑞 10 ), ( 𝑞 0𝐹 ,𝜖, 𝑞 𝐹 ),( 𝑞 1𝐹 ,𝜖, 𝑞 𝐹 )}
ℒ( 𝑒 0 ∗ ) 𝑒 0
ℒ( 𝑒 0 ∗ ) 𝑒 0 𝜖 𝜖
𝛿 ′ = 𝛿 0 ∪ 𝛿 1 ∪{( 𝑞 𝐹 ,𝜖, 𝑞 00 ),( 𝑞 0𝐹 ,𝜖, 𝑞 𝐹 )} ℒ( 𝑒 0 ∗ )=ℒ( 𝑄 ′ , Σ ′ , 𝛿 ′ , 𝑞 𝐹 ,{ 𝑞 𝐹 }) where ℒ( 𝑒 0 )=ℒ( 𝑄 0 , Σ 0 , 𝛿 0 , 𝑞 00 ,{ 𝑞 0𝐹 }) 𝑄 ′ = 𝑄 0 ∪ 𝑄 1 ∪{ 𝑞 𝐹 } Σ ′ = Σ 0 ∪ Σ 1 𝛿 ′ = 𝛿 0 ∪ 𝛿 1 ∪{( 𝑞 𝐹 ,𝜖, 𝑞 00 ),( 𝑞 0𝐹 ,𝜖, 𝑞 𝐹 )}
Show the NFA for L((a|b)*c*). Try an example: Show the NFA for L((a|b)*c*).
Show the NFA for L((a|b)*c*). Try an example: Show the NFA for L((a|b)*c*). a q0 q1 b q2 q3
Show the NFA for L((a|b)*c*). Try an example: Show the NFA for L((a|b)*c*). a q0 q1 𝜖 𝜖 q4 q5 b q2 q3 𝜖 𝜖
Show the NFA for L((a|b)*c*). Try an example: Show the NFA for L((a|b)*c*). 𝜖 a q0 q1 𝜖 𝜖 q6 𝜖 q4 q5 b q2 q3 𝜖 𝜖
Show the NFA for L((a|b)*c*). Try an example: Show the NFA for L((a|b)*c*). 𝜖 a q0 q1 𝜖 𝜖 q6 𝜖 q4 q5 b q2 q3 𝜖 𝜖 𝜖 c q9 q7 q8 𝜖
Show the NFA for L((a|b)*c*). Try an example: Show the NFA for L((a|b)*c*). 𝜖 a q0 q1 𝜖 𝜖 q6 𝜖 q4 q5 b q2 q3 𝜖 𝜖 𝜖 𝜖 c q9 q8 𝜖 q7
Converting NFAs/DFAs to REs (intuitions, examples)
Idea 1: Permit whole REs on (G)NFA edges Idea 2: Repeatedly remove one state until a RE can simply be read off the (G)NFA.
Idea 1: Permit whole REs on (G)NFA edges Idea 2: Repeatedly remove one state until a RE can simply be read off the (G)NFA. q0 a c q2 q3 b q1 d e
Idea 1: Permit whole REs on (G)NFA edges Idea 2: Repeatedly remove one state until a RE can simply be read off the (G)NFA. q0 ae*(c|d) q0 a c q2 q3 q3 b q1 d q1 be*(c|d) e
Alternative paths may have their REs disjoined: Basic simplification rules: Disjunction e0 q0 q1 e1 Alternative paths may have their REs disjoined: e0 | e1 q0 q1
Sequential paths may have their REs juxtaposed: Basic simplification rules: Juxtaposition q2 e1 q0 q1 e0 q3 e2 Sequential paths may have their REs juxtaposed: q2 q0 e0e1 q3 e0e2
Self-loops can be replaced by an edge with a kleene-star of the RE: Basic simplification rules: Star e1 q0 q1 q2 e0 e2 Self-loops can be replaced by an edge with a kleene-star of the RE: q0 q1 q1’ q2 e0 e1* e2 q0 q2 e0e1*e2
Try an example: b q0 q2 b a a q1 b
Try an example: b q0 q2 b ab*a
Try an example: q0 q2 b|ab*a b
Try an example: q0 q’0 q2 b* b|ab*a
Thus possible regexes are: b*(b|ab*a) Try an example: q0 q2 b*(b|ab*a) Thus possible regexes are: b*(b|ab*a) Or: b+|b*ab*a
Try an example: a q0 q2 a b b b b q1 a q3 a
Try an example: aa q0 ba ab b b bb q1 a q3 a
Try an example: aa q0 ba ab bb bb ba q3 ab aa
Try an example: aa|bb q0 ab|ba q3 aa|bb
Try an example: aa|bb|(ab|ba)(aa|bb)*(ab|ba) q0 So an answer is: (aa|bb|(ab|ba)(aa|bb)*(ab|ba))*
Models of regular languages “Converts to” GNFA “Converts to” RE DFA “Minimizes to” NFA “Converts to” “Converts to”