160 likes | 302 Views
Module 22. Regular languages are a subset of LFSA algorithm for converting any regular expression into an equivalent NFA Builds on existing algorithms described in previous lectures. Regular languages are a subset of LFSA. Reg. Lang. subset LFSA. Let L be an arbitrary regular language
E N D
Module 22 • Regular languages are a subset of LFSA • algorithm for converting any regular expression into an equivalent NFA • Builds on existing algorithms described in previous lectures
Reg. Lang. subset LFSA • Let L be an arbitrary regular language • Let R be the regular expression such that L(R) = L • R exists by definition of L is regular • Construct an NFA-l M such that L(M) = L • M is constructed from regular expression R • Argue L(M) = L • There exists an NFA-l M such that L(M) = L • L is in LFSA • By definition of L in LFSA and equivalence of LFSA and LNFA-l
L L R M Regular Expressions NFA-l’s Visualization • Let L be an arbitrary regular language • Let R be the regular expression such that L(R) = L • R exists by definition of L is regular • Construct an NFA-l M such that L(M) = L • M is constructed from regular expression R • Argue L(M) = L • There exists an NFA-l M such that L(M) = L • L is in LFSA • By definition of L in LFSA and equivalence of LFSA and LNFA-l Regular Languages LFSA
A Algorithm Specification * • Input • Regular expression R • Output • NFA M such that L(M) = NFA-l M Regular expression R
Recursive Algorithm • We have an inductive definition for regular languages and regular expressions • Our algorithm for converting any regular expression into an equivalent NFA is recursive in nature • Base Case • Recursive or inductive Case
Base Case • Regular expression R has zero operators • No concatenation, union, Kleene closure • For any alphabet S, only |S| + 2 regular languages can be depicted by any regular expression with zero operators • The empty language f • The language {l} • The |S| languages consisting of one string {a} for all a in S
Table lookup * • Finite number of base cases means we can use table lookup to handle them f l a b
Recursive Case • Regular expression R has at least one operator • This means R is built up from smaller regular expressions using the union, Kleene closure, or concatenation operators • More specifically, there are 3 cases: • R = R1+R2 • R = R1R2 • R = R1*
Recursive Calls 1) R = R1 + R22) R = R1 R23) R = R1* • The algorithm recursively calls itself to generate NFA’s M1 and M2 which accept L(R1) and L(R2) • The algorithm applies the appropriate construction • union • concatenation • Kleene closure to NFA’s M1 and M2 to produce an NFA M such that L(M) = L(R)
Pseudocode Algorithm * _____________ RegExptoNFA(_____________) { regular expression R1, R2; NFA M1, M2; Modify R by removing unnecessary enclosing parentheses /* Base Case */ If R = a, return (NFA for {a}) /* include l here */ If R = f, return (NFA for {}) /* Recursive Case */ Find “last operator O” of regular expression R Identify regular expressions R1 (and R2 if necessary) M1 = RegExptoNFA(R1) M2 = RegExptoNFA(R2) /* if necessary */ return (OP(M1, M2)) /* OP is chosen based on O */ }
Example A: R = (b+a)a* Last operator is concatenation R1 = (b+a) R2 = a* Recursive call with R1 = (b+a) B: R = (b+a) Extra parentheses stripped away Last operator is union R1 = b R2 = a Recursive call with R1 = b
Example Continued C: R = b Base case NFA for {b} returned B: return to this invocation of procedure Recursive call where R = R2 = a D: R = a Base case NFA for {a} returned B: return to this invocation of procedure return UNION(NFA for {b}, NFA for {a}) A: return to this invocation of procedure Recursive call where R = R2 = a*
Example Finished E: R = a* Last operator is Kleene closure R1 = a Recursive call where R = R1 = a F: R = a Base case NFA for {a} returned E: return to this invocation of procedure return (KLEENE(NFA for {a})) A: return to this invocation of procedure return CONCAT(NFA for {b,a}, NFA for {a}*)
(b|a) a* b a a a b a l l a l l l b l l a l a l b l Pictoral View concatenate (b|a)a* union Kleene Closure a
concatenate union Kleene closure b a a Parse Tree We now present the “parse” tree for regular expression (b+a)a*