Professional Documents
Culture Documents
Class 07 A
Class 07 A
de
Lexical analysis II
1. From NFA to DFA
2. How lex works
3. From RE to DFA
Subset construction –
NFA to DFA
• Algorithm 7.1 NFA2DFA (=3.20)
• Input: NFA N(r)
• Output: DFA D(r) w/ L(D(r))=L(N(r)),
defined by states Ds, transistions Dt;
each state in Ds represents a set of states of N(r)
• Help functions:
– ε-closure(T)closure(T) = set of NFA states reachable by є-transitions alone transitions alone
from some state s (including T)
– move(T,a) = set of NFA states to which there is a transition on input
symbol a from a state sєT
– Let s0 be the start state of N(r)
Example:
(a|b)*abb as DFA
a b b
0 1 2 3
a a
a
aaabaabba?
Example:
(a|b)*abb as NFA
a b b
0 1 2 3
aaabaabb?
Efficient implementation
• 2 stacks, each holding a set of NFA states:
– oldStates – current set of states (S on rhs of RunNFA:3.1)
– newStates – next set of states (S on lhs of RunNFA:3.1)
– newStates transferred to oldStates during looping
• boolean array alreadyOn, indexed by NFA states, indicates the states of
newStates – much faster than searching through the stack
• 2D-transitions alone array ε-closure(s0) is unmarkedmove[s,a] holds transition table of NFA (entries represented by linked
lists)
• Implement RunNFA:1, RunNFA:3.1 by function addState(s):
1. ε-closure(s0) is unmarkedpush ε-closure(s0) is unmarkeds ε-closure(s0) is unmarkedonto ε-closure(s0) is unmarkednewStates
2. ε-closure(s0) is unmarkedalreadyOn[s] ε-closure(s0) is unmarked= ε-closure(s0) is unmarkedTRUE
3. ε-closure(s0) is unmarkedfor ε-closure(s0) is unmarkedt ε-closure(s0) is unmarkedin ε-closure(s0) is unmarkedmove[s,ε]:
1. ε-closure(s0) is unmarkedif ε-closure(s0) is unmarkedNOT ε-closure(s0) is unmarkedalreadyOn[t]:
1. ε-closure(s0) is unmarkedaddState(t)
TECHNISCHE HOCHSCHULE DEGGENDORF
9