Unit-4 Solution
Unit-4 Solution
q₀ ∈ Q = Initial state
Non-Deterministic Pushdown
Full Form Deterministic Pushdown Automaton
Automaton
Transition For each state, input, and stack For each state, input, and stack
Function symbol, at most one transition symbol, multiple transitions allowed
Epsilon (ε) Allowed, but only when no input Allowed freely, can have many ε-
moves symbol transition is available transitions
Ease of Design Harder to design due to restrictions Easier to design and more flexible
Transition Function δ
δ(q, input, → {(new state, new
S.No Description
stack top) stack content)}
1 δ(q₀, a, Z₀) → {(q₀, XZ₀)} Push X on stack
2 δ(q₀, a, X) → {(q₀, XX)} Push X on stack
Non-deterministically switch to
3 δ(q₀, ε, X) → {(q₁, X)}
popping state
4 δ(q₁, b, X) → {(q₁, ε)} Pop X for each b
Accept if stack returns to initial
5 δ(q₁, ε, Z₀) → {(qf, Z₀)}
state
Step 3: Match terminal symbols with input and pop from stack:
δ(q₀, a, a) → (q₀, ε)
δ(q₀, b, b) → (q₀, ε)
Transition Table:
Transition No. δ(q₀, input, stack top) → (q₀, new stack
content)
1 δ(q₀, ε, Z₀) (q₀, SZ₀)
2 δ(q₀, ε, S) (q₀, AAa)
3 δ(q₀, ε, A) (q₀, a)
4 δ(q₀, ε, A) (q₀, Sa)
5 δ(q₀, ε, A) (q₀, Sb)
6 δ(q₀, a, a) (q₀, ε)
7 δ(q₀, b, b) (q₀, ε)
Acceptance:
This PDA accepts strings that can be derived from the grammar by emptying
the stack completely after consuming the entire input string.
Input
Step State Stack Action
Remaining
Step-by-Step Solution:
Let us assume that L is a context-free language.
such that any string s ∈ L with |s| ≥ p can be split into 5 parts: s = uvwxy, satisfying the
Then, by the Pumping Lemma for Context-Free Languages, there exists a pumping length 'p'
following conditions:
1. 1. |vwx| ≤ p
Since |vwx| ≤ p, the substring vwx lies within the first p symbols of s, i.e., within the x’s and
possibly some y’s.
Case 1: vwx lies entirely in x’s ⇒ Pumping v and x will change the number of x’s but not y’s
and z’s ⇒ x^n y^p z^p ≠ L.
Case 2: vwx lies across x’s and y’s ⇒ Pumping will unbalance the number of x’s and y’s ⇒ x^i
y^j z^p (i ≠ j) ≠ L.
Case 3: vwx lies entirely in y’s ⇒ Pumping changes only the number of y’s ⇒ x^p y^i z^p (i ≠
p) ≠ L.
Similar contradiction arises for other placements of vwx (e.g., y-z or only z).
In all cases, pumping causes the string to fall out of L, contradicting the pumping lemma.
Conclusion:
Therefore, our assumption is incorrect. The language L = {xⁿyⁿzⁿ | n ≥ 1} is NOT context-free.
q₀ = initial state
5. δ(q₀, ε, A) → (q₀, a)
6. δ(q₀, a, a) → (q₀, ε)
7. δ(q₀, b, b) → (q₀, ε)
Explanation:
• The PDA starts by pushing the start symbol S onto the stack.
• Terminal symbols on the stack are matched and popped when reading input.
• The PDA accepts the string if the stack becomes empty after consuming all input symbols.
Assume that L is regular. Then, there exists a pumping length p such that any string s ∈ L
with |s| ≥ p can be divided into three parts, s = xyz, satisfying:
- |xy| ≤ p,
Now the number of a's is not equal to the number of b's and c's, so xy²z ∉ L.
Let y = a^k, where k > 0. Then xy²z = a^(p+k) b^p c^p.
Assume that L is context-free. Then, there exists a pumping length p such that any string s ∈
L with |s| ≥ p can be written as s = uvwxy, satisfying:
- |vwx| ≤ p,
Choose s = a^p b^p c^p ∈ L. Since |vwx| ≤ p, vwx can span at most two types of symbols.
We consider all possibilities:
- If vwx lies entirely in one segment (only a's or only b's or only c's), then pumping will
unbalance the number of a's, b's, or c's.
- If vwx spans two segments (e.g., a's and b's, or b's and c's), pumping will again disrupt the
Conclusion
We have shown that the language L = {aⁿbⁿcⁿ | n ≥ 0} is neither regular nor context-free
using the Pumping Lemma for regular and context-free languages.
Language Definition
L = { s ∈ {0,1}* | number of 0’s and number of 1’s are not equal in every string of s }
This means that the number of 0s is not equal to the number of 1s in any string s. In other
words, L contains all strings over {0,1} where the count of 0s ≠ count of 1s.
Each of these parts is context-free, and we can construct a PDA for each.
Note: This is a non-deterministic PDA since the machine has to guess whether it's counting
extra 0s or extra 1s.
Transitions:
1. q0, ε, Z → q1, Z (start)
2. q1, 0, ε → q1, X (push for 0)
3. q1, 1, X → q1, ε (pop for 1)
4. q1, 1, Z → q2, Z (switch to count extra 1s)
5. q2, 1, ε → q2, X (push for 1)
6. q2, 0, X → q2, ε (pop for 0)
7. q1 or q2, ε, X → qf, X (accept if imbalance)
Note: If the stack is empty with Z and no imbalance, it should not go to qf.
Conclusion
The language L is not context-free, but we can recognize it using the union of two context-
free PDAs as described. The PDA sketched above accepts strings where the number of 0s is
not equal to the number of 1s.
Transitions:
1. q0, ε, Z → q1, Z (start)
2. q1, 0, ε → q1, X (push for 0)
3. q1, 1, X → q1, ε (pop for 1)
4. q1, 1, Z → q2, Z (switch to count extra 1s)
5. q2, 1, ε → q2, X (push for 1)
6. q2, 0, X → q2, ε (pop for 0)
7. q1 or q2, ε, X → qf, X (accept if imbalance)
Note: If the stack is empty with Z and no imbalance, it should not go to qf.
Conclusion
The language L is not context-free, but we can recognize it using the union of two context-
free PDAs as described. The PDA sketched above accepts strings where the number of 0s is
not equal to the number of 1s.
Conclusion
The above PDA accepts the language L = {0ⁿ1ⁿ | n > 0} using stack operations to ensure that
the number of 0s equals the number of 1s, and all 0s come before any 1s.
1. Construct a PDA from the following CFG. G = ({S, X}, {a, b}, P, S)
where the productions are – S → XS | ε , A → aXb | Ab |b PDA
Construction from CFG
Given CFG:
Non-terminals: {S, X}
Terminals: {a, b}
Start symbol: S
Productions:
S → XS | ε
X → aXb | Xb | b
PDA Construction:
We construct a PDA that accepts by empty stack, simulating leftmost derivations.
PDA Definition (7-Tuple):
P = (Q, Σ, Γ, δ, q₀, Z₀, F)
Q = {q₀}
Σ = {a, b}
Γ = {S, X, a, b, Z₀}
q₀ = start state
Z₀ = initial stack symbol
F = ∅ (acceptance by empty stack)
Transitions (δ):
δ(q₀, ε, Z₀) = {(q₀, SZ₀)}
δ(q₀, ε, S) = {(q₀, XS)}
δ(q₀, ε, S) = {(q₀, ε)}
δ(q₀, ε, X) = {(q₀, aXb)}
δ(q₀, ε, X) = {(q₀, Xb)}
δ(q₀, ε, X) = {(q₀, b)}
δ(q₀, a, a) = {(q₀, ε)}
δ(q₀, b, b) = {(q₀, ε)}
δ (q0,0, x) = (q0, x)
δ (q0, ε, x) = (q1, ε)
δ (q1, ε, x) = (q1, ε)
Step 2: Characteristics
DPDA:
• Has at most one possible move for each configuration.
• Cannot make ε-moves when a move on input exists.
• Recognizes a proper subset of context-free languages.
NPDA:
• Can have multiple transitions.
• Can use ε-moves to explore alternatives.
• Recognizes all context-free languages.
Step 5: Description
q0: Push phase – read and push symbols.
q1: Pop phase – read input and pop matching symbol from stack.
q2: Final state when Z0 is reached, indicating successful match of W and Wᴿ.
Step 6: Transition Graph
Graph:
q0 --a/Z0→aZ0--> q0
q0 --a/a→aa--> q0
q0 --b/b→bb--> q0
q0 --ε/a→a--> q1
q1 --a/a→ε--> q1
q1 --b/b→ε--> q1
q1 --ε/Z0→Z0--> q2
(Final state: q2)