1. Gyanastra
  2. Theory of Computation
  3. Pushdown Automata
  4. PDA for a^n b^m c^n
Free lesson, no account needed 7 min read 14 practice questions after it
Asked in past papers4 times
  • GATE CSE2026202520232016

Theory of Computation · Pushdown Automata

PDA for a^n b^m c^n

Overview

A pushdown automaton (PDA) accepts the language L={anbmcn∣n,m≥1}L = \{ a^n b^m c^n \mid n, m \ge 1 \}.
The a's are counted on the stack and matched one-to-one against the c's, while the b's are ignored entirely; separate states are required to remember that the b-block has been passed and the c-block has begun.

Key concepts

  • Language: L={anbmcn∣n,m≥1}L = \{ a^n b^m c^n \mid n, m \ge 1 \} — the a's must be matched (counted) against the c's, while the b's are simply ignored: they are neither pushed nor popped.
  • Stack alphabet: {A,Z0}\{A, Z_0\}, where Z0Z_0 is the bottom marker. Push AA for each a, pop AA for each c, never touch the stack for b's.
  • Phase 1 — push all a's: start in the initial state with Z0Z_0 on the stack; on reading an a, push AA; repeat for every a, so after the a-block the stack holds AnZ0A^n Z_0.
  • Phase 2 — skip the b's: on seeing the first b with AA on top, leave the stack unchanged (no push, no pop) and move to the b-skipping state; in that state any number of further b's may be read with AA still on top, staying in the same state with the stack unchanged.
  • Phase 3 — match c's against a's: on reading a c with AA on top, replace AA by ε\varepsilon (pop it); keep doing this for every c, popping one AA per c.
  • Phase 4 — acceptance: when the entire input is over and the top of the stack is Z0Z_0 (all c's consumed and all AA's popped), leave Z0Z_0 as it is and accept.
  • Why three states (plus the initial state) are compulsory:
    • After seeing a b, the machine must move to a new state to remember "a b has been seen, so no a is allowed after this."
    • After seeing a c, the machine must move to a new state to remember "a c has been seen, so no b is allowed after this."
    • Popping cannot be done in the same state: if popping were done in the same state, a b could appear after a c and there would be confusion about which symbol is being matched. Hence the separate states.

How it works

  1. Start in the initial state with Z0Z_0 (bottom marker) on the stack.
  2. On reading an a, push AA onto the stack; repeat for every a, so after the a-block the stack holds AnZ0A^n Z_0.
  3. On seeing the first b with AA on top of the stack, leave the stack unchanged (do not push, do not pop) and move to the next state.
  4. In this state, any number of further b's may be read with AA still on top; again leave the stack as it is and move to the next state.
  5. In the c-matching state, on reading a c with AA on top of the stack, replace AA by ε\varepsilon (pop it); keep doing this for every c, popping one AA per c.
  6. When the entire input is over and the top of the stack is Z0Z_0 (all c's consumed and all AA's popped), leave Z0Z_0 as it is and accept.
Figure. PDA for a^n b^m c^n

Formulas

  • L={anbmcn∣n,m≥1}L = \{ a^n b^m c^n \mid n, m \ge 1 \} — the language accepted by the PDA.
  • Stack after the a-block: AnZ0A^n Z_0 — one AA pushed per a, above the bottom marker.
  • Stack operation for a: push AA.
  • Stack operation for b: no change (leave AA).
  • Stack operation for c: pop AA (replace AA by ε\varepsilon).
  • Acceptance condition: input exhausted and stack top =Z0= Z_0.

Worked examples

Transition list (state, input, stack top →\to new state, stack operation). Let q0q_0 = initial, q1q_1 = first b seen, q2q_2 = b's being skipped, q3q_3 = c-matching, qfq_f = accepting.

StateInputStack topNew stateStack operation
q0q_0aZ0Z_0q0q_0push AA
q0q_0aAAq0q_0push AA
q0q_0bAAq1q_1no change (leave AA)
q1q_1bAAq2q_2no change (leave AA)
q2q_2bAAq2q_2no change (leave AA)
q2q_2cAAq3q_3pop AA (replace by ε\varepsilon)
q3q_3cAAq3q_3pop AA (replace by ε\varepsilon)
q3q_3— (input over)Z0Z_0qfq_fleave Z0Z_0, accept

Trace for the string a a b b c ca\,a\,b\,b\,c\,c (n=2n = 2, m=2m = 2):

  1. q0q_0, read a, stack top Z0Z_0 →\to push AA. Stack: A Z0A\,Z_0.
  2. q0q_0, read a, stack top AA →\to push AA. Stack: A A Z0A\,A\,Z_0.
  3. q0q_0, read b, stack top AA →\to go to q1q_1, stack unchanged. Stack: A A Z0A\,A\,Z_0.
  4. q1q_1, read b, stack top AA →\to go to q2q_2, stack unchanged. Stack: A A Z0A\,A\,Z_0.
  5. q2q_2, read c, stack top AA →\to go to q3q_3, pop AA. Stack: A Z0A\,Z_0.
  6. q3q_3, read c, stack top AA →\to stay in q3q_3, pop AA. Stack: Z0Z_0.
  7. q3q_3, input over, stack top Z0Z_0 →\to accept.

Revision summary

  • The a's are counted on the stack and matched one-to-one with the c's; the b's are ignored entirely — no push, no pop, no counting.
  • The stack alphabet is {A,Z0}\{A, Z_0\}: push AA for each a, pop AA for each c, never touch the stack for b's.
  • Two working states after the initial state suffice: one to skip the b's (entered on the first b, looping on further b's) and one to match c's — because the PDA must forbid an a after a b and forbid a b after a c.
  • Popping in the same state as b-skipping would allow a b after a c and cause confusion, so c-matching must be in its own state.
  • Acceptance happens only when the input is exhausted and the stack top is Z0Z_0 (all a's matched by c's).
  • The language requires n,m≥1n, m \ge 1: at least one a, one b and one c.

Previous-year exam insights

  • A language is context-free iff some PDA accepts it; a PDA accepts by final state or by empty stack, and the two acceptance modes are equivalent in power (each can be converted to the other).
    Asked inGATE 2016GATE 2023
  • Pumping lemma for CFLs: for any CFL there is a pumping length p such that every string of length ≥ p can be written uvwxy with |vwx| ≤ p, |vx| ≥ 1, and uviuv^{i} wxiwx^{i} y in L for all i ≥ 0;
    use it to prove languages like {ama^{m} bmb^{m} cmc^{m}} or {ama^{m} bmb^{m} cm+nc^{m+n}} non-context-free.
    Asked inGATE 2025
  • A language of the form {aia^{i} bjb^{j} ckc^{k} dld^{l}} with a single linear constraint among the exponents (e.g. i = k, or i + k = j + l) is context-free, while two independent equality constraints (e.g. i = k and j = l simultaneously, or i = j = k) generally make it non-context-free.
    Asked inGATE 2026
  • Deterministic PDAs are strictly weaker than nondeterministic PDAs; languages such as {ana^{n} bnb^{n}} union {ana^{n} b2nb^{2n}} or even-length palindromes are context-free but not accepted by any DPDA.
    Asked inGATE 2016
  • A PDA with a single stack cannot count two independent quantities; matching one symbol block against another (one push/pop pair) is fine, but matching three blocks pairwise requires more than one stack.
    Asked inGATE 2025GATE 2026
  • Standard PDA transition notation: a transition (u, c, X) → (v, gamma) reads input c (or epsilon), requires stack top X, moves to v and replaces X by the string gamma (gamma = epsilon means pop, gamma = X means no change, gamma = XA means push A).
    Asked inGATE 2023
Pages 1–2 of 2