Equivalence of DFA and Regular Expressions CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Fall 2018 Chinese University of Hong Kong 1/18
Three ways of doing it q 1 expressions regular NFA DFA (0+1) 1 0 0,1 q 2 q 0 1 0 1 0 0 1 q 2 q 1 q 0 2/18 L = { x ∈ Σ ∗ | x ends in 01 } Σ = { 0 , 1 } ∗ 01
They are equally powerful DFA NFA regular expressions regular languages 3/18
q 0 q 1 0 q 0 q 1 q 2 0 1 4/18 Examples: regular expression → NFA R 1 = 0 R 2 = 01
5/18 q 5 1 NFA 3 0 1 0 q 6 0 q 4 q 2 NFA 3 q 2 q 0 q 1 q 3 Examples: regular expression → NFA ε ε R 3 = 0+01 ε ε ε ∗ ε R 4 = (0+01) ε ε q ′ q ′
Regular expressions In general, how do we convert a regular expression to an NFA? rules 6/18 A regular expression over Σ is an expression formed by the following • The symbols ∅ and ε are regular expressions • Every symbol in Σ is a regular expression • If Σ = { 0 , 1 } , then 0 and 1 are both regular expressions • If R asd S are regular expressions, so are R + S , RS and R ∗
7/18 q 0 1 q 1 q 0 1 0 q 1 0 regular q 0 q 0 expression General method when Σ = { 0 , 1 } = ⇒ NFA ∅ ε
General method q 0 q 1 NFA R q 0 q 1 NFA S regular NFA R q 1 NFA R expression RS q 0 8/18 NFA S = ⇒ NFA ε ε ε ε ε R + S ε ε ε ε R ∗ ε ε
Roadmap regular expressions 2-state GNFA GNFA NFA 9/18 ✓
Simplify the NFA First we simplify the NFA so that • It has exactly one accepting state • No arrows come into the start state • No arrows go out of the accepting state q 0 q 1 q 3 q 4 q 5 10/18
Simplify the NFA First we simplify the NFA so that • It has exactly one accepting state • No arrows come into the start state • No arrows go out of the accepting state q 0 q 1 q 3 q 4 q 5 10/18 ε ε ε
Simplify the NFA q 0 q 1 q 2 q 3 0 1 1 0 • It has exactly one accepting state • No arrows come into the start state • No arrows go out of the accepting state 11/18
Simplify the NFA q 0 q 1 q 2 q 3 0 1 1 0 11/18 ε ε • It has exactly one accepting state ✓ • No arrows come into the start state ✓ • No arrows go out of the accepting state ✓
Generalized NFAs A generalized NFA is an NFA whose transitions are labeled by regular expressions, like q 0 q 1 q 2 01 12/18 0 ∗ 1 ε + 10 ∗ 0 ∗ 11
GNFA state elimination regular expressions 2-state GNFA GNFA NFA We will eliminate every state but the start and accepting states 13/18
State elimination q 0 q 2 q 1 q 0 01 q 2 q 0 q 1 01 q 2 q 1 14/18 0 ∗ 1 ε + 10 ∗ 0 ∗ 11 ⇓ ( ε + 10 ∗ )( 0 ∗ 1 ) ∗ 0 ∗ 11 ⇓ ( ε + 10 ∗ )( 0 ∗ 1 ) ∗ 0 ∗ 11 + 01
State elimination: general method R 2 v q u by R 4 R 3 R 1 v q u Replace 15/18 To eliminate state q , for every pair of states ( u , v ) such that u → q → v R 1 R ∗ 2 R 3 + R 4 Remember to do this even when u = v
Roadmap regular regular expression R A 2 -state GNFA is the same as a R q 1 q 0 16/18 NFA GNFA GNFA 2-state expressions ✓ ✓ ✓ ✓
Conversion example 0 1 00 1 q 1 q 2 q 3 0 1 00 1 1 Check: 1 After eliminating q 2 : q 1 q 2 0 1 1 0 q 0 1 q 0 00 1 q 1 q 2 q 3 0 1 1 0 After eliminating q 1 : q 0 q 1 q 2 q 3 0 1 17/18 ε ε
Conversion example q 0 0 1 1 0 q 2 q 1 1 0 1 00 1 Check: q 3 q 2 q 1 q 0 After eliminating q 2 : 17/18 1 q 3 q 3 q 2 q 1 q 0 After eliminating q 1 : q 1 0 q 2 1 0 ε ε 00 ∗ 1 + 1 0 ∗ 1 ε 0 ∗ 1 ( 00 ∗ 1 + 1 ) ∗
Conversion example q 0 0 1 1 0 q 2 q 1 Check: q 3 q 2 q 1 q 0 After eliminating q 2 : 17/18 q 3 1 q 2 q 1 q 1 q 0 After eliminating q 1 : 0 1 q 2 q 3 0 ε ε 00 ∗ 1 + 1 0 ∗ 1 ε 0 ∗ 1 ( 00 ∗ 1 + 1 ) ∗ 0 ∗ 1 ( 00 ∗ 1 + 1 ) ∗ ? =
Check your answer! 0 1 00 1 Yes have this form? Does every string ending in 1 0 1 0 1 Always ends in 1 1 18/18 q 1 (0+1) All strings ending in 1 0 1 1 0 q 2 ∗ 1
Check your answer! q 1 Yes have this form? Does every string ending in 1 Always ends in 1 (0+1) All strings ending in 1 0 1 1 0 q 2 18/18 ∗ 1 0 ∗ 1 ( 00 ∗ 1 + 1 ) ∗ = 0 ∗ 1 ( 0 ∗ 1 ) ∗
Recommend
More recommend