Title: Compiler Construction Principles
1Compiler Construction Principles Implementation
Techniques
- Dr. Ying JIN
- Associate Professor
- Oct. 2007
2Implementation of DFA
3Implementation of DFA
- Objective (meaning of implementing a DFA)
- Given a DFA which defines rules for a set of
strings - Develop a program, which
- Read a string
- Check whether this string is accepted by the DFA
- If a string is accepted by a DFA,
- Next state
- Stop in the final state
- If a string is not accepted by a DAF,
- No next state (?)
- Not stop in the final state
4Implementation of DFA
- Two ways
- Basing on transforming table of DFA
- Basing on graphical representation of DFA
5Transforming Table based Implementation
- Main idea
- Input a string
- Output true if acceptable, otherwise false
- Data structure
- Transforming table (two dimensional array T)
- Two variables
- CurrentState record current state
- CurrentChar record current character that is
read in the string
6Transforming Table based Implementation
- Main idea
- General Algorithm
- 1.CurrentState S0
- 2. read the first character as
CurrentChar - 3. if CurrentChar is not the end of the
string, - if T(CurrentState,CurrentChar)?
error, - CurrentState
T(CurrentState,CurrentChar), - read next character of
the string as CurrentChar, - goto 3
- 4. if CurrentChar is the end of the
string and CurrentState is one of the - terminal states, return true
otherwise, return false.
7Example
a b c d
S0 S1 ? S2 S
S1 ? S1 ? S2
S2 S ? ? ?
S ? ? S ?
1) abbacc true
2) cab
false
8Program structure for Table-based Implementation
Transforming table for the DFA
Variables CurrentChar, CurrentState
Read the string that want to be checked
Checking process
9Graph based Implementation of DFA
- each state corresponds to a case statement
- each edge corresponds to a goto statement
- for accept state, add one more branch, if current
char is the end of the string then accept
Li case CurrentChar of a
goto Lj b goto Lk
other Error( )
Li case CurrentChar of a
goto Lj b goto Lk
return true
other Error( )
i
10LS0 read character to CurrentChar case
CurrentChar of a goto LS1
c goto LS2 d goto LS3
other return false
LS1 read character to CurrentChar case
CurrentChar of b goto LS1 d
goto LS2 other return false
11LS2 read character to CurrentChar case
CurrentChar of a goto LS3
other return false
LS3 read character to CurrentChar
case CurrentChar of c goto LS2
return true other return
false
12Definition of NFA
13Formal Definition
- (?,SS, SS0, ?, TS)
- ?(alphabet),set of allowed characters, each
character can be called as input symbol - SS S0, S1, S2, ,a finite set, each element
is called state - S0? SS, set of start states
- ? SS ? ? ? power set of SS ? ? , transforming
function - TS?SS, set of terminal (accept) states
- Note? is a function which accepts a state and a
symbol and returns a set of states or ?(no
definition)
14Differences between DFA NFA
DFA NFA
Start state One start state Set of start states
? ? ?
T (S, a) S or ? S1, , Sn or ?
implementation easy Non-deterministic
15Example of NFA
- a, b, c, d
- SS S0, S10, S2, S
- Set of Start state S0 , S10
- Set of terminal states S
- (S0,a)? S10, S,(S0,?)? S2,
- (S10,b)?S10, (S10, ?)?S2,
- (S2, ?)?S,
- (S, c)?S
S10
S2
16From NFA to DFA
17Main Idea
- Solve two problems
- ? edge
- ?-closure (SS)
??? - Merging those edges with the same symbol
- NextStates(SS, a)
- Conversion of NFA to DFA
- Using a set of states in NFA as one state in DFA
- Assuring accepting the same set of strings
18The process to calculate ?-closure (???)
- For a given NFA A, and a set of states SS,
- ?-closure(SS) SS
- If there exists a state s in SS, which has a
?-edge referring to a state s and
s??-closure(SS), add to s to ?-closure(SS) - Repeat until there is no state having ?-edge to
states that is not in ?-closure(SS)
19?-closure (???) -- Example
?-closure(S0, S10) ? S0, S10 ? S0,
S10, S2 ? S0, S10, S2 ? S0, S10, S2,S
S10
S2
20Moving States
- For a given set of states SS and a symbol a in a
NFA A, - NextStates(SS, a) s if there is a state
s1?SS, and a - edge s1 s in A
a
21Moving States
NextStates(S0, S10, a) S10, S
S10
S2
NextStates(S0, S10, b) S2
22Algorithm
- Given a NFA A ?, SS, SS0, ?, TS
- Generating an equivalent DFA A ?, SS,S0, ?,
TS - Steps
- (1) S0 ?-closure(SS0), add S0 to SS
- (2) select one state s from SS, for any symbol
a??, - let s NextStates(?-closure(s), a),
- add (s, a) ? s to ?,
- if s?SS, add s to SS
- (3) repeat (2) until all states are handled
- (4) for a state s in SS, s S1, .., Sn, if
there exists Si?TS, then s is an accept state in
A, add s to TS
23Example
S0 ?-closure(S0, S10) S0, S10,
S2,S ,
S10
a b c
S0, S10, S2,S S10, S,S2 S10, S,S2 S
S10, S,S2 S10, S,S2 S
S S
S2
24Minimizing DFA
25Problem
- Equivalent of two DFAs
- If the set of strings accepted by two DFAs are
the same - Among those DFAs that accept the same set of
strings, the minimal DFA refers to the one that
has minimal number of states
How this happens?
26Equivalent DFAs
S1
S1
S2
ad, bd
There are states that accepting the same set of
strings!
27Main Idea
- Equivalent states(????)
- For two states s1 and s2 in a DFA, if treat s1
and s2 as start states and they accept the same
set of strings, s1 and s2 will be called
equivalent states - Two ways to minimizing DFA
- Merging equivalent states (????)
- Splitting non-equivalent states(????)
28Algorithm
- Given a DFA A ?, SS, S0, ?, TS
- Generating an equivalent DFA A ?, SS,S0,
?, TS - Splitting Steps
- (1) two groups non-terminal states, terminal
states - (2) select one group of states SSi Si1,,
Sin, - replace SSi with split(SSi)
- (3) repeat (2) until all groups are handled
- (4) SS set of groups
- (5) S0 is the group consisting of S0
- (6) if the group consisting of terminal states of
A, it is terminal state of A - (7) ? SSi SSj , if there is Si
Sj in A, Si?SSi, Sj?SSj
a
a
29Splitting a Set of States
- Given
- a NFA A ?, SS, S0, ?, TS
- Groups of states SS1, , SSm, SS1? ?SSm SS
- SSi Si1,, Sin,
- split(SSi) is to split SSi into two group G1 and
G2, - For j 1 to n
- for any a??,
- If (Si1,a ) ? Sk ? (Sij, a) ?Sl ? Sk and Sl
belong to the same group SSp , add Sij to G1 - Otherwise, add Sij to G2
30Simple Example
S0, S1, S2, S3, S4
S1
S0 , S1, S2, S3, S4
S2
31Assignment
- Define a DFA for accepting a set of binary
numbers, each binary number can be divided by 4 - (????DFA,???????????4???????)
- Implementation of the DFA above
32Assignment
- From NFA to DFA, and minimize it
?
S1
S2