push down automata- theoretical foundations of computations
1. Course – TFC
Course Instructor
Dr. Umadevi V
Department of CSE, BMSCE
Webpage:https://sites.google.com/site/drvumadevi/
1 July 2021 CSE, BMSCE 1
2. Unit-4
Pushdown Automata
Definition of the Pushdown Automaton, The Languages of a PDA,
Equivalence of PDA’s and CFG’s , Deterministic Pushdown
Automata,
The Pumping Lemma for Context Free Languages, Closure
Properties of Context Free Languages
1 July 2021 CSE, BMSCE 2
3. Push Down Automata (PDA)
Pushdown Automata is a finite automata with
extra memory called stack which helps Pushdown
automata to recognize Context Free Languages.
1 July 2021 CSE, BMSCE 3
5. Schematic Representation of PDA
Rough Slide
□ Example: L={an
bn
, n>=1}
□ Valid String “aabb”
1 July 2021 CSE, BMSCE 5
a a b b 𝞮
z0
Input
Stack
6. Schematic Representation of PDA
Rough Slide
□ Example: L={an
bn
, n>=1}
□ Valid String “aabb” Invalid String “aabbb”
1 July 2021 CSE, BMSCE 6
a a b b 𝞮
a
a
z0
Input
Stack
a a b b b 𝞮
z0
Input
Stack
7. Schematic Representation of PDA
□ Example: L={an
bn
, n>=1}
□ Valid String “aabb” Invalid String “aabbb”
1 July 2021 CSE, BMSCE 7
a a b b 𝞮
a
a
z0
Input
Stack
a a b b b 𝞮
a
a
z0
Input
Stack
8. Formal Definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
□ Q is the set of states
□ ∑ is the set of input alphabet
□ Γ is the set of symbols called stack alphabet
□ q0
∈ Q , is the initial state
□ Z0
∈ Γ, is the initial symbol on the stack
□ F is the set of final states
□ δ is a transition function which maps Q x {Σ ∪ ε} x Γ into Q x Γ*. In a given
state, PDA will read input symbol and stack symbol (top of the stack) and
move to a new state and change the symbol of stack.
δ(State, Input Symbol, Stack Symbol)=(Next State, Stack Symbol)
1 July 2021 CSE, BMSCE 8
9. Classification of Push Down Automata
1. Deterministic Push Down Automata (DPDA)
2. Non-Deterministic Push Down Automata (NPDA)
Note: Unlike Finite Automata, DPDA and NPDA are not equivalent. For example,
the language
L={WWR
, W ∈(a+b)*) } is accepted by NPDA but not by any DPDA
1 July 2021 CSE, BMSCE 9
10. Two ways a PDA accept the language
There are two ways a PDA can accept the language:
a. Empty Stack: If a sentence in a language causes PDA to empty its stack
then the sentence is accepted.
b. Final State: If a sentence in a language causes PDA to move to a state
which is member of a set of final states, then the sentence is accepted.
1 July 2021 CSE, BMSCE 10
11. Question
Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty stack
method. Show the moves made by PDA for the string aabb and aaabb
1 July 2021 CSE, BMSCE 11
12. Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty stack method.
Rough Slide
Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty stack
method. Show the moves made by PDA for the string aabb and aaabb
1 July 2021 CSE, BMSCE 12
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
a
q1
z0
a
13. Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty
stack method.
Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty stack
method. Show the moves made by PDA for the string aabb and aaabb
1 July 2021 CSE, BMSCE 13
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
Push a
q0
X X
a Push a
q0
Pop a
q1
X
q1
z0
X X Pop Z0
q1
a
X Pop a
q1
X
14. Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty stack
method.
1 July 2021 CSE, BMSCE 14
q0
a, z0
| az0
q1
Representing PDA as a generalized
Transition Diagram
a, a | aa
b, a | 𝞮
𝞮, Z0
| 𝞮
b, a | 𝞮
15. Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by empty stack
method.
1 July 2021 CSE, BMSCE 15
Formal definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
}
∑ = {a, b}
Γ ={a, Z0
}
q0
= q0
Z0
= Z0
F = { }
δ(q0
, a, z0
) =(q0
, az0
)
δ(q0
, a, a) =(q0
, aa)
δ(q0
, b, a) =(q1
, 𝞮)
δ(q1
, b, a) =(q1
, 𝞮)
δ(q1
, 𝞮, z0
) =(q1
, 𝞮)
Push Operation
Pop Operation
16. Instantaneous Description of a PDA
Rough Slide
□ The current configuration of PDA at any given instant can be described by Instantaneous
Description (ID).
□ An ID gives current state of PDA, the remaining string to be processed and the entire contents of
stack.
Instantaneous Description for the string "aabb"
1 July 2021 CSE, BMSCE 16
(q0
, aabb, z0
)
17. Instantaneous Description of a PDA
□ The current configuration of PDA at any given instant can be described by Instantaneous
Description (ID).
□ An ID gives current state of PDA, the remaining string to be processed and the entire contents of
stack.
Instantaneous Description for the string "aabb"
1 July 2021 CSE, BMSCE 17
(q0
, aabb, z0
) ˫ (q0
, abb, az0
)
˫ (q0
, bb, aaz0
)
˫ (q1
, b, az0
)
˫ (q1
, 𝞮, z0
)
˫ (q1
, 𝞮, 𝞮)
All the Input symbols
are read and Stack is empty,
therefore the string “aabb” is
Accepted.
18. Instantaneous Description of a PDA
Rough Slide
Instantaneous Description for the string "aaabb"
1 July 2021 CSE, BMSCE 18
(q0
, aaabb, z0
)
19. Instantaneous Description of a PDA
Instantaneous Description for the string "aaabb"
1 July 2021 CSE, BMSCE 19
(q0
, aaabb, z0
) ˫ (q0
, aabb, az0
)
˫ (q0
, abb, aaz0
)
˫ (q1
, bb, aaaz0
)
˫ (q1
, b, aaz0
)
˫ (q1
, 𝞮, az0
)
All the Input symbols
are read but the Stack is
not empty, therefore the string
“aaabb” is not accepted.
20. Question
Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty stack
method. Give the graphical representation of the PDA obtained. Show the moves made by
PDA for the string aabbbb and aabbb
1 July 2021 CSE, BMSCE 20
21. Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty stack method.
Rough Slide
1 July 2021 CSE, BMSCE 21
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
a
q1
z0
a
22. Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty
stack method.
1 July 2021 CSE, BMSCE 22
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
Push aa
q0
X X
a Push aa
q0
Pop a
q1
X
q1
z0
X X Pop Z0
q1
a
X Pop a
q1
X
q0
a, z0
| aaz0
q1
Representing PDA as a generalized
Transition Diagram
a, a | aaa
b, a | 𝞮
𝞮, Z0
| 𝞮
b, a | 𝞮
23. Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty stack
method.
1 July 2021 CSE, BMSCE 23
Formal definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
}
∑ = {a, b}
Γ ={a, Z0
}
q0
= q0
Z0
= Z0
F = { }
δ(q0
, a, z0
) =(q0
,aaz0
)
δ(q0
, a, a) =(q0
,aaa)
δ(q0
, b, a) =(q1
, 𝞮)
δ(q1
, b, a) =(q1
, 𝞮)
δ(q1
, 𝞮, z0
) =(q1
, 𝞮)
Push Operation
Pop Operation
24. Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty stack method.
Rough Slide
Instantaneous Description for the string "aabbbb"
1 July 2021 CSE, BMSCE 24
(q0
, aabbbb, z0
)
25. Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty
stack method.
Instantaneous Description for the string "aabbbb"
1 July 2021 CSE, BMSCE 25
(q0
, aabbbb, z0
) ˫ (q0
, abbbb, aaz0
)
˫ (q0
, bbbb, aaaaz0
)
˫ (q1
, bbb, aaaz0
)
˫ (q1
, bb, aaz0
)
˫ (q1
, b, az0
)
˫ (q1
, 𝞮, z0
)
˫ (q1
, 𝞮, 𝞮)
All the Input symbols
are read and Stack is
empty, therefore the string
“aabbbb” is accepted.
26. Construct PDA ( or DPDA) to recognize the language L={an
b2n
, n>=1} by empty
stack method.
Instantaneous Description for the string "aabbb"
1 July 2021 CSE, BMSCE 26
(q0
, aabbb, z0
) ˫ (q0
, abbb, aaz0
)
˫ (q0
, bbb, aaaaz0
)
˫ (q1
, bb, aaaz0
)
˫ (q1
, b, aaz0
)
˫ (q1
, 𝞮, az0
)
All the Input symbols
are read but Stack not is
empty, therefore the string
“aabbb” is not accepted.
27. Question
The push down automata indicate the acceptance
of input string in terms of
a) Finial state
b) Empty stack
c) Both (a) and (b)
d) None of these
1 July 2021 CSE, BMSCE 27
28. Question
The push down automata indicate the acceptance
of input string in terms of
a) Finial state
b) Empty stack
c) Both (a) and (b)
d) None of these
1 July 2021 CSE, BMSCE 28
29. Question
The instantaneous description is PDA shows
a) Present state
b) Stack symbol
c) String to be processed
d) All of these
1 July 2021 CSE, BMSCE 29
30. Question
The instantaneous description is PDA shows
a) Present state
b) Stack symbol
c) String to be processed
d) All of these
1 July 2021 CSE, BMSCE 30
31. Question
Construct PDA ( or DPDA) to recognize the language L={a2n
bn
, n>=1} by empty stack
method. Give the graphical representation of the PDA obtained. Show the moves made by
PDA for the string aaaabb and aabbb
1 July 2021 CSE, BMSCE 31
32. Question
Construct PDA ( or DPDA) to recognize the language L={a2n
bn
, n>=1} by empty stack
method. Give the graphical representation of the PDA obtained. Show the moves made by
PDA for the string aaaabb and aabbb
Solution:
Logic
1 July 2021 CSE, BMSCE 32
a a a a b b 𝞮
Initial
State q0
Go To
State q1
Push a
Go To
State q0
Go To
State q1
Push a
Go To
State q0
Pop a
Go To
State q2
Pop a
Be in
State q2
Pop Z0
Be in
State q2
33. Construct PDA ( or DPDA) to recognize the language L={a2n
bn
, n>=1} by empty stack method.
Rough Slide
1 July 2021 CSE, BMSCE 33
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
a
q1
z0
a
q2
z0
a
34. Construct PDA ( or DPDA) to recognize the language L={a2n
bn
, n>=1} by empty
stack method.
1 July 2021 CSE, BMSCE 34
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
q1
X X
a q1
Pop a
q2
X
q1
z0
Push a
q0
X X
a Push a
q0
X X
q2
z0
X X Pop Z0
q2
a
X Pop a
q2
X
35. Construct PDA ( or DPDA) to recognize the language L={a2n
bn
, n>=1} by empty
stack method.
1 July 2021 CSE, BMSCE 35
q0
q1
Representing PDA as a generalized Transition Diagram
𝞮, Z0
| 𝞮
b, a | 𝞮
q2
b, a | 𝞮
a, z0
| z0
a, a | a
a, z0
| az0
a, a | aa
36. Construct PDA ( or DPDA) to recognize the language L={a2n
bn
, n>=1} by empty
stack method.
1 July 2021 CSE, BMSCE 36
37. Question
The push down automata indicate the acceptance
of input string in terms of
a) Finial state
b) Empty store
c) Both (a) and (b)
d) None of these
1 July 2021 CSE, BMSCE 37
38. Question
The push down automata indicate the acceptance
of input string in terms of
a) Finial state
b) Empty store
c) Both (a) and (b)
d) None of these
1 July 2021 CSE, BMSCE 38
39. Question
The instantaneous description is PDA shows
a) Present state
b) Stack symbol
c) String to be processed
d) All of these
1 July 2021 CSE, BMSCE 39
40. Question
The instantaneous description is PDA shows
a) Present state
b) Stack symbol
c) String to be processed
d) All of these
1 July 2021 CSE, BMSCE 40
41. Question
Design Deterministic Push Down Automata for the language
L={WcWR
, W ∈ (a+b)*} by empty stack. Show instantaneous
description for the string aabcbaa
Example Strings:
c , aca , bcb , aacaa , abcba , abbcbba
Logic: Push w on to stack, when c is seen switch to different state, for wr
do the
Popping operation
1 July 2021 CSE, BMSCE 41
42. Design DPDA for the language L={WcWR
, W ∈ (a+b)*} by empty stack.
Rough Slide
1 July 2021 CSE, BMSCE 42
State Stack
Top
Symbol
Input Symbols
a b c 𝞮
q0
z0
a
b
q1
Z0
a
b
43. Design DPDA for the language L={WcWR
, W ∈ (a+b)*} by empty stack.
1 July 2021 CSE, BMSCE 43
State Stack
Top
Symbol
Input Symbols
a b c 𝞮
q0
z0
Push a
q0
Push b
q0
q1
X
a Push a
q0
Push b
q0
q1
X
b Push a
q0
Push b
q0
q1
X
q1
Z0
X X X Pop Z0
q1
a Pop a
q1
X X X
b
X Pop b
q1
X X
q0
q1
Transition Diagram
a, z0
| az0
b, z0
| bz0
a, a | aa
a, b | ab
b, b | bb
b, a | ba
a, a | 𝞮
b, b | 𝞮
𝞮, Z0
| 𝞮
c, z0
| z0
c, a | a
c, b | b
44. Design DPDA for the language L={WcWR
, W ∈ (a+b)*} by empty stack.
1 July 2021 CSE, BMSCE 44
Formal definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
}
∑ = {a, b, c}
Γ ={a, b, Z0
}
q0
= q0
Z0
= Z0
F = { }
δ(q0
, a, z0
) =(q0
,az0
)
δ(q0
, b, z0
) =(q0
,bz0
)
δ(q0
, a, a) =(q0
,aa)
δ(q0
, a, b) =(q0
,ab)
δ(q0
, b, b) =(q0
,bb)
δ(q0
, b, a) =(q0
,ba)
δ(q0
, c, z0
) =(q1
,z0
)
δ(q0
, c, a) =(q1
,a)
δ(q0
, c, b) =(q1
,b)
δ(q1
, a, a) =(q1
, 𝞮)
δ(q1
, b, b) =(q1
, 𝞮)
δ(q1
, 𝞮, z0
) =(q1
, 𝞮)
Push Operation
Pop Operation
No action
45. Design DPDA for the language L={WcWR
, W ∈ (a+b)*} by empty stack.
Rough Slide
Instantaneous Description for the string "aabcbaa"
1 July 2021 CSE, BMSCE 45
(q0
, aabcbaa, z0
)
46. Design DPDA for the language L={WcWR
, W ∈ (a+b)*} by empty stack.
Instantaneous Description for the string "aabcbaa"
1 July 2021 CSE, BMSCE 46
(q0
, aabcbaa, z0
) ˫ (q0
, abcbaa, az0
)
˫ (q0
, bcbaa, aaz0
)
˫ (q0
, cbaa, baaz0
)
˫ (q1
, baa, baaz0
)
˫ (q1
, aa, aaz0
)
˫ (q1
, a, az0
)
˫ (q1
, 𝞮, z0
)
˫ (q1
, 𝞮, 𝞮)
All the Input symbols
are read and Stack is
empty, therefore the string
“aabcbaa” is accepted.
47. Question
Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na
(w)==nb
(w)} by empty stack. Show instantaneous description for the
string aababb
Example Strings:
ab , ba , baba , aabb , baab , aababb
Logic:
1 July 2021 CSE, BMSCE 47
Input Stack Top Operation
a Z0
Push a
b Z0
Push b
a a Push a
b a Pop a
a b Pop b
b b Push b
48. Design DPDA for the language L={W, W ∈ (a+b)* and na
(w)==nb
(w)} by empty stack.
Rough Slide
1 July 2021 CSE, BMSCE 48
State Stack
Top
Symbol
Input Symbols
a b 𝞮
q0
z0
a
b
q1
Z0
a
b
49. Design DPDA for the language L={W, W ∈ (a+b)* and na
(w)==nb
(w)} by empty
stack.
1 July 2021 CSE, BMSCE 49
State Stack
Top
Symbol
Input Symbols
a b 𝞮
q0
z0
Push a
q0
Push b
q0
q1
a Push a
q0
Pop a
q0
X
b Pop b
q0
Push b
q0
X
q1
Z0
X X Pop Z0
q1
a
X X X
b
X X X
50. Design DPDA for the language L={W, W ∈ (a+b)* and na
(w)==nb
(w)} by empty
stack.
1 July 2021 CSE, BMSCE 50
Instantaneous Description for the string "aababb"
(q0
, aababb, z0
) ˫ (q0
, ababb, az0
)
˫ (q0
, babb, aaz0
)
˫ (q0
, abb, az0
)
˫ (q0
, bb, aaz0
)
˫ (q0
, b, az0
)
˫ (q0
, 𝞮, z0
)
˫ (q1
, 𝞮, z0
)
˫ (q1
, 𝞮, 𝞮)
All the Input symbols
are read and Stack is
empty, therefore the string
“aababb” is accepted.
51. Question
Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na
(w) < nb
(w)} by final state. Show instantaneous description for the string
abbab
Example Strings:
b , bb , babab , abbb , bbbaa , abbab
Logic:
1 July 2021 CSE, BMSCE 51
Input Stack Top Operation
a Z0
Push a
b Z0
Push b
a a Push a
b a Pop a
a b Pop b
b b Push b
𝞮 b Move to Final State
52. Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na
(w) < nb
(w)} by final state.
1 July 2021 CSE, BMSCE 52
State Stack
Top
Symbol
Input Symbols
a b 𝞮
q0
z0
Push a
q0
Push b
q0
X
a Push a
q0
Pop a
q0
X
b Pop b
q0
Push b
q0
q1
q1
Z0
X X X
a
X X X
b
X X q1
q0
q1
Transition Diagram
a, z0
| az0
b, z0
| bz0
a, a | aa
a, b | 𝞮
b, b | bb
b, a | 𝞮
𝞮, b | b
𝞮, b | b
53. Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na
(w) < nb
(w)} by final state.
1 July 2021 CSE, BMSCE 53
Formal definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
}
∑ = {a, b}
Γ ={a, b, Z0
}
q0
= q0
Z0
= Z0
F = {q1
}
δ(q0
, a, z0
) =(q0
,az0
)
δ(q0
, b, z0
) =(q0
,bz0
)
δ(q0
, a, a) =(q0
,aa)
δ(q0
, b, b) =(q0
,bb)
δ(q0
, a, b) =(q0
, 𝞮)
δ(q0
, b, a) =(q0
, 𝞮)
δ(q0
, 𝞮, b) =(q1
,b)
δ(q1
, 𝞮, b) =(q1
,b)
Push Operation
Pop Operation
No action
Note: for na
(w) > nb
(w)
δ(q0
, 𝞮, a) =(q1
,a)
δ(q1
, 𝞮, a) =(q1
,a)
54. Design Deterministic Push Down Automata for the language L={W, W ∈ (a+b)*
and na
(w) < nb
(w)} by final state.
1 July 2021 CSE, BMSCE 54
Instantaneous Description for the string "abbab"
(q0
, abbab, z0
) ˫ (q0
, bbab, az0
)
˫ (q0
, bab, z0
)
˫ (q0
, ab, bz0
)
˫ (q0
, b, z0
)
˫ (q0
, 𝞮, bz0
)
˫ (q1
, 𝞮, bz0
)
˫ (q1
, 𝞮, bz0
)
All the Input symbols
are read and reached to
Final state, therefore the string
“abbab” is accepted.
55. Question
Design Deterministic Push Down Automata for the language L={ai
bJ
ck
, J=i+k
,i,k≥0} by empty stack. Show instantaneous description for the string abbbcc
Example Strings:
1 July 2021 CSE, BMSCE 55
i k J=i+k Strings
ai
bJ
ck
0 0 0 𝞮
1 1 2 abbc
1 0 1 ab
0 1 1 bc
2 2 4 aabbbbcc
2 1 3 aabbbc
1 2 3 abbbcc
56. Question
Design Deterministic Push Down Automata for the language L={ai
bJ
ck
, J=i+k
,i,k≥0} by empty stack. Show instantaneous description for the string abbbcc
Example Strings:
𝞮 , abbc , ab , bc , aabbbbcc , aabbbc , abbbcc
Logic:
1 July 2021 CSE, BMSCE 56
ai
bi
bk
ck 𝞮
Push a Pop a Push b Pop Z0
Pop b
ai
bJ
ck
ai
bi
bk
ck
57. Design Deterministic Push Down Automata for the language L={ai
bJ
ck
, J=i+k ,i,k≥0} by empty
stack.
Rough Slide
1 July 2021 CSE, BMSCE 57
State Stack Top
Symbol
Input Symbols
a b c 𝞮
q0
z0
a
b
q1
Z0
a
b
q2 Z0
a
b
q3 Z0
a
b
58. Design Deterministic Push Down Automata for the language L={ai
bJ
ck
, J=i+k ,i,k≥0} by
empty stack.
1 July 2021 CSE, BMSCE 58
State Stack
Top
Symbol
Input Symbols
a b c 𝞮
q0
z0
Push a , q0
Push b, q1
X q3
a Push a , q0
Pop a, q1
X X
b X X X X
q1
Z0
X Push b, q1
X X
a X Pop a, q1
X X
b X Push b, q1
Pop b, q2
X
q2 Z0
X X X q3
a X X X X
b X X Pop b, q2
X
q3 Z0
X X X Pop z0
, q3
a X X X X
b X X X X
59. Design Deterministic Push Down Automata for the language L={ai
bJ
ck
, J=i+k
,i,k≥0} by empty stack.
1 July 2021 CSE, BMSCE 59
Instantaneous Description for the string "abbbcc"
(q0
, abbbcc, z0
) ˫ (q0
, bbbcc, az0
)
˫ (q0
, bbcc, z0
)
˫ (q1
, bcc, bz0
)
˫ (q1
, cc, bbz0
)
˫ (q2
, c, bz0
)
˫ (q2
, 𝞮, z0
)
˫ (q3
, 𝞮, z0
)
˫ (q3
, 𝞮, 𝞮)
All the Input symbols
are read and stack is
empty, therefore the string
“abbbcc” is accepted.
60. Question
Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by final state
method.
1 July 2021 CSE, BMSCE 60
61. Construct PDA ( or DPDA) to recognize the language L={an
bn
, n>=1} by final state method.
Rough Slide
1 July 2021 CSE, BMSCE 61
States Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
a
q1
z0
a
q2
z0
a
63. Question
Design Non-deterministic Push Down Automata for the language L={WWR
, W ∈
(a+b)*} by empty stack. Show instantaneous description for the string abba
or
Design Non-deterministic Push Down Automata for the language L={x, x ∈
(a+b)* and x is a palindrome} by empty stack. Show instantaneous description
for the string abba
1 July 2021 CSE, BMSCE 63
64. Question
Design Non-deterministic Push Down Automata for the language L={WWR
, W ∈
(a+b)*} by empty stack. Show instantaneous description for the string abba
Example Strings:
𝞮 , aa , bb , aaaa , abaaba , bbaabb , abbbba
1 July 2021 CSE, BMSCE 64
65. Question
Design Non-deterministic Push Down Automata for the language L={WWR
, W ∈
(a+b)*} by empty stack. Show instantaneous description for the string abba
Example Strings:
𝞮 , aa , bb , aaaa , abaaba , bbaabb
Logic:
1 July 2021 CSE, BMSCE 65
Input Stack
Top
Operation
a Z0
Push a, q0
b Z0
Push b, q0
a a Push a, q0
; Pop a , q1
b a Push b, q0
a b Push a, q0
b b Push b, q0
; Pop b, q1
66. Design NPDA for the language L={WWR
, W ∈ (a+b)*} by empty stack.
Rough Slide
1 July 2021 CSE, BMSCE 66
Input Stack
Top
Operation
a Z0
Push a, q0
b Z0
Push b, q0
a a Push a, q0
; Pop a , q1
b a Push b, q0
a b Push a, q0
b b Push b, q0
; Pop b, q1
𝞮 Z0
Change state to q2
𝞮 Z0
Pop Z0
, q2
(q0
, abba, z0
)
Instantaneous Description for the string "abba"
Logic
67. Design NPDA for the language L={WWR
, W ∈ (a+b)*} by empty stack.
1 July 2021 CSE, BMSCE 67
Input Stack
Top
Operation
a Z0
Push a, q0
b Z0
Push b, q0
a a Push a, q0
; Pop a , q1
b a Push b, q0
a b Push a, q0
b b Push b, q0
; Pop b, q1
(q0
, abba, z0
) ˫ (q0
, bba, az0
)
˫ (q0
, ba,baz0
)
Instantaneous Description for the string "abba"
(q0
, a, bbaz0
) (q1
, a, az0
)
Logic
(q0
, 𝞮, abbaz0
)
(q1
, 𝞮, z0
)
(q1
, 𝞮, 𝞮)
Rejected
Accepted
68. Design NPDA for the language L={WWR
, W ∈ (a+b)*} by empty stack.
Rough Slide
1 July 2021 CSE, BMSCE 68
State Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
a
b
q1
Z0
a
b
q2
Z0
a
b
69. Design NPDA for the language L={WWR
, W ∈ (a+b)*} by empty stack.
1 July 2021 CSE, BMSCE 69
State Stack Top
Symbol
Input Symbols
a b 𝞮
q0
z0
Push a , q0
Push b , q0
q2
a Push a , q0
Push b , q0 X
Pop a , q1
b Push a , q0
Push b , q0 X
Pop b , q1
q1
Z0 X X q2
a Pop a , q1 X X
b X Pop b , q1 X
q2
Z0 X X Pop Z0
, q2
a X X X
b X X X
70. Design NPDA for the language L={WWR
, W ∈ (a+b)*} by empty stack.
1 July 2021 CSE, BMSCE 70
Formal definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
, q2
}
∑ = {a, b}
Γ ={a, b, Z0
}
q0
= q0
Z0
= Z0
F = { }
δ(q0
, a, z0
) =(q0
,az0
)
δ(q0
, b, z0
) =(q0
,bz0
)
δ(q0
, a, b) ={(q0
,ab)
δ(q0
, b, a) ={(q0
,ba)
δ(q0
, a, a) ={(q0
, aa), (q1
, 𝞮)}
δ(q0
, b, b) ={(q0
,bb), (q1
, 𝞮)}
δ(q1
, a, a) =(q1
, 𝞮)
δ(q1
, b, b) =(q1
, 𝞮)
δ(q1
, 𝞮, z0
) =(q2
, Z0
)
δ(q2
, 𝞮, z0
) =(q2
, 𝞮)
q0
q1
Transition Diagram
a, z0
| az0
b, z0
| bz0
a, a | aa
a, b | ab
b, b | bb
b, a | ba
a, a | 𝞮
b, b | 𝞮
a, a | 𝞮
b, b | 𝞮
q2
𝞮, Z0
| z0
𝞮, Z0
| 𝞮
71. Equivalence of PDA and CFG
- Converting CFG to PDA
- Converting PDA to CFG
1 July 2021 CSE, BMSCE 71
72. Procedure for converting PDA to CFG
1. The input symbols of PDA will be the terminals of CFG
2. If PDA moves from state qi
to qj
on consuming the input a ∈ Σ when Z is top of the
stack, then the non-terminals of CFG are the triplets of the form (qi
Zqj
)
3. If q0
is the start state and qf
is the final state then (q0
Zqf
) is the start symbol of the
CFG
4. The productions of CFG can be obtained from the transitions of PDA as shown below:
a. For each transition of the form δ(qi
, a, Z) = (qj
, AB) introduce the
productions of the form
(qi
Zqk
) -> a(qj
Aql
)(ql
Bqk
) where qk
and ql
will take all possible values from Q
a. For each transition of the form δ(qi
, a, Z) = (qj
, ε ) introduce the production
(qi
Zqj
) -> a
Note: Using this procedure, we may introduce lot of useless symbols, which in any way can be
eliminated.
1 July 2021 CSE, BMSCE 72
73. Question
Obtain CFG that generates the language accepted by
the PDA
1 July 2021 CSE, BMSCE 73
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
}
∑ = {a, b}
Γ ={A, Z}
q0
= q0
Z0
= Z
F = {q1
}
δ(q0
, a, Z)=(q0
, AZ)
δ(q0
, b, A)=(q0
, AA)
δ(q0
, a, A)=(q1
, 𝞮) q0
q1
a, z | AZ
b, A | AA
a, A | 𝞮
74. Question
1 July 2021 CSE, BMSCE 74
Obtain CFG that generates the language accepted by the PDA M=({q0
, q1
}, {a,
b}, {A, Z}, q0
, Z , {q1
} ,
{ δ(q0
, a, Z)=(q0
, AZ),
δ(q0
, b, A)=(q0
, AA),
δ(q0
, a, A)=(q1
, 𝞮))
Solution:
For δ of the form
δ(qi
, a, Z) = (qj
, ε )
Resulting Production
(qi
Zqj
) → a
δ(q0
, a, A)=(q1
, ε) (q0
Aq1
) → a
75. Obtain CFG that generates the language accepted by the PDA M=({q0
, q1
}, {a, b}, {A, Z}, q0
, Z , {q1
} ,
{δ(q0
, a, Z)=(q0
, AZ), δ(q0
, b, A)=(q0
, AA), δ(q0
, a, A)=(q1
, 𝞮))
1 July 2021 CSE, BMSCE
75
Solution (Contd…):
For δ of the form
δ(qi
, a, Z) = (qj
, AB)
Resulting Production
(qi
Zqk
) → a(qj
Aql
)(ql
Bqk
)
where qk
and ql
will take all possible
values from Q
δ(q0
, a, Z)=(q0
, AZ) (q0
Zq0
) → a(q0
Aq0
)(q0
Zq0
) | a(q0
Aq1
)(q1
Zq0
)
(q0
Zq1
) → a(q0
Aq0
)(q0
Zq1
) | a(q0
Aq1
)(q1
Zq1
)
76. Obtain CFG that generates the language accepted by the PDA M=({q0
, q1
}, {a, b}, {A, Z}, q0
, Z , {q1
} ,
{δ(q0
, a, Z)=(q0
, AZ), δ(q0
, b, A)=(q0
, AA), δ(q0
, a, A)=(q1
, 𝞮))
1 July 2021 CSE, BMSCE
76
Solution (Contd…):
For δ of the form
δ(qi
, a, Z) = (qj
, AB)
Resulting Production
(qi
Zqk
) → a(qj
Aql
)(ql
Bqk
)
where qk
and ql
will take all possible
values from Q
δ(q0
, a, Z)=(q0
, AZ) (q0
Zq0
) → a(q0
Aq0
)(q0
Zq0
) | a(q0
Aq1
)(q1
Zq0
)
(q0
Zq1
) → a(q0
Aq0
)(q0
Zq1
) | a(q0
Aq1
)(q1
Zq1
)
δ(q0
, b, A)=(q0
, AA) (q0
Aq0
) → b(q0
Aq0
)(q0
Aq0
) | b(q0
Aq1
)(q1
Aq0
)
(q0
Aq1
) → b(q0
Aq0
)(q0
Aq1
) | b(q0
Aq1
)(q1
Aq1
)
Start Symbol of the grammar is (q0
Zq1
)
78. Question
Obtain CFG that generates the language accepted by
the PDA
1 July 2021 CSE, BMSCE 78
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1 ,
q2
}
∑ = {a, b}
Γ ={A, Z}
q0
= q0
Z0
= Z
F = {q2
}
δ(q0
, a, Z)=(q0
, AZ)
δ(q0
, a, A)=(q0
, A)
δ(q0
, b, A)=(q1
, 𝞮)
δ(q1
, 𝞮, Z)=(q2
, 𝞮)
79. Converting PDA to CFG: M=({ q0
, q1 ,
q2
}, {a, b} , {A, Z}, q0 ,
Z, {q2
}, {
δ(q0
, a, Z)=(q0
, AZ), δ(q0
, a, A)=(q0
, A), δ(q0
, b, A)=(q1
, 𝞮), δ(q0
, 𝞮, Z)=(q2
, 𝞮)}
Solution:
To obtain CFG from the PDA, all transitions should be of the form
δ(qi
, a, Z) = (qj
, AB)
or
δ(qi
, a, Z) = (qj
, ε )
In the given transitions except the second transition, all transitions are in the required form. So, let
us take the second transition
δ(q0
, a, A)=(q0
, A)
This transition indicates that when input symbol a is encountered and top stack symbol is A, the PDA
remains in the state q0
and contents of the stack are not altered. This can be interpreted as delete A
from stack and insert A onto the stack. So once A is deleted from the stack we enter into a new state
q3
. But in state q3
without consuming any input we add A onto the stack. The corresponding
transitions are
δ(q0
, a, A)=(q3
, 𝞮)
δ(q3
, 𝞮, Z)=(q0
, AZ)
1 July 2021 CSE, BMSCE 79
80. Converting PDA to CFG: M=({ q0
, q1 ,
q2
}, {a, b} , {A, Z}, q0 ,
Z, {q2
}, {
δ(q0
, a, Z)=(q0
, AZ), δ(q0
, a, A)=(q0
, A), δ(q0
, b, A)=(q1
, 𝞮), δ(q1
, 𝞮, Z)=(q2
, 𝞮)}
Solution (Contd…):
So the given PDA can be written using the following transitions
δ(q0
, a, Z)=(q0
, AZ)
δ(q0
, a, A)=(q3
, 𝞮)
δ(q3
, 𝞮, Z)=(q0
, AZ)
δ(q0
, b, A)=(q1
, 𝞮)
δ(q1
, 𝞮, Z)=(q2
, 𝞮)
1 July 2021 CSE, BMSCE 80
For δ of the form
δ(qi
, a, Z) = (qj
, ε )
Resulting Production
(qi
Zqj
) → a
δ(q0
, a, A)=(q3
, 𝞮) (q0
Aq3
)→ a
δ(q0
, b, A)=(q1
, 𝞮) (q0
Aq1
) → b
δ(q1
, 𝞮, Z)=(q2
, 𝞮) (q1
Zq2
) → 𝞮
81. Converting PDA to CFG: M=({ q0
, q1 ,
q2
}, {a, b} , {A, Z}, q0 ,
Z, {q2
}, {
δ(q0
, a, Z)=(q0
, AZ), δ(q0
, a, A)=(q0
, A), δ(q0
, b, A)=(q1
, 𝞮), δ(q1
, 𝞮, Z)=(q2
, 𝞮)}
Solution (Contd…):
So the given PDA can be written using the following transitions
δ(q0
, a, Z)=(q0
, AZ) , δ(q0
, a, A)=(q3
, 𝞮), δ(q3
, 𝞮, Z)=(q0
, AZ)
δ(q0
, b, A)=(q1
, 𝞮) , δ(q1
, 𝞮, Z)=(q2
, 𝞮)
1 July 2021 CSE, BMSCE 81
For δ of the form
δ(qi
, a, Z) = (qj
, AB)
Resulting Production
(qi
Zqk
) → a(qj
Aql
)(ql
Bqk
)
where qk
and ql
will take all possible
values from Q
δ(q0
, a, Z)=(q0
, AZ) (q0
Zq0
) → a(q0
Aq0
)(q0
Zq0
) | a(q0
Aq1
)(q1
Zq0
) |
a(q0
Aq2
)(q2
Zq0
) | a(q0
Aq3
)(q3
Zq0
)
(q0
Zq1
) → a(q0
Aq0
)(q0
Zq1
) | a(q0
Aq1
)(q1
Zq1
) |
a(q0
Aq2
)(q2
Zq1
) | a(q0
Aq3
)(q3
Zq1
)
(q0
Zq2
) → a(q0
Aq0
)(q0
Zq2
) | a(q0
Aq1
)(q1
Zq2
) |
a(q0
Aq2
)(q2
Zq2
) | a(q0
Aq3
)(q3
Zq2
)
(q0
Zq3
) → a(q0
Aq0
)(q0
Zq3
) | a(q0
Aq1
)(q1
Zq3
) |
a(q0
Aq2
)(q2
Zq3
) | a(q0
Aq3
)(q3
Zq3
)
82. Converting PDA to CFG: M=({ q0
, q1 ,
q2
}, {a, b} , {A, Z}, q0 ,
Z, {q2
}, {
δ(q0
, a, Z)=(q0
, AZ), δ(q0
, a, A)=(q0
, A), δ(q0
, b, A)=(q1
, 𝞮), δ(q1
, 𝞮, Z)=(q2
, 𝞮)}
Solution (Contd…):
So the given PDA can be written using the following transitions
δ(q0
, a, Z)=(q0
, AZ) , δ(q0
, a, A)=(q3
, 𝞮), δ(q3
, 𝞮, Z)=(q0
, AZ)
δ(q0
, b, A)=(q1
, 𝞮) , δ(q1
, 𝞮, Z)=(q2
, 𝞮)
1 July 2021 CSE, BMSCE 82
For δ of the form
δ(qi
, a, Z) = (qj
, AB)
Resulting Production
(qi
Zqk
) → a(qj
Aql
)(ql
Bqk
)
where qk
and ql
will take all possible values from Q
δ(q3
, 𝞮, Z)=(q0
, AZ) (q3
Zq0
) → (q0
Aq0
)(q0
Zq0
) | (q0
Aq1
)(q1
Zq0
) |
(q0
Aq2
)(q2
Zq0
) | (q0
Aq3
)(q3
Zq0
)
(q3
Zq1
) → (q0
Aq0
)(q0
Zq1
) | (q0
Aq1
)(q1
Zq1
) |
(q0
Aq2
)(q2
Zq1
) | (q0
Aq3
)(q3
Zq1
)
(q3
Zq2
) → (q0
Aq0
)(q0
Zq2
) | (q0
Aq1
)(q1
Zq2
) |
(q0
Aq2
)(q2
Zq2
) | (q0
Aq3
)(q3
Zq2
)
(q3
Zq3
) → (q0
Aq0
)(q0
Zq3
) | (q0
Aq1
)(q1
Zq3
) |
(q0
Aq2
)(q2
Zq3
) | (q0
Aq3
)(q3
Zq3
)
Start Symbol of the grammar is (q0
Zq2
)
86. Greibach Normal Form (GNF)
In Greibach Normal Form (GNF), there is a restriction on the terminals and
variables appearing on the right hand side of the production.
A CFG G = (V, T, P, S) is said to be in GNF if every production is of the form
A → a⍺
where a ∈ T and ⍺ ∈ V ∗ , i.e., ⍺ is a string of zero or more variables.
The first symbol on the right hand side of the production must be a terminal and
it can be followed by zero or more Variables.
If Grammar is in GNF, it can be easily converted into Push Down Automata (PDA),
which accepts Context Free Languages
1 July 2021 CSE, BMSCE 86
87. Procedure for converting CFG to PDA
Given any grammar, first obtain the grammar in GNF (Greibach Normal Form) and then
obtain PDA.
The steps to be followed to convert a grammar G=(V,T,P,S) to its equivalent PDA are
1. Convert the grammar to GNF
2. Let q0
be the start state and z0
is initial symbol on stack. Without consuming any input,
push the start symbol S onto the stack and change state to q1
The transition for this can be δ(q0
, ε, z0
)=(q1
,Sz0
)
3. For each production of the form A -> aβ introduce the transition
δ(q1
, a, A)=(q1
, β)
4. Finally, in state q1
, without consuming any input change the state to qf
which is an
accepting state. The transition can be of the form δ(q1
, ε, z0
)=(qf
, z0
)
1 July 2021 CSE, BMSCE 87
88. Question
Convert following Grammar to PDA. Show the instantaneous description for the
string “aabba”
S → aABC
A → aB | a
B → bA | b
C → a
1 July 2021 CSE, BMSCE 88
89. Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
Solution:
Step1: Convert the grammar to GNF
The given grammar is in GNF
Step2: Let q0
be the start state and z0
is initial symbol on stack. Without consuming any
input, push the start symbol S onto the stack and change state to q1
The transition for this can be δ(q0
, ε, z0
)=(q1
,Sz0
)
1 July 2021 CSE, BMSCE 89
90. Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
Rough Slide
Solution (Contd…):
Step3: For each production of the form A -> aβ introduce the transition δ(q1
, a, A)=(q1
,
β)
1 July 2021 CSE, BMSCE 90
Production Transition
S → aABC
A → aB
A → a
B → bA
B → b
C → a
91. Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
Solution (Contd…):
Step3: For each production of the form A -> aβ introduce the transition δ(q1
, a, A)=(q1
,
β)
Step4:Finally, in state q1
, without consuming any input change the state to qf
which is an
accepting state. The transition can be of the form δ(q1
, ε, z0
)=(qf
, z0
)
1 July 2021 CSE, BMSCE 91
Production Transition
S → aABC δ(q1
, a, S)=(q1
, ABC)
A → aB δ(q1
, a, A)=(q1
, B)
A → a δ(q1
, a, A)=(q1
, 𝞮)
B → bA δ(q1
, b, B)=(q1
, A)
B → b δ(q1
, b, B)=(q1
, 𝞮)
C → a δ(q1
, a, C)=(q1
, 𝞮)
92. Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
1 July 2021 CSE, BMSCE 92
Formal definition of PDA
P=(Q, ∑ , Γ , q0
, Z0
, δ, F)
Q = { q0
, q1
, qf
}
∑ = {a, b}
Γ ={S, A, B, C, Z0
}
q0
= q0
Z0
= Z0
F = {qf
}
δ(q0
, ε, z0
)=(q1
,Sz0
)
δ(q1
, a, S)=(q1
, ABC)
δ(q1
, a, A)=(q1
, B)
δ(q1
, a, A)=(q1
, 𝞮)
δ(q1
, b, B)=(q1
, A)
δ(q1
, b, B)=(q1
, 𝞮)
δ(q1
, a, C)=(q1
, 𝞮)
δ(q1
, ε, z0
)=(qf
, z0
)
93. Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
Rough Slide
1 July 2021 CSE, BMSCE 93
Transitions of PDA
δ(q0
, ε, z0
)=(q1
,Sz0
)
δ(q1
, a, S)=(q1
, ABC)
δ(q1
, a, A)=(q1
, B)
δ(q1
, a, A)=(q1
, 𝞮)
δ(q1
, b, B)=(q1
, A)
δ(q1
, b, B)=(q1
, 𝞮)
δ(q1
, a, C)=(q1
, 𝞮)
δ(q1
, ε, z0
)=(qf
, z0
)
Instantaneous Description for the string "aabba"
94. Convert following Grammar to PDA.
S → aABC ; A → aB | a ; B → bA | b ; C → a
1 July 2021 CSE, BMSCE 94
Transitions of PDA
δ(q0
, ε, z0
)=(q1
,Sz0
)
δ(q1
, a, S)=(q1
, ABC)
δ(q1
, a, A)=(q1
, B)
δ(q1
, a, A)=(q1
, 𝞮)
δ(q1
, b, B)=(q1
, A)
δ(q1
, b, B)=(q1
, 𝞮)
δ(q1
, a, C)=(q1
, 𝞮)
δ(q1
, ε, z0
)=(qf
, z0
)
(q0
, aabba, z0
) ˫ (q1
, aabba, Sz0
)
˫ (q1
, abba, ABCz0
)
Instantaneous Description for the string "aabba"
(q1
, bba, BBCz0
) (q1
, bba, BCz0
)
(q1
, ba, BCz0
)
Accepted
All Input Symbols are read and the final state is reached,
therefore the string “aabba” is accepted
(q1
, ba, ABCz0
)
(q1
, a, Cz0
) (q1
, a, ACz0
)
(q1
, 𝞮, z0
)
(qf
, 𝞮, z0
)
99. Pumping Lemma for Context Free Language (CFL)
Let L be context free language and is infinite. Let z is sufficiently
long string and z ∈ L ,
so that |z| >=n where n is some positive integer. If the string z can
be decomposed into combination of strings
z=uvwxy
Such that |vwx| <= n , |vx| >=1 ,
then uvi
wxi
y ∈ L for i=0,1,2,3,……
1 July 2021 CSE, BMSCE 99
100. Using Pumping Lemma to show certain languages are not
CFL
The general strategy used to prove that a given language is not CFL
is shown below:
1. Assume that the language L is infinite and it is context free
2. Select string say z and break it into sub strings u, v, w, x and y
such that |z|>=n,
z=uvwxy where |vwx| <= n , |vx| >=1
3. Find any i such that uvi
wxi
y ∉ L . According to pumping lemma
uvi
wxi
y ∈ L . So, the result is a contradiction to the assumption
that the language is context free. Therefore, the given language L
is not context free.
1 July 2021 CSE, BMSCE 100
101. Question
Show that the language L={an
bn
cn
, n≥1} is not Context Free Language (CFL).
1 July 2021 CSE, BMSCE 101
102. Question
Show that the language L={an
bn
cn
, n≥1} is not Context Free Language (CFL).
Solution:
Step1: Let L be CFL and let z= an
bn
cn
∈ L
Step2: Note that |z|=| an
bn
cn
|=3n >= n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
1 July 2021 CSE, BMSCE 102
103. Question
Show that the language L={an
bn
cn
, n≥0} is not Context Free Language (CFL).
Solution:
Step1: Let L be CFL and let z= an
bn
cn
∈ L
Step2: Note that |z|=| an
bn
cn
|=3n >= n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
1 July 2021 CSE, BMSCE 103
|vx| =|aj
al
|= (j+ l) ≥ 1
|vwx| =|aj
ak
al
|=(j+k+ l) ≤ n
104. Question
Show that the language L={an
bn
cn
, n≥0} is not Context Free Language (CFL).
Solution (Contd…):
Step3: For all i, uvi
wxi
y ∈ L.
Hence L={an
bn
cn
, n≥0} is not CFL
1 July 2021 CSE, BMSCE 104
105. Question
Show that the language L={WW , W ∈ (a+b)* }is not Context Free Language (CFL).
1 July 2021 CSE, BMSCE 105
106. Show that the language L={WW , W ∈ (a+b)*} is not Context Free Language (CFL).
Rough Slide
Solution:
Step1: Let L be CFL and Let z= an
bn
an
bn
∈ L
Step2: Note that |z|=| an
bn
an
bn
|=4n >= n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
1 July 2021 CSE, BMSCE 106
109. Question
Show that the language L={W , W ∈ (0+1)* , |w| is a perfect square }is not Context Free
Language (CFL).
1 July 2021 CSE, BMSCE 109
110. Show that the language L={W , W ∈ (0+1)* , |w| is a perfect square }is not Context Free Language
(CFL).
Rough Slide
Solution:
Step1: Let L be CFL and Let z= 0n2
∈ L
Step2: Note that |z|=| 0n2
|=n2
≥ n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
Step 3: For all i, uvi
wxi
y ∈ L.
1 July 2021 CSE, BMSCE 110
111. Show that the language L={W , W ∈ (0+1)* , |w| is a perfect square }is not Context Free Language
(CFL).
Rough Slide
Solution:
Step1: Let L be CFL and Let z= 0n2
∈ L
Step2: Note that |z|=| 0n2
|=n2
≥ n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
Step 3: For all i, uvi
wxi
y ∈ L.
Let |vx| = m , m ≤ n.
But for i=2,
n2
< |uv2
wx2
y| = n2
+m ≤ (n2
+ n) < (n+1)2
And hence the length is nit a perfect square and therefore string is not in L. This
contradiction allows us to conclude that our assumption was wrong and L is not CFL.
1 July 2021 CSE, BMSCE 111
112. Question
Show that the language L={an!
, n≥0} is not Context Free Language (CFL).
Solution:
Step1: Let L be CFL and it is infinite, Let z= an!
∈ L
Step2: Note that |z|=| an!
|=n! > n
Split z into uvwxy such that |vwx| ≤ n and |vx| ≥ 1
1 July 2021 CSE, BMSCE 112
|vwx| ≤ n, |aj
+ak
+al
| =(j+k+ l ) ≤ n
v=aj
, w=ak
, x=al
an!
= an!-(j+k+l )
aj
ak
al
|vx| ≥ 1, |aj
+al
| =(j+ l )≥ 1
113. Question
Show that the language L={an!
, n≥0 is not Context Free Language (CFL).
Solution (Contd..):
Step3: For all i, uvi
wxi
y ∈ L.
But for i=2,
1 July 2021 CSE, BMSCE 113
an!-(j+k+l)
a2j
ak
a2l
= an!+j+l
|an!+j+l
|= n!+j+l ≤ n!+n
< n!+n!(n)
< n!(n+1)
n! < (n!+j+l) < (n+1)!
Since, (n!+j+l) lies in between n! and (n+1)! ,
the string generated by uv2
wx2
y = an!+j+l
∉ L
which is contradiction, hence L is not CFL
114. Question
Pumping lemma for context free language breaks
the strings into
a) Two parts
b) Three parts
c) Five parts
d) Six parts
1 July 2021 CSE, BMSCE 114
115. Answer
Pumping lemma for context free language breaks
the strings into
a) Two parts
b) Three parts
c) Five parts
d) Six parts
1 July 2021 CSE, BMSCE 115
116. Question
In pumping lemma for context free language
a) We start by assuming the given language is context free and
then we get contradict
b) We first convert the given language into regular language and
then apply steps on
c) Both (a) and (b)
d) None of these
1 July 2021 CSE, BMSCE 116
117. Answer
In pumping lemma for context free language
a) We start by assuming the given language is context free
and then we get contradict
b) We first convert the given language into regular language and
then apply steps on
c) Both (a) and (b)
d) None of these
1 July 2021 CSE, BMSCE 117
119. 119
Closure properties of CFL
□ Closure properties consider operations on CFL that are
guaranteed to produce a CFL
□ The CFL’s are closed under union, concatenation, closure (star),
reversal, homomorphism, inverse homomorphism and
substitution.
□ CFL’s are not closed under intersection and complementation
120. 120
CFLs are closed under Union Operation
□ Let L1
and L2
be two CFL’s generated by CFGs G1
=(V1
, T1
, S1
, P1
) and G2
=(V2
, T2
, S2
, P2
)
121. 121
CFLs are closed under Union Operation
□ Let L1
and L2
be two CFL’s generated by CFGs G1
=(V1
, T1
, S1
, P1
) and G2
=(V2
, T2
, S2
, P2
)
□ To get grammar for L1
∪ L2
?
■ Add new start symbol S and rules S → S1
| S2
■ We get grammar G = (V, T, P, S) where
V = V1
∪ V2
∪ { S }, where S ∉ V1
∪ V2
P = P1
∪ P2
∪ { S → S1
| S2
}
122. 122
CFLs are closed under Union Operation
□ Let L1
and L2
be two CFL’s generated by CFGs G1
=(V1
, T1
, S1
, P1
) and G2
=(V2
, T2
, S2
, P2
)
□ To get grammar for L1
∪ L2
?
■ Add new start symbol S and rules S → S1
| S2
■ We get grammar G = (V, T, P, S) where
V = V1
∪ V2
∪ { S }, where S ∉ V1
∪ V2
P = P1
∪ P2
∪ { S → S1
| S2
}
□ Example:
■ L1
= { an
bn
| n ≥ 0 } , L2
= { bn
an
| n ≥ 0 }
■ G1
: S1
→ aS1
b | ε G2
: S2
→ bS2
a | ε
■ L1 ∪ L2 is G = ({S1
, S2
, S}, {a, b}, P, S) where
P = {P1 ∪ P2 ∪ {S → S1
| S2
}}
□ Union of two CFLs results in CFL.
123. 123
CFLs are closed under Concatenation Operation
□ Let L1
and L2
be two CFL’s generated by CFGs G1
=(V1
, T1
, S1
, P1
) and G2
=(V2
, T2
, S2
, P2
)
□ To get grammar for L1
L2
?
■ Add new start symbol S and rule S → S1
S2
■ We get G = (V, T, P, S) where
V = V1
∪ V2
∪ { S }, where S ∉ V1
∪ V2
P = P1
∪ P2
∪ { S → S1
S2
}
□ Example:
■ L1
= { an
bn
| n ≥ 0 } with G1
: S1
→ aS1
b | ε
■ L2
= { bm
am
| m ≥ 0 } with G2
: S2
→ bS2
a | ε
■ L1
L2
= { an
b{n+m}
am
| n, m ≥ 0 } with G = ({S, S1
, S2
}, {a, b},
{S → S1
S2
, S1
→ aS1
b | ε, S2
→ bS2
a}, S)
□ Concatenation of Two CFLs results in a CFL
124. 124
CFLs are closed under Kleene’s-Closure or
Star-Closure Operation
□ Let L1
be the CFL generated by CFG G1
=(V1
, T1
, S1
, P1
)
□ Kleene Star Closure L(G)=(L(G1
))*
□ Example:
■ L1
= {an
bn
| n ≥ 0}
(L1
)*= { a{n1}
b{n1}
... a{nk}
b{nk}
| k ≥ 0 and ni ≥ 0 for all i }
□ To get the grammar for (L1
)*
■ Add new start symbol S and rules S → SS1
| ε.
■ We get G = (V, T, P, S) where
V = V1
∪ { S }, where S ∉ V1
P = P1
∪ { S → SS1
| ε}
125. Context-free languages are not closed under Intersection
□ Consider two CFL’s
L1
={an
bn
cm
, n ≥ 0, m ≥ 0} and
L2
={an
bm
cm
, n ≥ 0, m ≥ 0}
□ But L1 ∩ L2 ={an
bn
cn
, n ≥ 0}, which is not CFL
□ Thus family of CFL’s is not closed under Intersection operation.
1 July 2021 CSE, BMSCE 125
126. 126
Intersection
□ The CFL’s are not closed under intersection
□ Example:
■ L = {0n
1n
2n
|n ≥ 1} is not context-free.
■ L1 = {0n
1n
2i
|n ≥ 1,i ≥1 }, L2 = {0i
1n
2n
|n ≥ 1,i ≥1 } are CFL’s with
corresponding grammars for L1: S->AB; A->0A1 | 01; B->2B | 2 ,
and for L2: S ->AB; A->0A | 0; B->1B2 | 12.
■ However, L = L1
∩ L2
■ Thus intersection of CFL’s is not CFL
127. Context-free languages are not closed under Complement
□ If the family of CFL’s were closed under Complementation then the right side
of the above expression would be a CFL for any L1
and L2
. But this contradicts
what we have just shown in previous slide that the intersection of two CFLs is
not necessarily Context free.
□ Consequently, the family of CFL is not closed under Complementation
1 July 2021 CSE, BMSCE 127
128. 128
Set Difference
□ L1
and L2
are CFLs. L1
- L2
is not necessarily a CFL
Proof:
■ L1 = ∑* - L
■ ∑* is regular and is also CFL
■ But ∑* - L = LC
■ If CFLs were closed under set difference, then ∑* - L = LC
would
always be a CFL.
■ But CFL’s are not closed under complementation
129. 129
Reversal
□ The CFL’s are closed under reversal
□ This means then if L is a CFL, so LR
is a CFL
□ It is enough to reverse each production of a CFL for L, i.e.,
substitute A→α by A→αR
□ Example:
■ L = { an
bn
| n ≥ 0 } with P : S → aSb | ε
■ LR
= {bn
an
| n ≥ 0 } with PR
: S → bSa | ε
130. 130
Homomorphism
□ Closure under homomorphism of CFL L for every a∈∑
□ Suppose L is a CFL over alphabet ∑ and h is a homomorphism
on ∑.
□ Let s be a substitution that replaces every a ∈ ∑, by h(a). i.e.,
s(a) = {h(a)}.
□ Then h(L) = s(L).
□ h(L) ={h(a1
)…h(ak
) | k ≥ 0} where h(ak
) is a homomorphism for
every ak
∈ ∑.
131. 131
Inverse homomorphism
□ To recall: If h is a homomorphism, and L is any language, then
h-1
(L), called an inverse homomorphism, is the set of all strings
w such that h(w)∈L
□ The CFL’s are closed under inverse homomorphism.
□ Theorem: If L is a CFL and h is a homomorphism, then h-1
(L) is
a CFL
133. 133
Proof Contd...
□ After input a is read, h(a) is placed in a buffer.
□ Symbols of h(a) are used one at a time and fed to PDA being
simulated.
□ Only when the buffer is empty does the PDA read another of its
input symbol and apply homomorphism to it.
134. 134
Proof Contd...
□ Suppose h applies to symbols of alphabet Σ and produces
strings in T*.
□ Let PDA P = (Q, T, Γ, δ, q0
, Z0
, F) that accept CFL L by
final state.
□ Construct a new PDA P′ = (Q′, Σ, Γ, δ′, (q0
, ε), Z0
, F X {ε})
to simulate language of h-1
(L), where
■ Q′ is the set of pairs (q, x) such that
□ q is a state in Q
□ x is a suffix of some string h(a) for some input string a in Σ
135. 135
Proof Contd...
■ δ′ is defined by
□ δ′((q, ε), a, X) = {((q, h(a)),a,X)}
□ If δ(q, b, X) = {(p, γ)} where b∈T or b = ε then δ′((q, bx), ε, X) = {((p, x), γ)}
■ The start state of P’ is (q0, ε)
■ The accepting state of P′ is (q, ε), where q is an accepting state of P.
■ (q0
,h(w),Z0
)|-*P (p,ε,γ) iff ((q0
,ε),w,Z0
) |-*P′ ((p, ε), ε, γ)
■ P accepts h(w) if and only if P′ accepts w, because of the way the accepting states of
P′ are defined.
■ Thus L(P′)=h-1
(L(P))
136. 136
Intersection with RL
□ Theorem: If L is CFL and R is a regular language, then L ∩
R is a CFL.
Stack
PDA
FA
AN
D
Accept/
Reject
137. 137
Intersection with RL Proof
□ P=(QP
, ∑, Γ, δP
, qP
, Z0
, FP
) be PDA to accept CFL by final
state
□ A=(QA
, ∑, δA
, qA
, FA
) be a DFA for RL
□ Construct PDA P′ = (Q, ∑, Γ, δ, qo
, Z0
, F) where
■ Q = Qp
X QA
■ qo
= (qp
, qA
)
■ F = (FP
X FA
)
■ δ is in the form δ((q, p), a, X) = ((r, s), γ) such that
1. s = δA
(p, a)
2. (r, γ) is in δP
(q, a, X)
138. 138
Proof Contd…
□ For each move of PDA P, we make the same move in PDA P′ and
also we carry along the state of DFA A in a second component of
P′.
□ P′ accepts a string w iff both P and A accept w.
□ w is in L ∩ R.
□ The moves ((qp
, qA
), w, Z) |-*P′ ((q, p), ε, γ) are possible iff (qp
,
w, Z) |-*P (q, ε, γ) moves and p = δ*(qA
, w) transitions are
possible.
139. 139
Set Difference with RL
□ For a CFL’s L, and a regular language R.
L - R is a CFL.
Proof:
■ R is regular and RC
is also regular
■ L - R = L ∩ RC
■ Complement of Regular Language (RC
) is regular
■ Intersection of a CFL and a regular language is CFL
■ CFL and Regular language are closed under intersection.
140. 140
Complementation
□ LC
is not necessarily a CFL
□ Proof:
■ Assume that CFLs were closed under complement.
■ If L is a CFL then LC
is a CFL
■ Since CFLs are closed under union, L1
C
∪ L2
C
is a CFL
■ And by our assumption (L1
C
∪ L2
C
) C
is a CFL
■ But (L1
C
∪ L2
C
) C
= L1
∩ L2
which we just showed isn’t
necessarily a CFL.
■ Contradiction!
141. 141
Substitution
□ Each symbol in the strings of one language is replaced by
an entire CFL language
□ Useful in proving some other closure properties of CFL
□ Example: S(0) = {an
bn
| n ≥1}, S(1) = {aa, bb} is a
substitution on alphabet Σ ={0, 1}.
142. 142
Substitution
■ Theorem: If a substitution s assigns a CFL to every symbol in the alphabet
of a CFL L, then s(L) is a CFL.
■ Proof
□ Let G = (V, ∑, P, S) be grammar for L
□ Let Ga
= (Va
, Ta
, Pa
, Sa
) be the grammar for each a ∈ ∑ with V∩Va
=φ
□ G′= (V′, T′, P′, S) for s(L) where
■ V′ = V ∪ Va
■ T′ = union of Ta
for all a ∈ ∑
■ P′ consists of
▪ All productions in any Pa
for a ∈ ∑
▪ In productions of P, each terminal a is replaced by Sa
■ A detailed proof that this construction works is in the reader.
■ Intuition: this replacement allows any string in La to take the place
of any occurrence of a in any string of L.
143. 143
Example (1)
□ L = {0n
1n
| n ≥1}, generated by the grammar
S→0S1|01,
□ s(0) = {an
bm
|m≤ n}, generated by the grammar S→aSb|A;
A→aA| ab,
□ s(1)={ab, abc}, generated by the grammar S → abA, A → c
|ε
□ Rename second and third S’s to S0
and S1
respectively.
Rename second A to B. Resulting grammars are:
S→0S1 | 01
S0
→aS0
b | A; A→aA | ab
S1
→abB; B→c | ε
144. 144
Example(1) Contd...
□ In the first grammar replace 0 by S0
and 1 by S1
. The combined
grammar:
G′ = ({S, S0
, S1
, A, B}, {a, b}, P′, S),
where P′ = {S → S0
SS1
| S0
S1
, S0
→ aS0
b | A, A →aA | ab,
S1
→abB, B→ c | ε}
145. 145
Application of Substitution
□ Closure under union of CFL’s L1
and L2
□ Closure under concatenation of CFL’s L1
and L2
□ Closure under Kleene’s star (closure * and positive
closure +
) of CFL’s L1
□ Closure under homomorphism of CFL Li
for every
ai
∈ ∑
149. Question
The context free languages are closed under:
a) Intersection
b) Complement
c) Kleene’s-Closure
d) None of the mentioned
1 July 2021 CSE, BMSCE 149
150. Question
The context free languages are closed under:
a) Intersection
b) Complement
c) Kleene’s-Closure
d) None of the mentioned
1 July 2021 CSE, BMSCE 150
151. Question
Context free languages are not closed under:
a) Intersection
b) Complement
c) All of the mentioned
1 July 2021 CSE, BMSCE 151
152. Question
Context free languages are not closed under:
a) Intersection
b) Complement
c) All of the mentioned
1 July 2021 CSE, BMSCE 152
153. Applications of Context Free Grammars (CFG)
YACC (Yet Another Compiler-Compiler)
Mark up language (HTML)
XML (Extensible Markup Language)
1 July 2021 CSE, BMSCE 153