You are on page 1of 40

Compiler Construction

LECTURE 7
Table Encoding of FA
 Transition b
table a a
0 1 2

a b
0 1 err
1 2 1
2 err err
2
Simulating FA 3

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];
Simulating FA 4

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];
Simulating FA 5

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];
Simulating FA 6

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];
Simulating FA 7

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];
Simulating FA 8

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];
RE → Finite Automata 9

Can we build a finite


automaton for every regular
expression?
Yes, – build FA inductively
based on the definition of
Regular Expression
NFA 10

Nondeterministic Finite
Automaton (NFA)
Can have multiple transitions
for one input
in a given state
Can have  - moves
Epsilon Moves 11
 ε – moves
machine can move from state A to state B without consuming input


A B
NFA 12
operation of the automaton is not
completely defined by input
1
0 1
A B C

On input “11”, automaton could be


in either state
Execution of FA 13

A NFA can choose


Whether to make -moves.
Which of multiple transitions
to take for a single input.
Acceptance of NFA 14
 NFA can get into multiple states
 Rule: NFA accepts if it can get in a final state

1
0 1
A B C

0
DFA and NFA 15

Deterministic Finite Automata


(DFA)
One transition per input per
state.
No  - moves
Execution of FA 16

A DFA
can take only one path through
the state graph.
Completely determined by input.
NFA vs DFA 17

NFAs and DFAs recognize the


same set of languages (regular
languages)
DFAs are easier to implement
– table driven.
NFA vs DFA 18

For a given language, the NFA


can be simpler than the DFA.
DFA can be exponentially
larger than NFA.
NFA vs DFA 19

NFAsare the key to automating


RE → DFA construction.
RE → NFA Construction 20

Thompson’s construction (CACM


1968)
Build an NFA for each RE
term.
Combine NFAs with
-moves.
RE → NFA Construction 21

Subset construction
NFA → DFA
Build the simulation.
Minimize number of states in
DFA (Hopcroft’s algorithm)
RE → NFA Construction 22

Key idea:
 NFA pattern for each symbol
and each operator.
 Join them with -moves in
precedence order.
RE → NFA Construction 23

a
s0 s1
NFA for a

a  b
s0 s1 s3 s4

NFA for ab
RE → NFA Construction 24
a
NFA for a s0 s1
RE → NFA Construction 25
a
NFA for a s0 s1
b
NFA for b s3 s4
RE → NFA Construction 26
a
NFA for a s0 s1
b
NFA for b s3 s4

a b
s0 s1 s3 s4
RE → NFA Construction 27
a
NFA for a s0 s1
b
NFA for b s3 s4

a  b
s0 s1 s3 s4

NFA for ab
RE → NFA Construction 28

a
 s1 s2 
s0 s5
 b
s3 s4 

NFA for a | b
RE → NFA Construction 29

a
s1 s2

NFA for a
RE → NFA Construction 30

a
s1 s2

b
s3 s4

NFA for a and b


RE → NFA Construction 31

a
 s1 s2 
s0 s5
 b
s3 s4 

NFA for a | b
RE → NFA Construction 32

 s1
a 
s0 s2 s4


NFA for a*
RE → NFA Construction 33

a
s1 s2

NFA for a
RE → NFA Construction 34

 s1
a 
s0 s2 s4


NFA for a*
Example RE → NFA 35

NFA for a ( b|c )* 

b
   s4 s5 
a 
s0 s1 s2 s3 s8 s9
 s c
6 s7 


Example RE → NFA 36

building NFA for a ( b|c )*

a
s0 s1
Example RE → NFA 37

NFA for a, b and c

b
s4 s5
a
s0 s1
c
s6 s7
Example RE → NFA 38

NFA for a and b|c

b
 s4 s5 
a
s0 s1 s3 s8
 s c
6 s7 
Example RE → NFA 39

NFA for a and ( b|c )*



b
  s4 s5 
a 
s0 s1 s2 s3 s8 s9
 s c
6 s7 


Example RE → NFA 40

NFA for a ( b|c )* 

b
   s4 s5 
a 
s0 s1 s2 s3 s8 s9
 s c
6 s7 

You might also like