You are on page 1of 56

Compiler

Construction
Lecture 7
DFA Minimization
 The generated DFA may
have a large number of
states.
 Hopcroft’s algorithm:
minimizes DFA states
2
DFA Minimization

3
DFA Minimization

4
Equivalent

5
Note:
 The above slides was the
theoretical representation of DFA
Minimization .

6
Example: DFA Minimization

7
Transition table

8
Equivalence

9
Final Minimal DFA

10
Example# 2 DFA
a a
a b b
A B D E
a
b a
b
C

b
DFA for (a | b )*abb
11
DFA Minimization
b a a
a b b
A,C B D E
a
b

Minimized DFA for (a | b )*abb

12
Optimized Acceptor
RE R RE=>NFA

NFA=>DFA

Min. DFA

input w Simulate yes, if w  L(R)


string DFA no, if w  L(R)

13
Derivation
 Derivation is used to find whether
the string belongs to a given
grammar.
 The process by which a grammar
generating string.

14
Derivation
 The derivation view gives the
precise description of top down
construction of parse tree. Or
 The sequence of replacement is
called derivation

15
 Process or discovering a
derivations is called parsing

16
Types of Derivation
  Leftmost derivation.
 Rightmost derivation.

17
Derivations
 Leftmost derivation: replace leftmost
non-terminal (NT) at each step.
 In leftmost derivation, at each and
every step the leftmost non-terminal is
expanded by substituting its
corresponding production to derive a
string.
 Rightmost derivation: replace
rightmost NT at each step
18
Example:

19
Derivations
 Rightmost derivation: replace
rightmost NT at each step.
 In rightmost derivation, at each
and every step the rightmost non-
terminal is expanded by
substituting its corresponding
production to derive a string.
20
Example:

21
Note:
 Both leftmost and rightmost
derivations give the same
expression or result

22
Grammar
1.EI
2.EE+E
3.EE*E
4.E(E)
5.I L
6.I ID
7.I IL
8.D  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
9.L  a | b | c | … A | B | … Z
Left most Derivation
• Expand start symbol first and work way
down in such a way that it matches the input
string
 For example, given a*(a+b1) we can
derive this by:
• E  E*E  I*E  L*E  a*E  a*(E)
 a*(E+E)  a*(I+E)  a*(L+E) 
a*(a+E)  a*(a+I)  a*(a+ID) 
a*(a+LD)  a*(a+bD)  a*(a+b1)
Rightmost Derivation
Example
 a*(a+b1) was already shown previously using
a leftmost derivation.
 We can also come up with a rightmost
derivation, but we must make replacements in
different order:
• E rm E*E rm E * (E) rm E*(E+E) rm E*(E+I)
rm E*(E+ID) rm E*(E+I1) rm E*(E+L1) rm
E*(E+b1) rm E*(I+b1) rm E*(L+b1) rm
E*(a+b1) rm I*(a+b1) rm L*(a+b1) rm a*(a+b1)
26
27
Ambiguous Grammars

28
Example:

29
Example:

30
31
32
33
34
Note:
 Ambiguity is bad for
programming languages

35
Ambiguity generated due
 Associativity problem
 Precedence problem

36
Associativity problem

37
Associativity Problem
 It can be solved with the help
of recursion
 Left recursion
 Right recursion

38
Note:
 We will make the grammar
recursive grammar

39
Left recursive
 SS+S
 Sa
 We will make it left recursive
 Sa+S
 Sa

40
Right recursive
 SS+S
 Sa
 We will make it right recursive
 SS+a
 Sa

41
Note:
 We can solved the
associativity problem with left
recursion or right recursion .

42
Right Recursive

43
Note:
 Only one tree can be made for
the above right recursive
grammar.
 And this is called right
recursive parse tree

44
Precedence problem

45
Removing precedence problem

46
Removing precedence problem

47
Parser

48
Left Recursion
 A grammar that contain a non-
terminal on the L.H.S and the
same non-terminal on the R.H.S
of the production then the
grammar is called left recursive
(or)
 If the leftmost symbol in one of its
production itself.
49
Example:
 General form
 AA/β
or
exp  exp + term
Here “exp” on the R.H.S is non
terminal and the same non terminal
“exp” appears on the L.H.S .
50
 Here if the R.H.S begin with “exp”
so the procedure for “exp” is called
recursively and the parser loop for
ever .
 Only the left hand non terminal will
be expanded. So dead lock occur.
 Infinity loop
51
 Our expression grammar is left
recursive.
 This can lead to non-
termination in a top-down
parser
 Non-termination is bad in any
part of a compiler!
52
Loop for ever

53
Elimination of left recursion
 We can eliminate left recursion
with help of below formula

54
55
56

You might also like