0% found this document useful (0 votes)
3 views

Lecture 07

Uploaded by

hamza ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 07

Uploaded by

hamza ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Compiler

Construction
Sohail Aslam
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
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
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
RE → Finite Automata
 Can we build a finite
automaton for every regular
expression?
 Yes, – build FA inductively
based on the definition of
Regular Expression
9
NFA
Nondeterministic Finite
Automaton (NFA)
 Can have multiple
transitions for one input
in a given state
 Can have  - moves
10
Epsilon Moves
 ε – moves
machine can move from state
A to state B without consuming
input

A B

11
NFA
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
12
Execution of FA
A NFA can choose
 Whether to make -moves.
 Which of multiple transitions
to take for a single input.

13
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
14
DFA and NFA
Deterministic Finite Automata
(DFA)
 One transition per input per
state.
 No  - moves

15
Execution of FA
A DFA
 can take only one path
through the state graph.
 Completely determined by
input.
16
NFA vs DFA
 NFAs and DFAs recognize
the same set of languages
(regular languages)
 DFAs are easier to
implement – table driven.

17
NFA vs DFA
 For a given language, the
NFA can be simpler than
the DFA.
 DFA can be exponentially
larger than NFA.

18
NFA vs DFA
 NFAs are the key to
automating RE → DFA
construction.

19
RE → NFA Construction
Thompson’s construction
(CACM 1968)
 Build an NFA for each RE
term.
 Combine NFAs with
-moves.
20
RE → NFA Construction
Subset construction
NFA → DFA
 Build the simulation.
 Minimize number of states
in DFA (Hopcroft’s
algorithm)
21
RE → NFA Construction
Key idea:
 NFA pattern for each
symbol and each operator.
 Join them with -moves in
precedence order.

22
RE → NFA Construction
a
s0 s1
NFA for a

a  b
s0 s1 s3 s4

NFA for ab
23
RE → NFA Construction
a
NFA for a s0 s1

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

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

a b
s0 s1 s3 s4

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

a  b
s0 s1 s3 s4

NFA for ab
27
RE → NFA Construction
a
 s1 s2 
s0 s5
 b
s3 s4 

NFA for a | b
28
RE → NFA Construction
a
s1 s2

NFA for a
29
RE → NFA Construction
a
s1 s2

b
s3 s4

NFA for a and b


30
RE → NFA Construction
a
 s1 s2 
s0 s5
 b
s3 s4 

NFA for a | b
31
RE → NFA Construction

 a 
s0 s1 s2 s4


NFA for a*
32
RE → NFA Construction

a
s1 s2

NFA for a
33
RE → NFA Construction

 a 
s0 s1 s2 s4


NFA for a*
34
Example RE → NFA
NFA for a ( b|c )* 

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


35
Example RE → NFA
building NFA for a ( b|c )*

a
s0 s1

36
Example RE → NFA
NFA for a, b and c

b
s4 s5
a
s0 s1
c
s6 s7

37
Example RE → NFA
NFA for a and b|c

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

38
Example RE → NFA
NFA for a and ( b|c )*

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


39
Example RE → NFA
NFA for a ( b|c )* 

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


40

You might also like