Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction

Similar presentations


Presentation on theme: "Compiler Construction"— Presentation transcript:

1 Compiler Construction
Sohail Aslam Lecture 7 compiler: intro

2 Table Encoding of FA b Transition table a a 1 2 a b 1 err 2

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

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 ε – moves machine 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
1 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 A B C

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 Thompson’s 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 (Hopcroft’s 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 a e b s0 s1 s3 s4 NFA for ab

24 RE → NFA Construction a NFA for a s0 s1

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

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

27 RE → NFA Construction a NFA for a s0 s1 b NFA for b s3 s4 a e b s0 s1
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 e a e s0 s1 s2 s4 e NFA for a*

33 RE → NFA Construction a s1 s2 NFA for a

34 RE → NFA Construction e e a e s0 s1 s2 s4 e NFA for a*

35 Example RE → NFA s4 s5 s0 s1 s2 s3 s8 s9 s6 s7 NFA for a ( b|c )* e e

36 Example RE → NFA building NFA for a ( b|c )* 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 s4 s5 s0 s1 s3 s8 s6 s7 NFA for a and b|c e e e e b a

39 Example RE → NFA s4 s5 s0 s1 s2 s3 s8 s9 s6 s7 NFA for a and ( b|c )*

40 Example RE → NFA s4 s5 s0 s1 s2 s3 s8 s9 s6 s7 NFA for a ( b|c )* e e


Download ppt "Compiler Construction"

Similar presentations


Ads by Google