Animated Conversion of Regular Expressions to C Code On the regular expression: ((a⋅ b)|c)*
Thompson’s construction Convert the regular expression to an NFA.
Step 1: construct a parse tree for the expression ((a⋅ b)|c) Step 1: construct a parse tree for the expression ((a⋅ b)|c)* giving each nested expression a unique name. r6 r5 * r3 | r4 r1 ⋅ r2 c a b
Step 2: construct NFA for r1. * r3 | r4 r1 ⋅ r2 c a b
Step 3: construct NFA for r2. 1 2 r6 r5 * b 3 4 r3 | r4 r1 ⋅ r2 c a b
Step 4: construct NFA for r3. b 1 2 4 r6 r5 * r3 | r4 r1 ⋅ r2 c a b
Step 5: construct NFA for r4. b 1 2 4 r6 r5 * c 5 6 r3 | r4 r1 ⋅ r2 c a b
Step 6: construct NFA for r5. b r5 * 1 2 4 𝜀 𝜀 r3 | r4 7 8 𝜀 𝜀 5 6 r1 ⋅ r2 c c a b
Step 7: construct NFA for r6. 𝜀 a b 1 2 4 𝜀 𝜀 𝜀 𝜀 10 9 7 8 𝜀 𝜀 5 6 c 𝜀
Subset construction Convert the NFA to a DFA.
Draw transition table for DFA 2 4 b 1 a 5 6 c 7 𝜀 8 9 10 NFA States DFA State Next State a b c
Add 𝜀-closure(9) as DFA start state 2 4 b 1 a 5 6 c 7 𝜀 8 9 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A
Mark state A 2 4 1 5 6 7 8 9 b a c 𝜀 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A
Compute 𝜀-closure(move(A, a)) 2 4 b 1 a 5 6 c 7 𝜀 8 9 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A B {2}
Compute 𝜀-closure(move(A, b)) 2 4 b 1 a 5 6 c 7 𝜀 8 9 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A B - {2}
Compute 𝜀-closure(move(A, c)) 2 4 b 1 a 5 6 c 7 𝜀 8 9 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A B - C {2} {6, 10, 7, 1, 5}
Mark state B 2 4 1 5 6 7 8 9 b a c 𝜀 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A B - C {2} B {6, 10, 7, 1, 5}
Compute 𝜀-closure(move(A, c)) 2 4 b 1 a 5 6 c 7 𝜀 8 9 10 NFA States DFA State Next State a b c {9, 7, 1, 5, 10} A B - C {2} B {6, 10, 7, 1, 5}