Transcript Module 22
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
1
Regular languages are a subset of
LFSA
2
Reg. Lang. subset LFSA
• Let L be an arbitrary
• Let R be the
– R exists by definition of
• Construct an
– M is constructed from
• Argue
• There exists an
• L is in
– By definition of
3
Visualization
L
L
Regular
Languages
R
Regular
Expressions
LFSA
M
NFA-l’s
4
Algorithm Specification
• Input
– Regular expression R
• Output
– NFA M such that L(M) =
Regular expression R
A
NFA-l M
5
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
6
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
7
Table lookup
• Finite number of base cases means we can
use table lookup to handle them
f
l
a
b
8
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*
9
Recursive Calls
1) R = R1 + R2
2) R = R1 R2
3) 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)
10
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 */
}
11
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
12
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*
13
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}*)
14
Pictoral View
concatenate
(b|a)a*
a
l
l
l
b
(b|a) b
lunion
a
b
a
l
l
a
l
l Kleene
a* Closure
l
a
l
aa
15
Parse Tree
We now present the “parse” tree for regular expression (b+a)a*
concatenate
union
b
Kleene closure
a
a
16