Water Industry Process Automation & Control Monthly - April 2024
Theory of Computation Unit 3
1. UNIT III
CONTEXT FREE GRAMMAR &
LANGUAGE
Mrs.D.Jena Catherine Bel,
Assistant Professor,CSE,
Velammal Engineering College.
2. CONTEXT FREE
GRAMMAR & LANGUAGE
CFG – Parse Trees – Ambiguity in Grammars and Languages – Definition of the
Pushdown Automata – Languages of a Pushdown Automata – Equivalence of
Pushdown Automata and CFG, Deterministic Pushdown Automata.
4. CONTEXT FREE GRAMMAR
• A CFG is a way of describing languages by recursive rules
called productions
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
4
5. CFG – Formal Definition
A context-free grammar (CFG) consisting of a finite
set of grammar rules is a quadruple (V, T, P, S)
where
• V is a set of non-terminal symbols.
• T is a set of terminals where V ∩ T = NULL.
• P is a set of rules, P: V → (V ∪ T)*, i.e., the left-
hand side of the production rule P does have any
right context or left context.
• S is the start symbol.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
5
6. Derivation
• Beginning with the start symbol, derive the terminal strings by
repeatedly replacing the variable by the body of some
production with that variable in the head
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
6
7. Derivation
• Leftmost derivation:
• In each step of derivation, if the leftmost variable is replaced then
it is called leftmost derivation
• Rightmost derivation:
• In each step of derivation, if the rightmost variable is replaced
then it is called rightmost derivation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
7
8. • Sentential Forms
• Any step in a derivation is a string of variables and or terminals.
Such strings are called sentential forms.
• If the derivation is leftmost then the string is a left sentential
form
• If the derivation is rightmost then the string is a right sentential
form
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
8
9. Parse Tree
• Tree representation of derivation is called parse tree or
derivation tree
• Interior nodes are labelled with variables and leaf nodes are
labelled with terminal or ε
• For each interior node, there must be a production such that
the head of the production is the label of the node and the
label of its children read from left to right form the body of
the production
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
9
11. Derivation / yield of a tree
The derivation or the yield of a parse tree is the final string
obtained by concatenating the labels of the leaves of the tree
from left to right, ignoring the Nulls. However, if all the leaves are
Null, derivation is Null.
Example
Let a CFG {N,T,P,S} be
N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε
One derivation from the above CFG is “abaabb”
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
11
14. Ambiguous grammar
• If a context free grammar G has more than one derivation tree
for some string w ∈ L(G), it is called an ambiguous grammar.
There exist multiple right-most or left-most derivations for
some string generated from that grammar.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
14
16. Why PDA
Design FA for accepting a language {an | n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
16
17. Why PDA
Design FA for accepting a language {an | n>=1}
L = {a, aa, aaa, aaaa, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
17
18. Why PDA
Design FA for accepting a language {an | n>=1}
L = {a, aa aaa, aaaa, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
18
19. Why PDA
Design FA for accepting a language {anbm | m,n>=0}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
19
20. Why PDA
Design FA for accepting a language {anbm | m,n>=0}
L= {ε,a,b,ab,aa,bb,aaa,aab,abb,bbb,aabb,aaabbb,aaaabbbb,
......}
Constraints/limitations
1. a followed by b
2. N >=0
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
20
21. Why PDA
Design FA for accepting a language {anbm | m,n>=0}
L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb,
aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
21
22. Why PDA
Design FA for accepting a language {an | n>=1}
L = {a, aa aaa, aaaa, ......}
Design FA for accepting a language {anbm | m,n>=0}
L = {ε, a, b,ab,aa, bb , aaa, aab, abb,bbb,..aabb, aaabbb,
aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
22
23. Why PDA
Design FA for accepting a language {anbn | n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
23
24. why
Design FA for accepting a language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
24
25. why
Design FA for accepting a language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >=0
3.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
25
26. why
Design FA for accepting a language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (a ) == Number of (b)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
26
28. Why PDA
is it Possible
Design FA for accepting a language {anbn | n>=1}
Design FA for accepting a language {anb2n | n>=1}
Design FA for accepting a language {anbm cn|m. n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
28
30. PDA Components
Input tape: The input tape is divided in many cells or symbols. The input head is read-only and may only move from left to right,
one symbol at a time.
Finite control: The finite control has some pointer which points the current symbol which is to be read.
Stack: The stack is a structure in which we can push and remove the items from one end only. It has an infinite size. In PDA, the
stack is used to store the items temporarily.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
30
35. Representation of State
Transition
Delta Function ( ) is the transition function, the use of which will become more clear by
taking a closer look at the Three Major operations done on Stack :-
1. Push
2. Pop
3. Skip /No operation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
35
41. Moves of PDA
Pop Skip/ no operation
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
41
42. Pushdown Automata
Define the pushdown automata for language {anbn |
n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
42
43. Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (a ) == Number of (b)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
43
44. Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
1. While (a)
Push a;
While (b)
Pop ;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
44
46. Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
46
47. Pushdown Automata
Define the pushdown automata for language {anbn | n>=1}
L = {ab, aabb, aaabbb, aaaabbbb, ......}
1. While (a)
Push a;
While (b)
Pop ;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
47
48. PDA for the Language {anbn | n>=1}
PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (qf, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
48
49. Pushdown Automata
2. Design PDA for accepting a language {anb2n | n>=1}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
49
50. Pushdown Automata
Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
Constraints/limitations
1. a followed by b
2. N >0
3. Number of (b ) == 2 * Number of (a)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
50
51. Pushdown Automata
Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
1. While (a)
push(aa);
While(b)
Pop ;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
51
52. Pushdown Automata
Problem – Design a PDA for accepting the language L = { anb2n | n>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
Logic 1:
While (a)
push(aa);
While(b)
Pop ;
Logic 2:
While (a)
push(a);
While(bb)
Pop;
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
52
53. PDA for the Language {anb2n | n>=1}
Logic 1:
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
δ:
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q2, ε, z) = (q2, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
53
55. PDA for the Language {anb2n | n>=1}
Logic 2:
PDA = ({q0, q1, q2, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, a)
δ(q1, b, a) = (q2, ε)
δ(q2, b, a) = (q1, a)
δ(q2, ε, Z) = (qf, Z)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
55
56. PDA foracceptingalanguage {0n1m0n |m,n>=1}
3. Design a PDA for accepting a language {0n1m0n | m, n>=1}.
L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... }
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
56
57. PDAfor acceptingalanguage {0n1m0n |m,n>=1}
Design a PDA for accepting a language {0n1m0n | m, n>=1}.
L = { 010, 0110, 01110, 001100, 0001000, 00001110000, ...... }
• Push all 0's onto the stack on encountering first 0's.
• read 1, just do nothing.
• read 0, and on each read of 0, pop one 0 from the stack.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
57
60. Instantaneous Description (ID)
A ID is a triple (q, w, α), where:
1. q is the current state.
2. w is the remaining input.
3.α is the stack contents, top at the left.
(p, b, T) ⊢ (q, w, α)
⊢ sign describes the turnstile notation and represents one move.
⊢* sign describes a sequence of moves.
Problem – Design a non deterministic
PDA for accepting the language L = { anb2n
: m>=1}, i.e.,
L = {abb, aabbbb, aaabbbbbb, aaaabbbbbbbb, ......}
δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)
⊢ δ(q0, abbbbbb, aaaaZ)
⊢ δ(q0, bbbbbb, aaaaaaZ)
⊢ δ(q1, bbbbb, aaaaaZ)
⊢ δ(q1, bbbb, aaaaZ)
⊢ δ(q1, bbb, aaaZ)
⊢ δ(q1, bb, aaZ)
⊢ δ(q1, b, aZ)
⊢ δ(q1, ε, Z)
⊢ δ(q2, ε)
ACCEPT
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
60
62. PushDown Automata
Find the language accepted by PDA
PDA for accepting the language L = { anb m+n cm|| m,n ≥ 1}
The strings of given lanugage will be: L = {abbc, abbbcc, abbbcc, aabbbbcc, ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
62
63. PDA for the Language {anbn | n>=1}
PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (qf, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
63
64. PDA for the Language {anbn | n>=1}
PDA = ({q0, q1, qf}, {a, b}, {a, Z}, δ, q0, Z, {qf})
δ:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (qf, ε)
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
64
67. L={w?{a,b}*}
Problem – Design a non deterministic PDA for accepting the language L ={w?{a,b}* | w contains equal no. of
a’s and b’s}, i.e.,
L = {ε,ab, aabb, abba, aababb, bbabaa, baaababb, .......}
If ‘a’ comes first then push it in stack and if again ‘a’ comes then also push it.
Similarly, if ‘b’ comes first (‘a’ did not comes yet) then push it into the stack and if
again ‘b’ comes then also push it.
Now, if ‘a’ is present in the top of the stack and ‘b’ comes then pop the ‘a’ from the
stack. And if ‘b’ present in the top of the stack and ‘a’ comes then pop the ‘b’ from
the stack.
So, at the end if the stack becomes empty then we can say that the string is accepted
by the PDA.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
67
69. Deterministic pushdown
Automata
Construct a PDA for language L = {wcw’ | w={0, 1}*}
where w’ is the reverse of w
L = {1c1, 0c0, 01c10, 001c100, 010c010 ......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
69
70. Non Deterministic pushdown Automata
Design a non deterministic PDA for
accepting the language L = {wwR w ∈
(a, b)*},
i.e.,
L = {aa, bb, abba, aabbaa, abaaba,
......}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
70
73. PDA–Applications
● For designing the parsing phase of a compiler (Syntax
Analysis).
● For implementation of stack applications.
● For evaluating the arithmetic expressions.
● For solving the Tower of Hanoi Problem.
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
73
74. S.NO PUSHDOWN AUTOMATA FINITE AUTOMATA
1.
For Type-2 grammar we
can design pushdown
automata.
For Type-3 grammar we
can design finite
automata.
2.
Non – Deterministic
pushdown automata has
more powerful than
Deterministic pushdown
automata.
Non-Deterministic Finite
Automata has same
powers as in
Deterministic Finite
Automata.
3.
Not every Non-
Deterministic pushdown
automata is transformed
into its equivalent
Deterministic pushdown
Every Non-Deterministic
Finite Automata is
transformed into an
equivalent Deterministic
Finite Automata
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
74
75. Construct a PDA
Construct a PDA for language L = {0n1m| n >= 1, m >= 1, m > n+2}
● Step-1: On receiving 0 push it onto stack. On receiving 1, ignore it and goto next state
● Step-2: On receiving 1, ignore it and goto next state
● Step-3: On receiving 1, pop a 0 from top of stack and go to next state
● Step-4: On receiving 1, pop a 0 from top of stack. If stack is empty, on receiving 1 ingore it and
goto next state
● Step-5: On receiving 1 ignore it. If input is finished then goto last state
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
75
80. Exercises
• NPDA for accepting the language L = {am b(2m) | m>=1}
• NPDA for accepting the language L = {ambncn | m,n ≥ 1}
• NPDA for accepting the language L = {amb(m+n)cn | m,n ≥ 1}
• NPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1}
• NPDA for accepting the language L = {a(m+n)bmcn | m,n ≥ 1}
• NPDA for accepting the language L = {anb(2n) | n>=1} U {anbn | n>=1}
• NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}
• NPDA for accepting the language L = {anbm | n,m ≥ 1 and n ≠ m}
• NPDA for accepting the language L = {amb(2m+1) | m ≥ 1}
• NPDA for accepting the language L = {an bn | n>=1}
• NPDA for accepting the language L = {a2mb3m | m ≥ 1}
• NPDA for accepting the language L = {an bn cm | m,n>=1}
• NPDA for accepting the language L = {ambnc(m+n) | m,n ≥ 1}
• NPDA for accepting the language L = {wwR | w ∈ (a,b)*}
• DFA for accepting the language L = {an bm | n+m=odd}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
80
87. Definition
Alphabet
● Definition: An alphabet is any finite set of symbols.,
● Example: Σ = {a, b, c, d} is an alphabet set where ‘a’, ‘b’, ‘c’, and ‘d’ are symbols.,
String
1. Definition: A string is a finite sequence of symbols taken from Σ.,
2. Example: ‘cabcad’ is a valid string on the alphabet set Σ = {a, b, c, d}
Length of a String
1. Definition : It is the number of symbols present in a string. (Denoted by |S|).,
2. Examples:,
3. If S=‘cabcad’, |S|= 6
If |S|= 0, it is called an empty string (Denoted by λ or ε)
Kleene Star
1. Definition: The Kleene star, Σ* , is a unary operator on a set of symbols or strings, Σ, that gives the
infinite set of all possible strings of all possible lengths over Σ including λ.
2. Representation: Σ* = Σ0 U Σ1 U Σ2 U……. where Σp is the set of all possible strings of length p.
3. Example: If Σ = {a, b}, Σ*= {λ, a, b, aa, ab, ba, bb,………..}
4. Kleene Closure / Plus
1. Definition: The set Σ+ is the infinite set of all possible strings of all possible lengths over Σ excluding
λ.
2. Representation: Σ+ = Σ1 U Σ2 U Σ3 U……. Σ+ = Σ* − { λ }
3. Example: If Σ = { a, b } , Σ+ ={ a, b, aa, ab, ba, bb,………..}
5. Language
1. Definition : A language is a subset of Σ* for some alphabet Σ. It can be finite or infinite.
2. Example : If the language takes all possible strings of length 2 over Σ = {a, b}, then L = { ab, bb, ba, bb}
Mrs.D.Jena
Catherine
Bel,AP/CSE,
VEC
87