0% found this document useful (0 votes)
4 views21 pages

Unit-4 Solution

The document discusses Pushdown Automata (PDA), including its definition, types (Deterministic and Nondeterministic), and their properties. It also covers the closure properties of Context-Free Languages (CFLs), decision problems related to CFLs, and the conversion of Context-Free Grammars (CFGs) to PDAs. Additionally, it addresses the context-freeness of specific languages and provides examples of PDA constructions and proofs regarding the non-regularity and non-context-freeness of certain languages.

Uploaded by

tooeasha1708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Unit-4 Solution

The document discusses Pushdown Automata (PDA), including its definition, types (Deterministic and Nondeterministic), and their properties. It also covers the closure properties of Context-Free Languages (CFLs), decision problems related to CFLs, and the conversion of Context-Free Grammars (CFGs) to PDAs. Additionally, it addresses the context-freeness of specific languages and provides examples of PDA constructions and proofs regarding the non-regularity and non-context-freeness of certain languages.

Uploaded by

tooeasha1708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit-4

Short Answer Type Questions with Solutions

1. Discuss briefly about the Push Down Automata (PDA).


Ans-Pushdown Automata (PDA) is a type of automaton used to recognize Context-Free
Languages (CFLs). It is similar to a finite automaton but with an added stack as auxiliary
memory. The stack allows the PDA to keep track of an unlimited amount of information,
making it more powerful than a finite automaton.
2. What do you mean by Two stack Pushdown Automata?
Ans:- A Two-Stack Pushdown Automaton is an extension of the standard PDA that uses two
stacks instead of one. This makes it more powerful than a regular PDA and gives it the same
computational power as a Turing Machine.
a) Two-Stack PDA is strictly more powerful than a regular PDA.
b) Equivalent in power to a Turing Machine.
c) Useful in theoretical computer science to show the computational limits of automata
models.
3. Define PDA. Draw the graphical representation for PDA.
Ans:- A Pushdown Automaton (PDA) is a finite automaton equipped with a stack. It is used
to recognize Context-Free Languages (CFLs).
Formal Definition:
A PDA is a 7-tuple:
PDA = (Q, Σ, Γ, δ, q₀, Z₀, F)
Where:
 Q = Finite set of states
 Σ = Input alphabet
 Γ = Stack alphabet

δ : Q × (Σ ∪ {ε}) × Γ → P(Q × Γ*)


 δ = Transition function:

 q₀ ∈ Q = Initial state

 Z₀ ∈ Γ = Initial stack symbol

 F ⊆ Q = Set of final states


4.Design a PDA which accepts set of balanced Paranthesis ( { { } } ).

Ans:- Step Input Left Current State Stack Content Action


q Z
1 {{}} ₀ ₀ Read {, push X: → XZ₀
q XZ
2 {}} ₀ ₀ Read {, top is X, push another X: → XXZ₀
q XXZ
3 }} ₀ ₀ Read }, pop one X: → XZ₀
q XZ
4 } ₀ ₀ Read }, pop one X: → Z₀
q Z
5 (empty) ₀ ₀ Input finished. On ε, move to qf if top is Z₀
Z
6 (empty) qf (final) ₀ ✅ Accepted

5. Differentiate DPDA with NPDA.


Ans:-

Feature DPDA (Deterministic PDA) NPDA (Non-Deterministic PDA)

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

Language Accepts a proper subset of Context- Accepts all Context-Free Languages


Accepted Free Languages (CFLs) (CFLs)

Behavior is uniquely determined at


Determinism Behavior is not uniquely determined
every step

Ease of Design Harder to design due to restrictions Easier to design and more flexible

Used in some deterministic parsers


Real-world use Used in theoretical models and proofs
(e.g., LL(1), LR(1))

Not possible (only one computation Possible (explores multiple


Backtracking
path) computation paths in parallel)

Power Less powerful More powerful


6. What in nondeterministic PDA?
Ans:- A Nondeterministic Pushdown Automaton (NPDA) is a type of PDA where multiple
transitions are allowed for the same input symbol, current state, and top stack symbol.
It is a theoretical machine used to recognize all Context-Free Languages (CFLs).

7.What are Closure Properties of CFLs?


Ans:- CFLs are closed under:
 Union
 Concatenation
 Kleene star
 Positive Closure
CFLs are not closed under:
 Intersection
 Complement
 Difference

8.What are the decision problems of CFLs?


 Emptiness: Decidable
 Finiteness: Decidable
 Membership: Decidable
 Equivalence: Undecidable
 Universality: Undecidable

9.What is the difference between a PDA and a Turing Machine


(TM)?
Feature PDA Turing Machine

Memory Stack Infinite tape

Language Context-Free Languages Recursively Enumerable


class (CFLs) Languages
Feature PDA Turing Machine

Power Less powerful More powerful

10.How is a CFG converted to a PDA?


Ans:- A CFG is converted to a PDA by:
 Starting with the start symbol on the stack.
 Replacing non-terminals on top of the stack with their productions.
 Matching terminals with input symbols.

Medium Answer Type Questions with Solutions


1. What in nondeterministic PDA? Explain with the help of transition
function.
Ans:- A Nondeterministic Pushdown Automaton (NPDA) is a machine that can
have multiple choices (transitions) at any step — based on:
 current state,
 input symbol
 and stack top.
Unlike DPDA, which has only one possible move per configuration, NPDA can
"guess" the correct path and accept a string if at least one path leads to
acceptance.
L = { aⁿbⁿ | n ≥ 0 }
Examples: ε, ab, aabb, aaabbb — all belong to the language.
But aab or abb do not.

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

2. Convert the grammar S -> aAA, A -> a |aS| bS to


a PDA that accepts the same language by Empty
stack
Ans:-PDA for Grammar: S → aAA, A → a | aS | bS
This PDA accepts the language generated by the given grammar using empty
stack method.
Given Grammar:
S → aAA
A → a | aS | bS
PDA Construction Steps:

Step 1: Push the start symbol 'S' onto the stack.


δ(q₀, ε, Z₀) → (q₀, SZ₀)

Step 2: Apply production rules (non-terminal expansion):


δ(q₀, ε, S) → (q₀, AAa)
δ(q₀, ε, A) → (q₀, a)
δ(q₀, ε, A) → (q₀, Sa)
δ(q₀, ε, A) → (q₀, Sb)

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.

Step-by-Step Simulation for Input: aabb


Initial configuration:
State = q₀, Input = aabb, Stack = Z₀

Input
Step State Stack Action
Remaining

1 q₀ aabb Z₀ Read a, push X → Stack: XZ₀


Input
Step State Stack Action
Remaining

2 q₀ abb XZ₀ Read a, push X → Stack: XXZ₀

ε-move: switch to q₁ without reading input →


3 q₀ bb XXZ₀
Stack: XXZ₀

4 q₁ bb XXZ₀ Read b, pop X → Stack: XZ₀

5 q₁ b XZ₀ Read b, pop X → Stack: Z₀

6 q₁ (empty) Z₀ ε-move to final state qf → Accepted

3. Find out whether the language L = {xⁿyⁿzⁿ | n ≥


1} is context free or not.

Context-Freeness of Language L = {xⁿyⁿzⁿ | n ≥ 1}


We are given the language L = {xⁿyⁿzⁿ | n ≥ 1} and asked to determine whether it is a
context-free language (CFL) or not.

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

3. 3. For all i ≥ 0, the string u(v^i)w(x^i)y ∈ L


2. 2. |vx| ≥ 1

Choose a string s = x^p y^p z^p ∈ L. Clearly, |s| ≥ 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.

4. Construct a PDA M equivalent to grammar with


following productions: S -> aAA , A -> aS / bS / a

PDA Construction for Grammar:


Given Grammar:
S → aAA
A → aS | bS | a

PDA Construction (By Empty Stack):


We construct a PDA M = (Q, Σ, Γ, δ, q₀, Z₀, ∅) that accepts by empty stack.

Q = {q₀} (only one state)

Σ = {a, b} (input symbols)

Γ = {S, A, a, b, Z₀} (stack symbols)

q₀ = initial state

Z₀ = initial stack symbol

F = ∅ (acceptance by empty stack)

PDA Transitions (δ):


1. δ(q₀, ε, Z₀) → (q₀, SZ₀)

2. δ(q₀, ε, S) → (q₀, AAa)

3. δ(q₀, ε, A) → (q₀, Sa)

4. δ(q₀, ε, A) → (q₀, Sb)

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.

• It then simulates derivations using ε-transitions for each production rule.

• 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.

5. Prove that the language L = {anbncn|n≥0} is


neither regular nor context free

Proof that L = {a^n b^n c^n | n ≥ 0} is Neither


Regular nor Context-Free
Introduction
We are given the language L = {aⁿbⁿcⁿ | n ≥ 0}. We aim to prove that this language is:
1. Not regular.
2. Not context-free.
We will use the Pumping Lemma for regular languages and the Pumping Lemma for context-
free languages to prove both claims.

1. L is Not a Regular Language


To prove that L is not regular, we use the Pumping Lemma for regular languages:

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,

- For all i ≥ 0, xyⁱz ∈ L.


- |y| > 0,

Choose s = a^p b^p c^p ∈ L.


According to the lemma, we can write s = xyz, with |xy| ≤ p, which means that x and y
contain only a's.

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.

This contradicts the pumping lemma, so L is not regular.


2. L is Not a Context-Free Language
To prove that L is not context-free, we use the Pumping Lemma for context-free languages:

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,

- For all i ≥ 0, uvⁱw xⁱy ∈ L.


- |vx| > 0,

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

In all cases, uvⁱwxⁱy ∉ L for some i, which contradicts the lemma.


equal count.

Therefore, L is not context-free.

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.

6. Design PDA for language: K2 Long L = {s ϵ (0, 1)* |


number of 0’s and 1’s are not equal in every string of
s}.

Ans:- PDA Design for Language L

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.

PDA Design - Explanation


To design a PDA for this language, note that the language is not context-free if taken in the
form {w ∈ {0,1}* | number of 0s ≠ number of 1s}. However, we can construct a PDA that
accepts the complement of the language where the number of 0s equals the number of 1s,
then define L as the complement.
But since the PDA cannot recognize non-context-free languages directly, we observe that L is
not context-free. So a single PDA cannot accept all such strings. However, for academic
construction, we can define a PDA that accepts two parts:
1. Strings with more 0s than 1s.
2. Strings with more 1s than 0s.

Each of these parts is context-free, and we can construct a PDA for each.

PDA Construction (Sketch)


Let’s construct a PDA that accepts strings with unequal number of 0s and 1s by taking the
union of two PDAs:
- PDA1: Accepts strings with more 0s than 1s.
- PDA2: Accepts strings with more 1s than 0s.

PDA Design (High-Level):


1. Use a stack to keep track of the difference between 0s and 1s.
2. For each '0' read, push a symbol (say Z) onto the stack.
3. For each '1' read, pop a symbol from the stack.
4. If the stack is not empty at the end (either extra 0s or extra 1s), accept the string.
5. Reject if the stack is empty (equal number of 0s and 1s).

Note: This is a non-deterministic PDA since the machine has to guess whether it's counting
extra 0s or extra 1s.

Formal PDA Description (Sketch)


PDA = (Q, Σ, Γ, δ, q0, Z0, F)
Q = {q0, q1, q2, qf}
Σ = {0, 1}, Γ = {Z, X},
Start state = q0, Initial stack symbol = Z, Final state = qf

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.

7. Design PDA for language: K2 Long L = {s ϵ (0,


1)* |number of 0’s and 1’s are not equal in every
string of s}.
Formal PDA Description (Sketch)
PDA = (Q, Σ, Γ, δ, q0, Z0, F)
Q = {q0, q1, q2, qf}
Σ = {0, 1}, Γ = {Z, X},
Start state = q0, Initial stack symbol = Z, Final state = qf

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.

8. Construct PDA to accept L = {0ⁿ1ⁿ | n > 0}


Formal PDA Description
PDA = (Q, Σ, Γ, δ, q0, Z0, F), where:
Q = {q0, q1, qf}
Σ = {0, 1}
Γ = {Z0, X}
Start state: q0
Initial stack symbol: Z0
Final state: qf
Transitions δ:
1. (q0, 0, Z0) → (q0, XZ0) ; Push X for each 0
2. (q0, 0, X) → (q0, XX) ; Continue pushing X
3. (q0, 1, X) → (q1, ε) ; Start popping for 1s
4. (q1, 1, X) → (q1, ε) ; Continue popping X
5. (q1, ε, Z0) → (qf, Z0) ; Accept when stack reaches bottom and input is done

PDA Diagram (Text Description)


States:
- q0: Reads 0s and pushes X onto the stack.
- q1: Reads 1s and pops X from the stack.
- qf: Accepts if stack has only Z0 and input is consumed.

The PDA switches from q0 to q1 upon reading the first 1.

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.

Long Answer Type Questions with Solutions

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₀, ε)}

2.Generate CFG for the given PDA M is defined as


M = ({q0, q1}, {0,1} {x, z0}, δ, q0, z0, q1)

where δ is given as follows:

δ (q0,1, z0) = (q0, xz0)

δ (q0,1, x) = (q0, xx)

δ (q0,0, x) = (q0, x)

δ (q0, ε, x) = (q1, ε)

δ (q1, ε, x) = (q1, ε)

δ (q1,0, x) = (q1, xx)


δ (q1,0, z0) = ( q1, ε)
3. Differentiate between deterministic PDA (DPDA) and non-deterministic
PDA (NPDA) with suitable example. Also discuss two stack PDA with example.

Deterministic vs Non-deterministic PDA and Two-


Stack PDA
Step 1: Definition of DPDA and NPDA
• Deterministic PDA (DPDA): A PDA is deterministic if for each state, input symbol, and stack
symbol, there is at most one transition.
• Non-deterministic PDA (NPDA): A PDA is non-deterministic if there exist multiple possible
transitions for the same input and stack symbol.

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 3: Example of DPDA


Language: L = {0^n1^n | n ≥ 0}
PDA pushes 0s on stack, pops 0 for each 1.
Transitions:
δ(q0, 0, Z0) = (q0, 0Z0)
δ(q0, 0, 0) = (q0, 00)
δ(q0, 1, 0) = (q1, ε)
δ(q1, 1, 0) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
This PDA is deterministic as there is only one move per input.

Step 4: Example of NPDA


Language: L = {a^n b^n | n ≥ 0} ∪ {a^n b^{2n} | n ≥ 0}
The PDA non-deterministically chooses whether to match equal or double b's.
Hence, it needs to guess and split its computation, making it non-deterministic.

Step 5: Two-Stack PDA


A two-stack PDA (2-PDA) is a more powerful variant of a PDA that uses two stacks instead of
one.
It can recognize languages that are not context-free, including some context-sensitive
languages.

Step 6: Example of 2-PDA


Language: L = {a^n b^n c^n | n ≥ 0}
This language is not context-free, but can be recognized by a 2-PDA.
• First, push 'a's onto Stack 1.
• Then, for each 'b', pop from Stack 1 and push to Stack 2.
• For each 'c', pop from Stack 2.
If both stacks are empty at the end, accept.
4. Design a PDA for the Language L ={WWR | W={a,b}*}
PDA Design for L = {WWᴿ | W ∈ {a,b}*}
Step 1: Understand the Language
The language L = {WWᴿ | W ∈ {a,b}*} consists of any string followed by its reverse.
For example: 'abba', 'aabbaa', 'ababba'.
We need a PDA that can store the first half and compare it with the reverse of the second
half.

Step 2: PDA Approach


1. Read and push symbols from the input onto the stack (phase 1).
2. At some point, non-deterministically guess the midpoint (ε-move).
3. Start popping the stack and match the input with the top of the stack (phase 2).
4. If the stack becomes empty at the end, accept the input.

Step 3: PDA Components


States: {q0, q1, q2}
Input symbols: {a, b}
Stack symbols: {a, b, Z0}
Start state: q0
Start stack symbol: Z0
Accepting state: q2

Step 4: Transitions (δ)


δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb)
δ(q0, ε, a) = (q1, a)
δ(q0, ε, b) = (q1, b)
δ(q1, a, a) = (q1, ε)
δ(q1, b, b) = (q1, ε)
δ(q1, ε, Z0) = (q2, Z0)

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)

You might also like