Professional Documents
Culture Documents
CPSC 388 - Compiler Design and Construction: Scanner - Regular Expressions To DFA
CPSC 388 - Compiler Design and Construction: Scanner - Regular Expressions To DFA
and Construction
Cat
(a(a|b))*
a a |
S F
a b
Internal Nodes
Need to keep track of left (l)and right
(r) NFA and merge them into a single
NFA
Or
Concatination
Kleene Closure
Or Node
l
ε
ε
S F
r ε
ε
Concatenation Node
l r
Kleene Closure
ε
ε ε
S F
ε
Try It
Convert the regular expression to a
NFA
(a|b)*abb
First convert RE to a tree
Then convert tree to NFA
NFA to DFA
Recall that a DFA can be represented
as a transition table
Characters
+ - Digit
S A A B
State A B
B B
Operations on NFA
ε-closure(t) – Set of NFA states
reachable from NFA state t on ε-
transitions alone.
ε-closure(T) – Set of NFA states
reachable from some NFA state t in
set T on ε-transitions alone.
move(T,a) – Set of NFA states to
which there is a transition on input
symbol a from some state t in T
NFA to DFA Algorithm
Initially ε-closure(s) is the only state
in DFA and it is unmarked
While (there is unmarked state T in DFA)
mark T;
for (each input symbol a) {
U = ε-closure(move(T,a));
if (U not in DFA)
add U unmarked to DFA
transition[T,a]=U;
Try it
Take NFA from previous example and
construct DFA
Regular Expression: (a|b)*abb
ε
a
2 3
ε ε ε ε a b b
S 1 6 7 8 9 F
ε ε
4 5
b
ε
Corresponding DFA
b
C
1,2,4,
5,6,7
b
b
a
NewS a B D NewF
b b
S,1,2,4,7 1,2,3,4 1,2,4,5, 1,2,4,5,
6,7,8 6,7,9 6,7,F
a
a a
Start State and Accepting States
The Start State for the DFA is
ε-closure(s)
ε ε
2 3
ε ε ε ε b ε ε
S 1 6 7 8 9 F
ε ε
4 5
a ε
ε
Solution to Practice
NFA to DFA a
A
2
a
NewS
S,1,3
b B
4
b
Summary of Scanners
Lexemes
Tokens
Regular Expressions, Extended RE
Regular Definitions
Finite Automata (DFA & NFA)
Conversion from RE->NFA->DFA
JLex