Regular Expression to NFA COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
RE to NFA So far, RGR RGL Minimum DFA RE NFA DFA Done Soon Now
RE to NFA Algorithm 1 (bottom-up): Recursively build the FSA, Mimicking structure of the RE. Each FSA built has one start state, and one final state. Conversions: if ø, if ℇ, and if a. 2 1 1 1 2 a
RE to NFA if P + Q, or if PQ, and if P*. 1 ε Q P 2 P Q ε 1 2 1 P ε 2
RE to NFA example: (b (aba + ε) a)* b a b a a 3 7 a b ε 1 2 3 4 5 6 7 8 a 9 10 11 a 3 4 5 6 7 8 a b ε
RE to NFA (b (aba + ε) a)* 3 7 a b ε 3 7 a b ε 9 12 3 7 a b ε 9 12 4 5 6 7 8 a b ε 3 4 5 6 7 8 a b ε 13 9 12 3 4 5 6 7 8 a b ε 13 9 12 2 1
3 4 5 6 7 8 a b ε 13 9 12 2 1 RE to NFA (b (aba + ε) a)* 3 4 5 6 7 8 a b ε 13 9 12 2 1 10 11 2 12 3 4 6 7 ε a 8 13 14 1 b 10 5 9 11 15
Algorithm 2 (top-down): 4 Rules Start: Apply Rules: RE to NFA Algorithm 2 (top-down): 4 Rules Start: Apply Rules: E a a* ε ab a b b a + b a
RE to NFA Example (a + b)* (aa + bb) (a + b)* aa + bb aa a + b ε bb ε
RE to NFA (another Example) ba(a + b)* ab b a ε
summary Algorithm 1: Builds FSA bottom up Good for machines Bad for humans Algorithm 2: Builds FSA top down Bad for machines Good for humans
RE to NFA So far, RGR RGL Minimum DFA RE NFA DFA Two algorithms Done Soon Two algorithms