Lecture 07
Lecture 07
Transition b
table a a
0 1 2
a b
0 1 err
1 2 1
2 err err
1
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];
2
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];
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
RE → Finite Automata
Can we build a finite
automaton for every regular
expression?
Yes, – build FA inductively
based on the definition of
Regular Expression
8
NFA
Nondeterministic Finite
Automaton (NFA)
Can have multiple
transitions for one input
in a given state
Can have - moves
9
Epsilon Moves
ε – moves
machine can move from state
A to state B without consuming
input
A B
10
NFA
operation of the automaton is not
completely defined by input
1
0 1
A B C
12
Acceptance of NFA
NFA can get into multiple states
Rule: NFA accepts if it can get
in a final state
1
0 1
A B C
0
13
DFA and NFA
Deterministic Finite Automata
(DFA)
One transition per input per
state.
No - moves
14
Execution of FA
A DFA
can take only one path
through the state graph.
Completely determined by
input.
15
NFA vs DFA
NFAs and DFAs recognize
the same set of languages
(regular languages)
DFAs are easier to
implement – table driven.
16
NFA vs DFA
For a given language, the
NFA can be simpler than
the DFA.
DFA can be exponentially
larger than NFA.
17
NFA vs DFA
NFAs are the key to
automating RE → DFA
construction.
18
RE → NFA Construction
Thompson’s construction
(CACM 1968)
Build an NFA for each RE
term.
Combine NFAs with
-moves.
19
RE → NFA Construction
Subset construction
NFA → DFA
Build the simulation.
Minimize number of states
in DFA (Hopcroft’s
algorithm)
20
RE → NFA Construction
Key idea:
NFA pattern for each
symbol and each operator.
Join them with -moves in
precedence order.
21
RE → NFA Construction
a
s0 s1
NFA for a
a b
s0 s1 s3 s4
NFA for ab
22
RE → NFA Construction
a
NFA for a s0 s1
23
RE → NFA Construction
a
NFA for a s0 s1
b
NFA for b s3 s4
24
RE → NFA Construction
a
NFA for a s0 s1
b
NFA for b s3 s4
a b
s0 s1 s3 s4
25
RE → NFA Construction
a
NFA for a s0 s1
b
NFA for b s3 s4
a b
s0 s1 s3 s4
NFA for ab
26
RE → NFA Construction
a
s1 s2
s0 s5
b
s3 s4
NFA for a | b
27
RE → NFA Construction
a
s1 s2
NFA for a
28
RE → NFA Construction
a
s1 s2
b
s3 s4
NFA for a | b
30
RE → NFA Construction
a
s0 s1 s2 s4
NFA for a*
31
RE → NFA Construction
a
s1 s2
NFA for a
32
RE → NFA Construction
a
s0 s1 s2 s4
NFA for a*
33
Example RE → NFA
NFA for a ( b|c )*
b
s4 s5
a
s0 s1 s2 s3 s8 s9
s c
6 s7
34
Example RE → NFA
building NFA for a ( b|c )*
a
s0 s1
35
Example RE → NFA
NFA for a, b and c
b
s4 s5
a
s0 s1
c
s6 s7
36
Example RE → NFA
NFA for a and b|c
b
s4 s5
a
s0 s1 s3 s8
s c
6 s7
37
Example RE → NFA
NFA for a and ( b|c )*
b
s4 s5
a
s0 s1 s2 s3 s8 s9
s c
6 s7
38
Example RE → NFA
NFA for a ( b|c )*
b
s4 s5
a
s0 s1 s2 s3 s8 s9
s c
6 s7
39