Compiler Construction - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Compiler Construction

Description:

Compiler Construction Sohail Aslam Lecture 7 – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 41
Provided by: white
Category:

less

Transcript and Presenter's Notes

Title: Compiler Construction


1
Compiler Construction
  • Sohail Aslam
  • Lecture 7

2
Table Encoding of FA
b
  • Transition table

a
a
0
1
2
a b
0 1 err
1 2 1
2 err err
3
Simulating FA
  • trans_tableNSTATESNCHARS accept_statesNSTATE
    S
  • state INITIAL
  • while(state ! err)
  • c input.read()
  • if(c EOF ) break
  • statetrans_tablestatec
  • return accept_statesstate

4
Simulating FA
  • trans_tableNSTATESNCHARS accept_statesNSTATE
    S
  • state INITIAL
  • while(state ! err)
  • c input.read()
  • if(c EOF ) break
  • statetrans_tablestatec
  • return accept_statesstate

5
Simulating FA
  • trans_tableNSTATESNCHARS accept_statesNSTATE
    S
  • state INITIAL
  • while(state ! err)
  • c input.read()
  • if(c EOF ) break
  • statetrans_tablestatec
  • return accept_statesstate

6
Simulating FA
  • trans_tableNSTATESNCHARS accept_statesNSTATE
    S
  • state INITIAL
  • while(state ! err)
  • c input.read()
  • if(c EOF ) break
  • statetrans_tablestatec
  • return accept_statesstate

7
Simulating FA
  • trans_tableNSTATESNCHARS accept_statesNSTATE
    S
  • state INITIAL
  • while(state ! err)
  • c input.read()
  • if(c EOF ) break
  • statetrans_tablestatec
  • return accept_statesstate

8
Simulating FA
  • trans_tableNSTATESNCHARS accept_statesNSTATE
    S
  • state INITIAL
  • while(state ! err)
  • c input.read()
  • if(c EOF ) break
  • statetrans_tablestatec
  • return accept_statesstate

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
  • e movesmachine 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
1
0
A
B
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
1
0
A
B
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
  • Thompsons 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 (Hopcrofts
    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
e
a
b
s0
s1
s3
s4
NFA for ab
24
RE ? NFA Construction
a
s0
s1
NFA for a
25
RE ? NFA Construction
a
s0
s1
NFA for a
b
s3
s4
NFA for b
26
RE ? NFA Construction
a
s0
s1
NFA for a
b
s3
s4
NFA for b
a
b
s0
s1
s3
s4
27
RE ? NFA Construction
a
s0
s1
NFA for a
b
s3
s4
NFA for b
e
a
b
s0
s1
s3
s4
NFA for ab
28
RE ? NFA Construction
a
s1
s2
e
e
s0
s5
b
e
s3
s4
e
NFA for a b
29
RE ? NFA Construction
a
s1
s2
NFA for a
30
RE ? NFA Construction
a
s1
s2
b
s3
s4
NFA for a and b
31
RE ? NFA Construction
a
s1
s2
e
e
s0
s5
b
e
s3
s4
e
NFA for a b
32
RE ? NFA Construction
e
a
e
e
s1
s0
s4
s2
e
NFA for a
33
RE ? NFA Construction
a
s1
s2
NFA for a
34
RE ? NFA Construction
e
a
e
e
s1
s0
s4
s2
e
NFA for a
35
Example RE ? NFA
NFA for a ( bc )
e
b
s4
s5
e
e
e
e
a
e
s8
s0
s1
s2
s3
s9
c
e
s6
s7
e
e
36
Example RE ? NFA
building NFA for a ( bc )
a
s0
s1
37
Example RE ? NFA
NFA for a, b and c
b
s4
s5
a
s0
s1
c
s6
s7
38
Example RE ? NFA
NFA for a and bc
b
s4
s5
e
e
a
s8
s0
s1
s3
c
e
s6
s7
e
39
Example RE ? NFA
NFA for a and ( bc )
e
b
s4
s5
e
e
e
a
e
s8
s0
s1
s2
s3
s9
c
e
s6
s7
e
e
40
Example RE ? NFA
NFA for a ( bc )
e
b
s4
s5
e
e
e
e
a
e
s8
s0
s1
s2
s3
s9
c
e
s6
s7
e
e
Write a Comment
User Comments (0)
About PowerShow.com