Phases of Compiler PDF
Phases of Compiler PDF
Phases of Compiler
Language definition.
• Ʃ= alphabet= set of symbols
• String= sequence of symbols
• Empty string=epsilon= e, ε, λ or even Λ
• Ʃ*= all possible string over alphabet Ʃ
• L= language over Ʃ={strings over Ʃ* satisfying certain property }
• ={x Ɛ Ʃ* : x satisfies certain properties}
• For example if Ʃ={0,1}
• L1={0,1}
• L2={00,01,10,11} etc
• Since Language is a set we may apply set operations on languages
• Concatenation of Languages
• L1oL2=L1L2={xy such that x is in L1 and y is in L2}
• L1={0,01,110}
• L2={10,110}
• L1L2={010,0110,0110,01110, 11010, 110110}
• ={010,0110,01110, 11010, 110110}
Kleen star and Kleen Plus
• 𝐿0 = {ε}
• 𝐿1 =L
• 𝐿2 = LL
• 𝐿3 =LLL
Q\Ʃ 0 1
q0 q2 q0
*q1 q1 q1
q2 q2 q1
Q\Ʃ 0 1
q0 q2 q0
q1 q1
q2 q2 q1
Acceptance of a string by DFA
• A string is said to be accepted If after reading the string DFA reaches
final state starting from initial state using its transition function for a
given state and read input.
• Language of DFA= a set of all strings acceptable by it
• DFA can be constructed for a given language
• Language of DFA can be found if DFA is given.
• DFA is said to accept a language L if all its strings are accepted by DFA
δ ( q0, 100101001)
=δ ( q2, 00101001)
=δ ( q2, 0101001)
=δ ( q2, 101001)
=δ ( q1, 01001)
=δ ( q1, 1001)
=δ ( q1, 001)
=δ ( q1, 01)
=δ ( q1, 1)
q1which belongs to final state
Hence string get accepted
Construct a DFA that accept all string over {0,
1} containing even no. of 0 and even n0. of 1
State transition diagram of DFA
• Construct a DFA that accept all string over {a, b} that contain even no. of a and
odd n0. of b
• Construct a DFA that accept all string over {a, b} that does not contain even no.
of a and odd n0. of b
Design a DFA that accept
all strings over {0,1,2,3} whose
Sum of digits
is divisible by 4
Design a DFA that accept all strings over {a,b}that contain exactly 2 a’s
and odd no. of b’s Design
q0: No a , even no of b
q1: No a , odd no of b
q2: one a , even no of b
q3: One a , odd no of b
q4: two a , even no of b
q5: two a , odd no of b
q6: More than two a’s
DFA
• Design a DFA that accepts all string over {0,1} that is divisible by 3
Design a DFA that accepts strings over {a,b} that contain substring aaa
q0: No progress towards aaa
q1: last processed symbol was a
q2: last two processed symbol were aa
q3: aaa has been found in the string
DFA
• Construct a DFA that accept all strings over {a, b} that contain substring abba
• Design a DFA that accepts all strings over {a,b} ending with abb
• Design a DFA that accepts the language L= {𝑎𝑖 𝑏𝑖 : i ≤ 𝑛}, for a fixed integer n
Simulating DFA
Non deterministic Finite Automaton (NFA)
definition
• NFA=<Q, Ʃ , δ , s, F> Where
Q= set of states
Ʃ=alphabet
A transition function δ: Q × ΣU{ } → 2Q mapping the current
state q ∈ Q and input symbol a ∈ Σ to a new state δ(q, a) ∈ 2Q
s ∈ Q is initial state
F= set of final state
• A deterministic finite automaton (DFA) is a special case of an NFA where:
There are no moves on input , and For each state s and input symbol a, there
is exactly one edge out of s labelled a
Non deterministic Finite Automaton (NFA)
Example
• NFA=<Q, Ʃ , δ , s, F> Where
Q= set of states={q0,q1,q2}
Ʃ=alphabet={0,1}
A transition function δ:
δ(q0,0)= {q0,q1}, δ(q0,1)= {q0}, δ(q1,0)=Ø
δ(q1,1)={q2}, δ(q2,0)= Ø, δ(q2,1)= Ø
s = initial state=q0
F= set of final state={ q2}
• A deterministic finite automaton (DFA) is a special case of an NFA where: There are no moves on input e, and
• For each state s and input symbol a, there is exactly one edge out of s labelled a
State transition table & State transition
diagram
Q\Ʃ 0 1
q1 Ø {q2}
*q2 Ø Ø
Acceptance by NFA
• An NFA accepts input string x if and only if there is some path in the
transition graph of NFA from the start state to one of the accepting
states, such that the symbols along the path spell out x
• A string x is said to be accepted if NFA can find a path to reach any
one of the final state reading the input x from initial state.
• The language defined (or accepted) by an NFA is the set of strings
labeling some path from the start to an accepting state.
• What does this NFA does ?