Compiler Construction Lecture 3 Dr. Naveed Ejaz

advertisement
Compiler
Construction
Lecture 3
Dr. Naveed Ejaz
Dept of Computer Science, College of
Science in Zulfi, Majmaah University, KSA
Lexical Analysis
Part 2
Table Encoding of FA
 Transition
table
a
b
a
0
0
1
2
1
a
1
2
err
2
b
err
1
err
3
Simulating FA
trans_table[NSTATES][NCHARS];
accept_states[NSTATES];
state = INITIAL;
while(state != err){
c = input.read();
if(c == EOF ) break;
state=trans_table[state][c];
}
return accept_states[state];
4
Simulating FA
trans_table[NSTATES][NCHARS];
accept_states[NSTATES];
state = INITIAL;
while(state != err){
c = input.read();
if(c == EOF ) break;
state=trans_table[state][c];
}
return accept_states[state];
5
Simulating FA
trans_table[NSTATES][NCHARS];
accept_states[NSTATES];
state = INITIAL;
while(state != err){
c = input.read();
if(c == EOF ) break;
state=trans_table[state][c];
}
return accept_states[state];
6
Simulating FA
trans_table[NSTATES][NCHARS];
accept_states[NSTATES];
state = INITIAL;
while(state != err){
c = input.read();
if(c == EOF ) break;
state=trans_table[state][c];
}
return accept_states[state];
7
Simulating FA
trans_table[NSTATES][NCHARS];
accept_states[NSTATES];
state = INITIAL;
while(state != err){
c = input.read();
if(c == EOF ) break;
state=trans_table[state][c];
}
return accept_states[state];
8
Simulating FA
trans_table[NSTATES][NCHARS];
accept_states[NSTATES];
state = INITIAL;
while(state != err){
c = input.read();
if(c == EOF ) break;
state=trans_table[state][c];
}
return accept_states[state];
9
RE → Finite Automata
 Can we build a finite
automaton for every regular
expression?
 Yes, – build FA inductively
based on the definition of
Regular Expression
10
NFA
Nondeterministic Finite
Automaton (NFA)
 Can have multiple
transitions for one input
in a given state
 Can have e - moves
11
Epsilon Moves
 ε – moves
machine can move from state
A to state B without consuming
input
e
A
B
12
NFA
operation of the automaton is not
completely defined by input
1
A
0
B
1
C
On input “11”, automaton could be
in either state
13
Execution of FA
A NFA can choose
 Whether to make e-moves.
 Which of multiple
transitions to take for a
single input.
14
Acceptance of NFA
 NFA can get into multiple states
 Rule: NFA accepts if it can get
in a final state
1
A
0
B
1
C
0
15
DFA and NFA
Deterministic Finite Automata
(DFA)
 One transition per input per
state.
 No e - moves
16
Execution of FA
A DFA
 can take only one path
through the state graph.
 Completely determined by
input.
17
NFA vs DFA
 NFAs and DFAs recognize
the same set of languages
(regular languages)
 DFAs are easier to
implement – table driven.
18
NFA vs DFA
 For a given language, the
NFA can be simpler than
the DFA.
 DFA can be exponentially
larger than NFA.
19
NFA vs DFA
 NFAs are the key to
automating RE → DFA
construction.
20
RE → NFA Construction
Thompson’s construction
(CACM 1968)
 Build an NFA for each RE
term.
 Combine NFAs with
e-moves.
21
RE → NFA Construction
Subset construction
NFA → DFA
 Build the simulation.
 Minimize number of states
in DFA (Hopcroft’s
algorithm)
22
RE → NFA Construction
Key idea:
 NFA pattern for each
symbol and each operator.
 Join them with e-moves in
precedence order.
23
RE → NFA Construction
a
s0
s1
NFA for a
s0
a
s1
e
s3
b
s4
NFA for ab
24
RE → NFA Construction
NFA for a
s0
a
s1
25
RE → NFA Construction
NFA for a
NFA for b
s0
s3
a
b
s1
s4
26
RE → NFA Construction
NFA for a
NFA for b
s0
a
s1
s0
s3
a
b
s3
s1
s4
b
s4
27
RE → NFA Construction
NFA for a
s0
NFA for b
s0
a
a
b
s3
s1
e
s3
s1
s4
b
s4
NFA for ab
28
RE → NFA Construction
e
s1
a
s2
e
s0
s5
e
s3
b
s4
e
NFA for a | b
29
RE → NFA Construction
s1
a
s2
NFA for a
30
RE → NFA Construction
s1
a
s3
b
s2
s4
NFA for a and b
31
RE → NFA Construction
e
s1
a
s2
e
s0
s5
e
s3
b
s4
e
NFA for a | b
32
RE → NFA Construction
e
s0
e
s1
a
s2
e
s4
e
NFA for a*
33
RE → NFA Construction
s1
a
s2
NFA for a
34
RE → NFA Construction
e
s0
e
s1
a
s2
e
s4
e
NFA for a*
35
Example RE → NFA
e
NFA for a ( b|c )*
s0
a
e
s1 s2
e
e s4
s3
e s6
b
s5 e
e
s8 s 9
c
s7 e
e
36
Example RE → NFA
building NFA for a ( b|c )*
s0
a
s1
37
Example RE → NFA
NFA for a, b and c
s0
a
s4
b
s5
s6
c
s7
s1
38
Example RE → NFA
NFA for a and b|c
s0
a
e s4
s1
s3
e s6
b
s5 e
s8
c
s7 e
39
Example RE → NFA
NFA for a and ( b|c )*
s0
a
s1 s2
e
e s4
s3
e s6
e
b
s5 e
e
s8 s 9
c
s7 e
e
40
Example RE → NFA
e
NFA for a ( b|c )*
s0
a
e
s1 s2
e
e s4
s3
e s6
b
s5 e
e
s8 s 9
c
s7 e
e
41
Download