Beruflich Dokumente
Kultur Dokumente
1 q0 1 0 q1 1 0 1 q3 0 0 q2
Khrisat 2004
-1-
CS Compiler Design
Regular Expressions
FLEX
C code
Khrisat 2004
-2-
CS Compiler Design
Character Stream
DFA Simulation
Khrisat 2004
-3-
CS Compiler Design
Build the NFA inductively Define rules for each base RE Combine for more complex REs
f
general machine
Khrisat 2004
-4-
CS Compiler Design
Thompson Construction
S F empty string transition
E1
E2
-5-
CS Compiler Design
New start state S -transitions to the start states of E1 and E2 -transitions from the final/accepting states of E1 and E2 to the new final state F
Closure: (E*) F
Khrisat 2004
-6-
CS Compiler Design
C
E x
D y B
C E
A D y S
Khrisat 2004
-7-
H
CS Compiler Design
Class Problem
Develop an NFA for the RE: (\+? | -?) d+
Khrisat 2004
-8-
CS Compiler Design
NFA to DFA
Remove the non-determinism 2 problems
CS Compiler Design
-9-
Solve by subset construction Build new DFA based upon the power set of states on the NFA Move (S,a) is relabeled to target a new state whenever single input goes to multiple states b a b a+b* a
start 1 a 2 start 1 a 1/2 b 2
(1,a) 1 or 2, create new state 1/2 (2,a) ERROR (1/2,a) 1/2 (2,b) 2 (1/2,b) b Any state with 2 in name is a final state
Khrisat 2004
- 10 -
CS Compiler Design
Problem 2: transitions
Any state reachable by an transition is part of the state -closure - Any state reachable from S by transitions is in the -closure; treat -closure as 1 big state, always include -closure as part of the state a b a b a*b*
start 1 2 3 start
1/2/3
2/3
-closure(1) = {1,2,3} -closure(2) = {2,3} create new state 1/2/3 create new state 2/3
Khrisat 2004
start 1 2 5
-closure(1) = {1, 2, 3, 5}
a
3 a b 4
Create a new state A = {1, 2, 3, 5} and examine transitions out of it move(A, a) = {3, 6} Call this a new subset state = B = {3, 6} move(A, b) = {4} move(B, a) = {6}
start A
B b
move(B, b) = {4}
b
Khrisat 2004
4
- 12 -
CS Compiler Design
Class Problem
2 0 1 4 b 5 a 3 6 7
Khrisat 2004
- 13 -
CS Compiler Design
start 1
b
2 c
Khrisat 2004
- 14 -
CS Compiler Design
State Minimization
a b
b 4
start
a
a
3 b
Khrisat 2004
All transitions from states in group G1 go to states in another group G2 Construct minimized DFA such that there is 1 state for each group of states b Basic strategy: identify b distinguishing transitions 2 a b start a 4 5 1 b a 3 a
Khrisat 2004
- 16 -
CS Compiler Design
DFA Simulation
- 17 -
- 18 -
CS Compiler Design
Khrisat 2004
- 19 -
CS Compiler Design