40
Compiler Compiler Construction Construction Sohail Aslam Lecture 7

Compiler Construction Sohail Aslam Lecture 7. 2 Table Encoding of FA Transition table a b a ab 01err 121 2 012

Embed Size (px)

Citation preview

Compiler Compiler ConstructionConstruction

Compiler Compiler ConstructionConstruction

Sohail Aslam

Lecture 7

2

Table Encoding of FATable Encoding of FATable Encoding of FATable Encoding of FA Transition

table a

b

a

a b

0 1 err

1 2 1

2 err err

0 1 2

3

Simulating FASimulating FASimulating FASimulating FAtrans_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 FASimulating FASimulating FASimulating FAtrans_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 FASimulating FASimulating FASimulating FAtrans_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 FASimulating FASimulating FASimulating FAtrans_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 FASimulating FASimulating FASimulating FAtrans_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 FASimulating FASimulating FASimulating FAtrans_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 RE → → Finite AutomataFinite AutomataRE RE → → Finite AutomataFinite Automata Can we build a finite

automaton for every regular expression?

Yes, – build FA inductively based on the definition of Regular Expression

10

NFANFANFANFANondeterministic Finite

Automaton (NFA) Can have multiple

transitions for one input in a given state

Can have - moves

11

Epsilon MovesEpsilon MovesEpsilon MovesEpsilon Moves ε – moves

machine can move from state A to state B without consuming input

A B

12

NFANFANFANFAoperation of the automaton is not completely defined by input

1

1

0

On input “11”, automaton could be in either state

A B C

13

Execution of FAExecution of FAExecution of FAExecution of FAA NFA can choose Whether to make -moves. Which of multiple transitions

to take for a single input.

14

Acceptance of NFAAcceptance of NFAAcceptance of NFAAcceptance of NFA NFA can get into multiple states Rule: NFA accepts if it can get

in a final state

11

0A B C

0

15

DFA and NFADFA and NFADFA and NFADFA and NFADeterministic Finite Automata

(DFA) One transition per input per

state. No - moves

16

Execution of FAExecution of FAExecution of FAExecution of FA

A DFA can take only one path

through the state graph. Completely determined by

input.

17

NFA vs DFANFA vs DFANFA vs DFANFA vs DFA NFAs and DFAs recognize

the same set of languages (regular languages)

DFAs are easier to implement – table driven.

18

NFA vs DFANFA vs DFANFA vs DFANFA vs DFA For a given language, the

NFA can be simpler than the DFA.

DFA can be exponentially larger than NFA.

19

NFA vs DFANFA vs DFANFA vs DFANFA vs DFA

NFAs are the key to automating RE → DFA construction.

20

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

Thompson’s construction (CACM 1968)

Build an NFA for each RE term.

Combine NFAs with -moves.

21

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

Subset construction NFA → DFA

Build the simulation. Minimize number of states

in DFA (Hopcroft’s algorithm)

22

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA ConstructionKey idea: NFA pattern for each

symbol and each operator. Join them with -moves in

precedence order.

23

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s0

as1

NFA for a

s0a

s1

NFA for ab

s3b s4

24

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Constructions0

as1

NFA for a

25

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Constructions0

as1

NFA for a

s3

bs4

NFA for b

26

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Constructions0

as1

NFA for a

s0a

s1 s3b s4

s3

bs4

NFA for b

27

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s0

as1

NFA for a

s0a

s1

NFA for ab

s3b s4

s3

bs4

NFA for b

28

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s0 s5

s1a

s2

NFA for a | b

s3b s4

29

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s1a

s2

NFA for a

30

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s1a

s2

s3b s4

NFA for a and b

31

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s0 s5

s1a

s2

NFA for a | b

s3b s4

32

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s0 s4s1a

s2

NFA for a*

33

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s1a

s2

NFA for a

34

RE RE →→ NFA Construction NFA ConstructionRE RE →→ NFA Construction NFA Construction

s0 s4s1a

s2

NFA for a*

35

Example RE Example RE →→ NFA NFA Example RE Example RE →→ NFA NFA

NFA for a ( b|c )*

s3 s9

s4 s5

s6 s7

s8s0 s1 s2

a

b

c

36

Example RE Example RE →→ NFA NFA Example RE Example RE →→ NFA NFA

building NFA for a ( b|c )*

s0 s1

a

37

Example RE Example RE →→ NFA NFA Example RE Example RE →→ NFA NFA

NFA for a, b and c

s4 s5

s6 s7

s0 s1

a

b

c

38

Example RE Example RE →→ NFA NFA Example RE Example RE →→ NFA NFA

NFA for a and b|c

s3

s4 s5

s6 s7

s8s0 s1

a

b

c

39

Example RE Example RE →→ NFA NFA Example RE Example RE →→ NFA NFA

NFA for a and ( b|c )*

s3 s9

s4 s5

s6 s7

s8s0 s1 s2

a

b

c

40

Example RE Example RE →→ NFA NFA Example RE Example RE →→ NFA NFA

NFA for a ( b|c )*

s3 s9

s4 s5

s6 s7

s8s0 s1 s2

a

b

c