Scanner Construction from Regex
Scanner Construction from Regex
FALL 2010
Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.
Students enrolled in Comp 412 at Rice University have explicit permission to make
copies of these materials for their personal use.
Faculty from other educational institutions may use these materials for nonprofit
educational purposes, provided this copyright notice is preserved.
Quick Review
Last class:
— The scanner is the first stage in the front end
— Specifications can be expressed using regular expressions
— Build tables and code from a DFA
So,
• r17 takes it through s0, s1, s2 and accepts
• r takes it through s0, s1 and fails
• a takes it straight to se
0,1,2,3,4 All
r ,5,6,7,8, others
Char next character 9
State s0
s0 s1 se se
while (Char EOF)
State (State,Char)
Char next character s1 se s2 se
0,1,2,3,4 All
Char next character r ,5,6,7,8, other
State s0 9 s
while (Char EOF) s0 s1 se se
Next (State,Char)
start error error
Act (State,Char)
perform action Act s1 se s2 se
State Next error add error
Char next character
s2 se s2 se
if (State is a final state ) error add error
then report success se se se se
else report failure error error error
(0|1|2| … 9)
S2 S3
0,1,2
r 3 0,1
S0 S1 S5 S6
4,5,6,7,8,9
S4
r 3 0,1
S0 S1 S5 S6
State 4,5,6
r 0,1 2 3 other
Action 7,8,9
1
0 e e e e e
start
2 2 5 4
1 e e
add add add add
3 3 3 3 e
2 e
add add add add exit
e
3,4 e e e e e
exit
6 e
5 e e e e
add exit
x
6 e e e e e
exit
e e e e e e e
(0|1|2| … 9)
S2 S3
but we
Comp don’t
412, worry (much) about number of
Fall 2010 4,5,6,7,8,9 11
states. S4
Where are we going?
• We will show how to construct a finite state automaton
to recognize any RE Introduce NFAs
• Overview:
— Direct construction of a nondeterministic finite automaton
(NFA) to recognize a given RE
Easy to build in an algorithmic way
Requires -transitions to combine regular subexpressions
— Construct a deterministic finite automaton (DFA) to
simulate the NFA
Use a set-of-states construction Optional, but
worthwhile
— Minimize the number of states in the DFA
Hopcroft state minimization algorithm
— Generate the scanner code
Additional specifications needed for the actions
b a
a b b
S0 S1 S2 S3
a
a
a|b
a b b
S0 S1 S2 S3 S4
NFA
NFA becomes an NFA
Scanner generators
• Lex and Flex work along these lines
• Algorithms are well-known and well-understood
• Key issue is interface to parser (define all parts of
speech)
• You could build one in a weekend!
Key idea
• NFA pattern for each symbol & each operator
• Join them with moves in precedence order
a a b
S0 S1 S0 S1 S3 S4
a
S1 S2
S0 S5 S0 S1
a
S3 S4
b
S3 S4
NFA for a*
NFA for a | b
b
S1 S2
2. b | c
S0 S5
c
S3 S4
3. ( b | c )* b
S2 S3
S0 S1 S6 S7
c
S4 S5
4. a ( b | c )*
b
S4 S5
a
S0 S1 S2 S3 S8 S9
c
S6 S7
b|c
But, we can automate production
a
S0 S1 of the more complex NFA
version ...
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 27
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 28
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 29
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 30
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 31
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9,
s1 q4, q6, q9 none q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 32
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 33
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 34
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 35
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none
q3, q4, q6
s3 q7, q8, q9, none
q3, q4, q6
Comp 412, Fall 2010 36
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
q7 is the core
state of s3
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 37
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
q5 is the core
state of s2
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 38
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
q1, q2, q3,
s0 q0 none none
q4, q6, q9
q1, q2, q3, q5, q8, q9, q7, q8, q9,
s1 q4, q6, q9 none q3, q4, q6 q3, q4, q6
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6
Comp 412, Fall 2010 Final states because of q9 39
NFA DFA with Subset Construction
a ( b | c )* : b
q4 q5
a
q0 q1 q2 q3 q8 q9
q6 c q7
States -closure(Move(s,*))
DFA NFA a b c
s0 q0 s1 none none
q1, q2, q3,
s1 none s2 s3
q4, q6, q9
q5, q8, q9,
s2 none s2 s3
q3, q4, q6
b
a b c
s2 s0 s1 none none
b
a s1 none s2 s3
s0 s1 b c
c s2 none s2 s3
s3
c s3 none s2 s3
b|c
But, remember
our goal: a
S0 S1
Comp 412, Fall 2010 41
Where are we? Why are we doing this?
RE NFA (Thompson’s construction)
• Build an NFA for each term
• Combine them with -moves
a b c
s1 s2 s3 s4
abc | bc | ad
s0 s5 b s6 c s7
s6
d
b Subset construction eliminates -
s1 s2 c s3 transitions and merges the paths for
a
a. It leaves duplicate tails, such as
s0 b s4 c s5 bc.
s8 a s9 d s10
a b c
s1 s2 s3
subset(reverse(NFA))
a d s11
s8 s9
d
a s2
b
And subset it, again
b c
s0 s3 s11
The Cycle of Constructions
Minimal DFA
minimal
RE NFA DFA
Comp 412, Fall 2010 DFA 46
Brzozowski
Brzozowski