Back to the 2023 paper

Module 2: Context-Free Languages (CFL) and PDA

20237m

Design a PDA (empty stack and final state both) for L={WWr∣W∈{a,b}∗}L = \{ WW^r \mid W \in \{a,b\}^* \}.

Worked SolutionAI Assisted

Answer

Language: L={WWr∣W∈{a,b}∗}L = \{ W W^r \mid W \in \{a,b\}^* \} — even-length palindromes over {a,b}\{a,b\}.

WrW^r = reverse of WW. Examples: ϵ\epsilon, aaaa, bbbb, abbaabba, baabbaab, aabbaaaabbaa, …


PDA Design (NPDA)

This language is not deterministic (NPDA needed) — the PDA must guess the midpoint.

PDA: P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F)

  • Q={q0,q1,q2}Q = \{q_0, q_1, q_2\}
    • q0q_0: reading first half, pushing symbols
    • q1q_1: reading second half, popping & matching
    • q2q_2: accept (final state)
  • Σ={a,b}\Sigma = \{a, b\}
  • Γ={a,b,Z0}\Gamma = \{a, b, Z_0\} (Z0Z_0 = bottom marker)
  • q0q_0 = start
  • F={q2}F = \{q_2\} (final state)
  • Empty stack also accepted: Z0Z_0 remains at bottom, all input symbols pushed then popped

Transition Function δ\delta

Current State Input Stack Top Action Next State Push/Pop
q0q_0 aa X∈{a,b,Z0}X \in \{a,b,Z_0\} Push aa q0q_0 aa
q0q_0 bb XX Push bb q0q_0 bb
q0q_0 ϵ\epsilon XX Guess midpoint, switch to pop q1q_1 ϵ\epsilon (no change)
q1q_1 aa aa Pop & match q1q_1 ϵ\epsilon (pop)
q1q_1 bb bb Pop & match q1q_1 ϵ\epsilon (pop)
q1q_1 ϵ\epsilon Z0Z_0 All matched, stack = Z0Z_0 q2q_2 ϵ\epsilon

Formally:

  • δ(q0,a,X)={(q0,aX)}\delta(q_0, a, X) = \{(q_0, aX)\} for X∈{a,b,Z0}X \in \{a,b,Z_0\}
  • δ(q0,b,X)={(q0,bX)}\delta(q_0, b, X) = \{(q_0, bX)\} for X∈{a,b,Z0}X \in \{a,b,Z_0\}
  • δ(q0,ϵ,X)={(q1,X)}\delta(q_0, \epsilon, X) = \{(q_1, X)\} for X∈{a,b,Z0}X \in \{a,b,Z_0\} (midpoint guess)
  • δ(q1,a,a)={(q1,ϵ)}\delta(q_1, a, a) = \{(q_1, \epsilon)\}
  • δ(q1,b,b)={(q1,ϵ)}\delta(q_1, b, b) = \{(q_1, \epsilon)\}
  • δ(q1,ϵ,Z0)={(q2,ϵ)}\delta(q_1, \epsilon, Z_0) = \{(q_2, \epsilon)\}

State Diagram

         a,b / X → aX,bX
      ┌───────────────────┐
      ▼                   │
  (q0)──ϵ, X/X──►(q1)──a/a:pop,b/b:pop──►(q1)
      │               │
      │               ϵ, Z0/ϵ
      │               ▼
      └──────────────►(q2)  [Accept]

How It Works (Example: W=abW=ab → abbaabba)

  1. Push phase (q0q_0):

    • Read a, push a: stack = a Z0
    • Read b, push b: stack = b a Z0
    • Guess midpoint (ϵ\epsilon): go to q1q_1, stack unchanged b a Z0
  2. Pop phase (q1q_1):

    • Read b, top=b → pop → stack = a Z0
    • Read a, top=a → pop → stack = Z0
    • Input exhausted, ϵ\epsilon-transition on Z0Z_0 → q2q_2
  3. Accept: q2∈Fq_2 \in F, or stack = Z0Z_0 (empty-stack acceptance).


Acceptance Modes

Mode Condition
Final State Reach q2∈Fq_2 \in F
Empty Stack After reading all input, stack = Z0Z_0 (all pushed symbols matched and popped, only Z0Z_0 remains)

Both equivalent here; PDA accepts by both.


Why Nondeterministic?

  • The ϵ\epsilon-transition from q0q_0 to q1q_1 guesses the midpoint.
  • For odd-length strings (not in LL), no guess works → rejects.
  • For WWrWW^r, correct guess at exact middle → accepts.
  • DPDA cannot guess midpoint deterministically → LL not DCFL.

Alternative: CFG

S→aSa∣bSb∣ϵS \rightarrow aSa \mid bSb \mid \epsilon generates LL.


Final Answer

NPDA with 3 states (q0,q1,q2q_0, q_1, q_2):

  • q0q_0: push a,ba,b onto stack while reading first half
  • ϵ\epsilon-transition: guess midpoint, switch to q1q_1
  • q1q_1: pop & match each input symbol with stack top
  • ϵ\epsilon on Z0Z_0: accept (q2q_2)
  • Accepts by final state q2q_2 and empty stack (Z0Z_0 only).

Similar questions

AI LearningWeb Platform

Coursify — Personal AI Course Generator

Struggling with a difficult subject or concept? Turn any engineering topic into a structured course with interactive lessons, quizzes, and research-backed explanations.