Download presentation
Presentation is loading. Please wait.
1
1 Lecture 20 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
2
2 Regular languages are a subset of LFSA
3
3 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- M such that L(M) = L –M is constructed from regular expression R Argue L(M) = L There exists an NFA- M such that L(M) = L L is in LFSA –By definition of L in LFSA and equivalence of LFSA and LNFA-
4
4 Visualization Regular Languages LFSA Regular Expressions NFA- ’s L L R M 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- M such that L(M) = L M is constructed from regular expression R Argue L(M) = L There exists an NFA- M such that L(M) = L L is in LFSA By definition of L in LFSA and equivalence of LFSA and LNFA-
5
5 Algorithm Specification Input –Regular expression R Output –NFA M such that L(M) = L(R) Regular expression R NFA- M A
6
6 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
7
7 Base Case Regular expression R has zero operators –No concatenation, union, Kleene closure –For any alphabet , only | | + 2 regular languages can be depicted by any regular expression with zero operators The empty language The language { } The | | languages consisting of one string {a} for all a in
8
8 Table lookup Finite number of base cases means we can use table lookup to handle them a a b b
9
9 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 = R 1 +R 2 R = R 1 R 2 R = R 1 *
10
10 Recursive Calls The algorithm recursively calls itself to generate NFA’s M 1 and M 2 which accept L(R 1 ) and L(R 2 ) The algorithm applies the appropriate construction –union –concatenation –Kleene closure to NFA’s M 1 and M 2 to produce an NFA M such that L(M) = L(R) 1) R = R 1 + R 2 2) R = R 1 R 2 3) R = R 1 *
11
11 Pseudocode Algorithm NFA RegExptoNFA(regular expression R) { regular expression R 1, R 2 ; NFA M 1, M 2 ; Modify R by removing unnecessary enclosing parentheses /* Base Case */ If R = a, return (NFA for {a}) /* include here */ If R = , return (NFA for {}) /* Recursive Case */ Find “last operator O” of regular expression R Identify regular expressions R 1 (and R 2 if necessary) M 1 = RegExptoNFA(R 1 ) M 2 = RegExptoNFA(R 2 ) /* if necessary */ return (OP(M1, M2)) /* OP is chosen based on O */ }
12
12 Example A: R = (b+a)a * Last operator is concatenation R 1 = (b+a) R 2 = a* Recursive call with R 1 = (b+a) B: R = (b+a) Extra parentheses stripped away Last operator is union R 1 = b R 2 = a Recursive call with R 1 = b
13
13 Example Continued C: R = b Base case NFA for {b} returned B: return to this invocation of procedure Recursive call where R = R 2 = 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 = R 2 = a*
14
14 Example Finished E: R = a* Last operator is Kleene closure R 1 = a Recursive call where R = R 1 = a F: R = a Base case NFA for {a} returned E: return to this invocation of procedure return (NFA for {a}*) A: return to this invocation of procedure return CONCAT(NFA for {b,a}, NFA for {a}*)
15
15 concatenate Pictoral View (b|a)a* (b|a)a* union baba b a Kleene Closure aa a a b a
16
16 Parse Tree We now present the “parse” tree for regular expression (b+a)a* concatenate unionKleene closure baa
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.