Top Banner Ad

BCS402 — Unit 4: Push Down Automata and Properties of Context Free Languages

Table of Contents (18 sections)

BCS402 · Theory of Automata and Formal Languages · Unit 4

Push Down Automata and Properties of Context Free Languages

Unit Scope & Exam Weightage: This unit covers NPDA definition and moves, acceptance by final state and empty stack, DPDA and deterministic CFLs, PDA construction for CFLs, CFG from PDA, two-stack PDA, pumping lemma for CFL, closure and decision properties, and programming problems on CFL properties. Expect 7-mark constructions (PDA diagrams/transition tables) and 2-mark definitions/comparisons.


Importance-Wise Roadmap (BCS402 Unit 4)

Priority Exact Syllabus Topic PYQ Evidence Marks Pattern Tag
1 PDA construction for CFL (palindrome, a^n b^2n, CFG→PDA) 2023-24 (PYQ), predicted clusters 2023-26 7m × 4 Core construction
2 Two-stack PDA (a^n b^n c^n) 2023-24, 2025-26 7m + 2m Power argument
3 DPDA vs NPDA / DCFL 2023-24, 2025-26 7m × 2 Comparison
4 CFG from given PDA 2023-24 7m [qXp] rules
5 Acceptance by final state / empty stack 2025-26 2m Definitions
6 CFL pumping lemma, closure, decision problems Not directly asked Predicted Theory backup

PDA-specific examiner guidance: Always show the stack content after each input symbol in an instantaneous-description (ID) sequence. A construction answer must state the stack bottom marker z0z_0, the push/pop convention (leftmost symbol = top), and the acceptance mode.


1. Pushdown Automaton — Model and Moves

Nondeterministic Pushdown Automaton (NPDA): a 7-tuple M=(Q,Σ,Γ,δ,q0,z0,F)M=(Q,\Sigma,\Gamma,\delta,q_0,z_0,F) where QQ = finite states, Σ\Sigma = input alphabet, Γ\Gamma = stack alphabet, δ:Q×(Σ∪{ε})×Γ→\delta:Q\times(\Sigma\cup\{\varepsilon\})\times\Gamma\rightarrow finite subsets of Q×Γ∗Q\times\Gamma^*, q0q_0 = start state, z0∈Γz_0\in\Gamma = initial stack bottom symbol, F⊆QF\subseteq Q = final states. The stack gives unbounded memory with LIFO access.

1.1 Moves and Instantaneous Descriptions

An ID (instantaneous description) is (q,w,α)(q,w,\alpha): current state qq, unread input ww, stack content α\alpha with the leftmost symbol on top.

Move type Transition ID transition
Consume input, replace top (p,aX)∋(q,γ)(p,aX)\ni(q,\gamma) (p,aw,Xβ)⊢(q,w,γβ)(p,aw,X\beta)\vdash(q,w,\gamma\beta)
ε-move (no input consumed) (p,ε,X)∋(q,γ)(p,\varepsilon,X)\ni(q,\gamma) (p,w,Xβ)⊢(q,w,γβ)(p,w,X\beta)\vdash(q,w,\gamma\beta)

Acceptance:

  • By final state: ww accepted if some computation ends in (q,ε,α)(q,\varepsilon,\alpha) with q∈Fq\in F (stack content irrelevant).
  • By empty stack: ww accepted if some computation ends in (q,ε,ε)(q,\varepsilon,\varepsilon) — the stack is empty. The two modes are equivalent: a PDA accepting by final state can be converted to one accepting by empty stack and vice versa.

PYQ (2025-26, 2 marks): "Define acceptance by final state and acceptance by empty stack in a PDA." Use the two bullet definitions above; add that in empty-stack acceptance the final-state set may be ignored, while final-state acceptance does not require an empty stack.

1.2 DPDA and DCFL

Feature DPDA NPDA
Transition per (q,a,X)(q,a,X) At most one Finitely many
ε-moves Restricted (no ambiguous ε/input choice) Unrestricted
Languages accepted Deterministic CFLs (DCFL), a proper subset of CFL All CFLs
Parser Deterministic (LR-style), efficient Needs backtracking
Complement closure Closed under complement Not closed under complement

PYQ (2025-26, 7 marks): "Why are NPDAs considered more powerful than DPDAs? Provide an example of a language accepted by NPDA but not by DPDA." Nondeterminism lets the machine guess the middle of a string or which branch of a grammar to use. Example: L=wwRL=ww^R over {a,b}\{a,b\} (even-length palindromes). The NPDA pushes symbols until it guesses the midpoint, then must match; no DPDA can know where the second half starts because there is no delimiter. Hence LL is CFL but not DCFL.

PYQ (2023-24, 7 marks): "Differentiate between DPDA and NPDA. Construct a PDA that accepts language L={a^n b^n | n≥1}." Use the table, then the construction below. Note anbna^nb^n is deterministic (the switch from a to b is visible in the input).

PDA for anbna^nb^n (empty stack):

q0: (q0, a, z0) -> (q0, a z0)     // push first a under z0
    (q0, a, a)  -> (q0, a a)      // push further a's
    (q0, b, a)  -> (q1, ε)        // see first b: start popping, switch state
q1: (q1, b, a)  -> (q1, ε)        // pop one a per b
    (q1, ε, z0) -> (q1, ε)        // empty stack -> accept

ID trace for aaabb: (q0,aaabb,z0)⊢(q0,aabb,az0)⊢(q0,abb,aaz0)⊢(q0,bb,aaaz0)⊢(q1,b,aaz0)⊢(q1,ε,az0)⊢(q1,ε,z0)⊢(q1,ε,ε)(q_0,aaabb,z_0)\vdash(q_0,aabb,az_0)\vdash(q_0,abb,aaz_0)\vdash(q_0,bb,aaaz_0)\vdash(q_1,b,aaz_0)\vdash(q_1,\varepsilon,az_0)\vdash(q_1,\varepsilon,z_0)\vdash(q_1,\varepsilon,\varepsilon) ✔ For aabb the last step would leave two as on the stack → rejected.


2. PDA Constructions for CFLs

2.1 PDA for L={anb2n∣n≥1}L=\{a^nb^{2n}\mid n\ge1\}

Predicted (bank PRED, 2025-26, 7 marks): "Construct PDA that accepts L={a^n b^{2n} | n≥1}."

Idea: push two markers for each a, then pop one marker per b.

q0: (q0, a, z0) -> (q0, X X z0)      // first a: push two X below z0
    (q0, a, X)  -> (q0, X X X)       // each further a: net +2 X
    (q0, b, X)  -> (q1, ε)           // first b: begin popping
q1: (q1, b, X)  -> (q1, ε)           // pop one X per b
    (q1, ε, z0) -> (q1, ε)           // accept when stack empties

Trace aabb (n=2, need 4 b's): after aa stack = XXXX z0; after four bs stack = z0 → ε → accepted. aaabb (5 b's) fails: no X to pop for the fifth b. aabbb (3 b's) fails: one X remains.

2.2 PDA for Palindromes / wwRww^R

Predicted (bank PRED, 2024-25, 7 marks ×2): "Design a PDA that accepts L={ww^R | w∈{a,b}*} / palindromes over {a,b}"

q0: (q0, a, X) -> (q0, aX)  for X in {a,b,z0}   // push symbols while guessing first half
    (q0, b, X) -> (q0, bX)
    (q0, ε, X) -> (q1, X)                      // nondeterministic guess of the middle
q1: (q1, a, a) -> (q1, ε)                      // match a against top
    (q1, b, b) -> (q1, ε)                      // match b against top
    (q1, ε, z0) -> (q1, ε)                     // accept by empty stack

Trace abba: push a, push b; guess middle after ab; then input b matches top b, input a matches top a, ε-pops z0z_0 → accepted. The guess is what makes the automaton nondeterministic; a DPDA cannot make this choice uniquely without a centre marker.

2.3 CFG → PDA (top-down parsing construction)

For CFG G=(V,Σ,P,S)G=(V,\Sigma,P,S), build PDA with Q={q}Q=\{q\}, stack alphabet V∪ΣV\cup\Sigma, start stack SS:

  • For each production A→X1X2…XkA\rightarrow X_1X_2\dots X_k: δ(q,ε,A)∋(q,X1X2…Xk)\delta(q,\varepsilon,A)\ni(q,X_1X_2\dots X_k) (push RHS left-to-right so leftmost symbol is on top).
  • For each terminal a∈Σa\in\Sigma: δ(q,a,a)∋(q,ε)\delta(q,a,a)\ni(q,\varepsilon) (match and pop).

Example for S→aSb∣εS\rightarrow aSb\mid\varepsilon, input aabb: (q,aabb,S)⊢(q,aabb,aSb)⊢(q,abb,Sb)⊢(q,abb,aSbb)⊢(q,bb,Sbb)⊢(q,bb,bb)⊢(q,b,b)⊢(q,ε,ε)(q,aabb,S)\vdash(q,aabb,aSb)\vdash(q,abb,Sb)\vdash(q,abb,aSbb)\vdash(q,bb,Sbb)\vdash(q,bb,bb)\vdash(q,b,b)\vdash(q,\varepsilon,\varepsilon) ✔ — one leftmost derivation equals one PDA computation.

2.4 PDA → CFG (PYQ 2023-24, 7 marks)

PYQ: "Find the equivalent CFG of PDA P=({q0,q1},{a,b},{a,z0},δ,q0,z0) where δ(q0,a,z0)=(q0,az0), δ(q0,a,a)=(q1,aa), δ(q1,a,a)=(q1,ε), δ(q1,ε,z0)=(q1,ε)."

Use variables [qXp][qXp] meaning "starting at state qq with XX on top, the PDA can reach state pp having popped XX".

Construction rules:

  1. If δ(q,a,X)∋(p,ε)\delta(q,a,X)\ni(p,\varepsilon), add [qXp]→a[qXp]\rightarrow a.
  2. If δ(q,a,X)∋(p,Y1Y2…Yk)\delta(q,a,X)\ni(p,Y_1Y_2\dots Y_k), add [qXpk]→a[pY1p1][p1Y2p2]…[pk−1Ykpk][qXp_k]\rightarrow a[pY_1p_1][p_1Y_2p_2]\dots[p_{k-1}Y_kp_k] for all choices of intermediate states.
  3. If δ(q,ε,X)∋(p,ε)\delta(q,\varepsilon,X)\ni(p,\varepsilon), add [qXp]→ε[qXp]\rightarrow\varepsilon.

Applying them:

  • From δ(q1,a,a)=(q1,ε)\delta(q_1,a,a)=(q_1,\varepsilon): [q1aq1]→a[q_1aq_1]\rightarrow a.
  • From δ(q1,ε,z0)=(q1,ε)\delta(q_1,\varepsilon,z_0)=(q_1,\varepsilon): [q1z0q1]→ε[q_1z_0q_1]\rightarrow\varepsilon.
  • From δ(q0,a,a)=(q1,aa)\delta(q_0,a,a)=(q_1,aa): [q0aq1]→a[q1aq1][q1aq1]⇒aaa[q_0aq_1]\rightarrow a[q_1aq_1][q_1aq_1]\Rightarrow aaa.
  • From δ(q0,a,z0)=(q0,az0)\delta(q_0,a,z_0)=(q_0,az_0) with the final state chosen as q1q_1: [q0z0q1]→a[q0aq1][q1z0q1]⇒a⋅aaa⋅ε=aaaa[q_0z_0q_1]\rightarrow a[q_0aq_1][q_1z_0q_1]\Rightarrow a\cdot aaa\cdot\varepsilon = aaaa.

Equivalent CFG: S=[q0z0q1]→aaaaS=[q_0z_0q_1]\rightarrow aaaa (with supporting variables [q0aq1]→aaa[q_0aq_1]\rightarrow aaa, [q1aq1]→a[q_1aq_1]\rightarrow a, [q1z0q1]→ε[q_1z_0q_1]\rightarrow\varepsilon). Check by simulation: the PDA accepts only the string aaaa, as shown by pushing at the first a, switching at the second, and popping two as with the remaining two input symbols before ε-popping z0z_0.

2.5 Two-Stack PDA

PYQ (2025-26, 7 marks): "Design Two Stack Pushdown Automata for L={a^n b^n c^n | n≥1}." PYQ (2023-24, 2 marks): "Explain the concept of two stack PDA. Give an example of a language accepted by two stack PDA but not accepted by normal one stack PDA."

Design:

  1. Read the a block, pushing one marker X onto stack 1 for each a.
  2. Read the b block: pop one X from stack 1 per b (checking count of a = count of b), and simultaneously push one marker Y onto stack 2 per b.
  3. Read the c block: pop one Y from stack 2 per c.
  4. Accept if both stacks contain only their bottom markers at end of input.
Phase 1 (a's)        Phase 2 (b's)             Phase 3 (c's)
stack1: X X X        pop X per b              empty
stack2: z0           push Y per b             pop Y per c -> empty

Why one stack is not enough: a single stack cannot both verify a against b and retain the count for c; popping to match b destroys the information needed later. Two stacks give Turing-machine-level memory, so anbncna^nb^nc^n (a non-CFL) is accepted. Example answer for 2 marks: two-stack PDA accepts L={anbncn}L=\{a^nb^nc^n\}; a one-stack PDA cannot (pumping lemma for CFL shows it is not context-free).


3. Properties of Context-Free Languages

3.1 Pumping Lemma for CFL (predicted theory)

If LL is an infinite CFL, there is a pumping length pp such that any z∈Lz\in L with ∣z∣≥p|z|\ge p can be written z=uvwxyz=uvwxy with:

  1. ∣vx∣≥1|vx|\ge1 (something is pumped),
  2. ∣vwx∣≤p|vwx|\le p (the pumped region is local),
  3. uviwxiy∈Luv^iwx^iy\in L for all i≥0i\ge0.

Application template: choose z=apbpcpz=a^pb^pc^p; the short middle vwxvwx can cover at most two symbol types, so pumping changes one or two counts but not all three equally → contradiction; hence {anbncn}\{a^nb^nc^n\} is not context-free.

3.2 Closure Properties

Operation CFL closed? Reason
Union Yes New start S→S1∣S2S\rightarrow S_1\mid S_2
Concatenation Yes S→S1S2S\rightarrow S_1S_2
Kleene star Yes S→SS1∣εS\rightarrow SS_1\mid\varepsilon
Intersection No L1={anbncm}L_1=\{a^nb^nc^m\}, L2={ambncn}L_2=\{a^mb^nc^n\} intersect to anbncna^nb^nc^n
Complement No Would make CFL closed under intersection via De Morgan
Intersection with regular language Yes Product of PDA and DFA
Difference with regular language Yes L∖R=L∩R‾L\setminus R = L\cap\overline R, and R‾\overline R is regular

3.3 Decision Problems

Problem Decidable for CFL?
Is w∈L(G)w\in L(G)? Yes — CYK / Earley parser
Is L(G)=∅L(G)=\varnothing? Yes — find generating non-terminals
Is L(G)L(G) finite? Yes — detect a useful cycle
Is L(G1)=L(G2)L(G_1)=L(G_2)? No (equivalence undecidable for CFL)
Is L(G)=Σ∗L(G)=\Sigma^*? No

4. Exam-Ready and Practice Questions

Solved-question index

Question Section with full solution
Acceptance by final state vs empty stack (2025-26, 2m) §1.1
DPDA vs NPDA + PDA for anbna^nb^n (2023-24, 7m) §1.2
NPDA more powerful than DPDA (2025-26, 7m) §1.2
PDA for anb2na^nb^{2n} (predicted, 7m) §2.1
PDA for wwRww^R / palindromes (predicted, 7m ×2) §2.2
CFG from given PDA (2023-24, 7m) §2.4
Two-stack PDA for anbncna^nb^nc^n (2025-26, 7m; 2023-24, 2m) §2.5

Practice questions (label: practice, not real PYQs)

  1. Practice: Give the ID sequence of the anbna^nb^n PDA on aaabbb.
  2. Practice: Modify the palindrome PDA to accept odd-length palindromes too.
  3. Practice: Convert S→aSb∣abS\rightarrow aSb\mid ab into a PDA using the CFG→PDA algorithm.
  4. Practice: Show with the CFL pumping lemma that {ww∣w∈{a,b}∗}\{ww\mid w\in\{a,b\}^*\} is not context-free.
  5. Practice: Explain why DCFLs are closed under complement but CFLs are not.

5. Last-Night Survival Checklist (Unit 4 only)

  1. 7-tuple: (Q,Σ,Γ,δ,q0,z0,F)(Q,\Sigma,\Gamma,\delta,q_0,z_0,F); stack top is the leftmost symbol; write the bottom marker z0z_0 in every construction.
  2. Three constructions memorised: anbna^nb^n (deterministic), anb2na^nb^{2n} (push two, pop one), palindrome (guess middle).
  3. Two-stack argument: stack 1 checks a vs b, stack 2 checks b vs c; one stack cannot retain both counts.
  4. PDA→CFG variables: [qXp][qXp]; terminal rule from ε-pop, chained rule from multi-symbol push.
  5. Closure traps: CFL union/concat/star yes; intersection/complement no; intersection with a regular language yes.

Final PDA reminder: A PDA answer without an ID trace is incomplete. Show at least one accepted string step-by-step and one rejected string that fails because of a leftover stack symbol; this demonstrates that the construction really implements the counting condition.