Constructing a PDA for L={a2nbcn0}L=\{a^{2n}bc\mid n\ge 0\} (Final State and Empty Stack)

Constructing a PDA for L={a2nbcn0}L=\{a^{2n}bc\mid n\ge 0\} (Final State and Empty Stack)

Verified Sources
Sep 12, 2026

We want a pushdown automaton (PDA) for the context-free language
L={a2nbcn0}.L=\{a^{2n}bc\mid n\ge 0\}. Intuitively: read an even number of aa’s (possibly 00), then match a single bb, then a single cc, and reject anything else.

We will build two equivalent machines:

  • one that accepts by final state (using an accepting state),
  • one that accepts by empty stack (by popping the entire stack).

Key notions:

  • pushdown automaton (PDA)
  • acceptance by final state
  • acceptance by empty stack
  • instantaneous description

Formally, acceptance modes are defined using the PDA’s transition relation and either an accepting state set FF (final state) or the condition that the stack becomes empty (empty stack). 2

Footnotes

  1. Pushdown automaton - Definitions and acceptance modes (final state vs empty stack) for PDAs.

  2. Lecture 14 – Pushdown Automata (PDA) - COSE215 - Formal definitions of acceptance by final states and by empty stacks with illustrative PDA transition examples.

PDA Acceptance by Final State and Empty Stack (overview)

Strategy to recognize a2nbca^{2n}bc

Use the stack to “count pairs of aa’s”:

  • For each input symbol aa, push an AA onto the stack.
  • When bb arrives, pop exactly one AA per previously seen aa; but because we need even many aa’s, we can enforce pairing by pushing per aa and requiring that after processing the bb we can only proceed if exactly the “pairing structure” allows an acceptance.

A simpler, deterministic-feeling construction is:

  • while reading aa’s, push one AA for each aa,
  • when bb is read, pop two AA’s per “unit step” is awkward because bb occurs only once. Instead, we implement a standard technique: push two markers per pair of aa’s by consuming aa’s in pairs using an intermediate state.

Concretely:

  • State q0q_0: expect the first aa of a pair.
  • State q1q_1: after seeing one aa of a pair, expect the second aa and then push a marker PP representing one completed pair.
  • After any number of completed pairs, transition to the bb-reading state, then to cc-reading state, and then accept (or empty the stack, depending on the acceptance mode).

Build NPDA (nondeterministic PDA) transitions for $a^{2n}bc$

  1. 1
    Step 1

    Start with stack bottom symbol ZZ; initially the machine has ZZ on the stack (used to detect when the stack should be empty at the end for empty-stack acceptance).

  2. 2
    Step 2

    Use two states so that one aa is stored implicitly (by moving states), and the second aa causes one stack push of a pair-marker PP.

  3. 3
    Step 3

    From the state reached after completing a pair (or from the n=0n=0 path), read a single bb and move to cc-reading.

  4. 4
    Step 4

    From the cc-reading state, either (final-state) enter an accepting state, or (empty-stack) pop the remaining pair-markers and ZZ so that the stack becomes empty.

  5. 5
    Step 5

    Ensure there are no transitions for any other symbol sequences (e.g., another bb, or an extra aa after bb).

PDA #1: Acceptance by final state

We define Mf=(Q,Σ,Γ,δ,q0,Z,F)M_f=(Q,\Sigma,\Gamma,\delta,q_0,Z,F) (a 7-tuple with accepting states FF).

Let:

  • Q={q0,q1,q2,qb,qc,qacc}Q=\{q_0,q_1,q_2,q_b,q_c,q_{\text{acc}}\}
  • Σ={a,b,c}\Sigma=\{a,b,c\}
  • Γ={Z,P}\Gamma=\{Z,P\}
  • start state q0q_0
  • initial stack symbol ZZ
  • accepting states F={qacc}F=\{q_{\text{acc}}\}

Interpretation:

  • q0q_0: either begin a pair by reading the first aa, or if no aa’s exist (n=0n=0), go directly to reading bb.
  • q1q_1: we have read exactly one aa of the current pair; now we must read the second aa to complete the pair and push PP.
  • q2q_2: we have completed n1n\ge 1 pairs; now either start another pair or go to read bb.
  • qbq_b: read exactly one bb.
  • qcq_c: read exactly one cc.
  • qaccq_{\text{acc}}: accept.

Transition function δ\delta (written as: δ(q,input or ε,stack top)(q,stack action)\delta(q,\,\text{input or }\varepsilon,\,\text{stack top})\ni (q',\,\text{stack action}))

Key transitions:

  1. Pairing aa’s (push one PP per pair aaaa)
  • δ(q0,a,Z)(q1,Z)\delta(q_0,a,Z)\ni(q_1,Z)

  • δ(q0,a,P)(q1,P)\delta(q_0,a,P)\ni(q_1,P)
    (allow reading first aa whether stack top is ZZ or PP)

  • δ(q1,a,Z)(q2,PZ)\delta(q_1,a,Z)\ni(q_2, PZ)

  • δ(q1,a,P)(q2,PP)\delta(q_1,a,P)\ni(q_2, PP)
    (upon second aa, push PP on top)

  1. Move to reading bb after finishing nn pairs
  • For n=0n=0 (no aa read): from q0q_0, read bb with stack unchanged:

    • δ(q0,b,Z)(qb,Z)\delta(q_0,b,Z)\ni(q_b,Z)
    • δ(q0,b,P)(qb,P)\delta(q_0,b,P)\ni(q_b,P)
      (the PP-case won’t be reachable for n=0n=0 in a correct run, but it keeps the definition total)
  • For n1n\ge 1: from q2q_2, read bb without changing stack:

    • δ(q2,b,Z)(qb,Z)\delta(q_2,b,Z)\ni(q_b,Z)
    • δ(q2,b,P)(qb,P)\delta(q_2,b,P)\ni(q_b,P)
  1. Read cc and accept (final-state)
  • δ(qb,c,Z)(qacc,Z)\delta(q_b,c,Z)\ni(q_{\text{acc}},Z)
  • δ(qb,c,P)(qacc,P)\delta(q_b,c,P)\ni(q_{\text{acc}},P)
  1. No other transitions allow continuation In particular, we do not provide transitions for:
  • reading aa in qbq_b or qcq_c,
  • reading bb in qcq_c,
  • reading any symbol after reaching qaccq_{\text{acc}}.

Why this works (sketch)

  • Each completed pair aaaa pushes exactly one PP, so runs with an odd number of aa’s get stuck in q1q_1 waiting for the second aa.
  • After all pairs, exactly one bb is consumed, then exactly one cc is consumed.
  • For n=0n=0, the machine reads bb immediately from q0q_0, then cc.

This recognizes precisely a2nbca^{2n}bc by reaching qaccq_{\text{acc}} after consuming the input. 2

Footnotes

  1. Pushdown automaton - Definitions and acceptance modes (final state vs empty stack) for PDAs. 2

  2. Lecture 14 – Pushdown Automata (PDA) - COSE215 - Formal definitions of acceptance by final states and by empty stacks with illustrative PDA transition examples.

PDA #2: Acceptance by empty stack

Now we build a PDA MeM_e that accepts by empty stack, i.e., after consuming the entire input it reaches a configuration with empty stack (stack content ε\varepsilon). 2

Let:

  • Q={q0,q1,q2,qb,qc,qpop}Q'=\{q_0,q_1,q_2,q_b,q_c,q_{\text{pop}}\}
  • Σ={a,b,c}\Sigma=\{a,b,c\}
  • Γ={Z,P}\Gamma'=\{Z,P\}
  • start state q0q_0
  • initial stack symbol ZZ
  • no accepting states needed for empty-stack acceptance (acceptance is by stack emptiness).

Transitions mirror the pair-building from MfM_f, but after reading cc we pop all stack symbols until empty.

  1. Pair aa’s (same logic)
  • δ(q0,a,Z)(q1,Z)\delta(q_0,a,Z)\ni(q_1,Z)
  • δ(q0,a,P)(q1,P)\delta(q_0,a,P)\ni(q_1,P)
  • δ(q1,a,Z)(q2,PZ)\delta(q_1,a,Z)\ni(q_2,PZ)
  • δ(q1,a,P)(q2,PP)\delta(q_1,a,P)\ni(q_2,PP)
  1. After pairs, read exactly one bb, then exactly one cc
  • n=0n=0 path:

    • δ(q0,b,Z)(qb,Z)\delta(q_0,b,Z)\ni(q_b,Z)
  • n1n\ge 1 path:

    • δ(q2,b,Z)(qb,Z)\delta(q_2,b,Z)\ni(q_b,Z)
    • δ(q2,b,P)(qb,P)\delta(q_2,b,P)\ni(q_b,P)
  • δ(qb,c,Z)(qc,Z)\delta(q_b,c,Z)\ni(q_c,Z)

  • δ(qb,c,P)(qc,P)\delta(q_b,c,P)\ni(q_c,P)

  1. Empty the stack after consuming cc Let qcq_c be the state that starts popping:
  • δ(qc,ε,P)(qc,ε)\delta(q_c,\varepsilon,P)\ni(q_c,\varepsilon)
    (pop a PP)
  • δ(qc,ε,Z)(qpop,ε)\delta(q_c,\varepsilon,Z)\ni(q_{\text{pop}},\varepsilon)
    (pop bottom symbol to truly empty the stack)
  • No further input should be consumed after this; because the stack becomes empty, acceptance-by-empty-stack is satisfied immediately when the computation ends.

Thus, for every string a2nbca^{2n}bc, the machine pushes one PP per aaaa pair, consumes bb then cc, and then empties the stack; for any string not in the language, it fails to reach empty stack. 2

Footnotes

  1. Pushdown automaton - Definitions and acceptance modes (final state vs empty stack) for PDAs. 2

  2. Lecture 14 – Pushdown Automata (PDA) - COSE215 - Formal definitions of acceptance by final states and by empty stacks with illustrative PDA transition examples. 2 3 4

Visual transition logic (pairing aa’s)

type="tip" title="Pro Tip" content="When a language constrains parity (like a2na^{2n}), model the parity by consuming input in fixed-size chunks (here, pairs aaaa). Using two states (q0q1q2q_0\to q_1 \to q_2) is a clean way to guarantee that odd-length aa-prefixes get stuck."

type="warning" title="Common Mistake" content="Don’t try to “count evenness” by pushing on every aa and popping only once at the end—because there is only one bb and one cc, you must structure the PDA so the aa-processing phase enforces the even constraint before transitioning to the fixed middle/end symbols."

Acceptance mechanism comparison

Same language, different acceptance condition.

Correctness notes & edge cases

How the PDA processes input

Pair up $a$’s

Phase 1

Use q0q1q2q_0\to q_1\to q_2 to ensure aa’s come in aaaa pairs; push one PP per pair."

Match the single $b$

Phase 2

From either q0q_0 (for n=0n=0) or q2q_2 (for n1n\ge1), read exactly one bb."

Match the single $c$

Phase 3

From qbq_b, read exactly one cc."

Accept

Phase 4

Final-state PDA enters qaccq_{acc}; empty-stack PDA pops PP’s and ZZ until the stack is empty."

Knowledge Check

Question 1 of 3
Q1Single choice

In the PDA construction for L={a2nbc}L=\{a^{2n}bc\}, which structural idea enforces that the number of aa’s is even?