Constructing a PDA for L={uawbu,w{a,b}, u=w}L=\{uawb\mid u,w\in\{a,b\}^*,\ |u|=|w|\}

Constructing a PDA for L={uawbu,w{a,b}, u=w}L=\{uawb\mid u,w\in\{a,b\}^*,\ |u|=|w|\}

Verified Sources
Sep 12, 2026

PDA basics: formal definition and how to construct one

We want a push-down automaton (PDA) that recognizes the language L={uawbu,w{a,b}, u=w}.L=\{uawb\mid u,w\in\{a,b\}^*,\ |u|=|w|\}. So each accepted string has the form:

  1. a prefix uu over {a,b}\{a,b\},
  2. then one symbol aa,
  3. then a string ww over {a,b}\{a,b\},
  4. then one symbol bb,

with the constraint that the length of uu equals the length of ww.

Key terms: pushdown automaton , stack alphabet , stack discipline , configuration.

High-level construction idea

Use the stack to count (in unary) how many symbols are in uu.

  • While reading the prefix uu (unknown length), push one stack symbol per read input symbol.
  • When the PDA decides it has reached the middle symbol aa, it switches to a “matching” phase where it reads the ww part and pops one stack symbol per symbol of ww.
  • Finally, after ww is fully matched (stack empty), it must read the last symbol bb and accept.

This works because “u=w|u|=|w|” is exactly the condition “we popped once for each symbol pushed.”

The PDA will be nondeterministic about where uu ends (i.e., where it decides to read the distinguished middle aa).

Pro Tip: nondeterministically guess the split

Since the PDA cannot know u|u| ahead of time, it uses nondeterminism to guess which input symbol is the fixed middle 'a' (the boundary between uu and ww).

Formal PDA definition

We construct a PDA M=(Q,Σ,Γ,δ,q0,Z0,F)M=(Q,\Sigma,\Gamma,\delta,q_0,Z_0,F) that accepts by final state.

  • Σ={a,b}\Sigma=\{a,b\}
  • Γ={Z0,X}\Gamma=\{Z_0,X\} where:
    • Z0Z_0 is the initial stack bottom marker
    • XX is used as one “unit” per symbol of uu
  • Q={qpush,qpop,qaccept}Q=\{q_{\text{push}},q_{\text{pop}},q_{\text{accept}}\}
  • Start state: q0=qpushq_0=q_{\text{push}}
  • Initial stack symbol: Z0Z_0
  • Final states: F={qaccept}F=\{q_{\text{accept}}\}

We define δ\delta by transitions (below). Notation: δ(q,input,top)(q,stack action)\delta(q,\,\text{input},\,\text{top}) \ni (q',\,\text{stack action}).

PDA behavior (push $u$, pop $w$, then read the ending $b$)

  1. 1
    Step 1

    In state qpushq_{\text{push}}, for each input symbol aa or bb (while still building uu), push one XX onto the stack for that symbol.

  2. 2
    Step 2

    At some point, nondeterministically choose to consume an input aa as the distinguished middle symbol. Then switch to qpopq_{\text{pop}}.

  3. 3
    Step 3

    In qpopq_{\text{pop}}, for each input symbol aa or bb consumed as part of ww, pop exactly one XX from the stack.

  4. 4
    Step 4

    When the next symbol after matching ww is processed, require that the stack top is Z0Z_0 (meaning all pushed units have been popped).

  5. 5
    Step 5

    With stack top Z0Z_0, consume the final input symbol bb and transition to qacceptq_{\text{accept}}.

Transition function δ\delta

We now list transitions that implement the above behavior.

1) Push phase: qpushq_{\text{push}}

For s{a,b}s\in\{a,b\}:

  • δ(qpush,s,Z0)(qpush,XZ0)\delta(q_{\text{push}}, s, Z_0)\ni (q_{\text{push}}, XZ_0)
  • δ(qpush,s,X)(qpush,XX)\delta(q_{\text{push}}, s, X)\ni (q_{\text{push}}, XX)

These transitions push one XX for every consumed symbol of uu.

2) Guess the middle aa: switch to pop phase

On reading the distinguished middle symbol aa, do:

  • δ(qpush,a,Z0)(qpop,Z0)\delta(q_{\text{push}}, a, Z_0)\ni (q_{\text{pop}}, Z_0) (allowed only if uu could be empty)
  • δ(qpush,a,X)(qpop,X)\delta(q_{\text{push}}, a, X)\ni (q_{\text{pop}}, X)

Interpretation: we consume that aa but do not pop anything at the boundary; we only pop during the ww phase.

3) Pop phase: match length of ww to length of uu

For s{a,b}s\in\{a,b\}:

  • δ(qpop,s,X)(qpop,ϵ)\delta(q_{\text{pop}}, s, X)\ni (q_{\text{pop}}, \epsilon)

This pops one XX per symbol of ww.

4) After ww, require stack empty (top is Z0Z_0) and read final bb

  • δ(qpop,b,Z0)(qaccept,Z0)\delta(q_{\text{pop}}, b, Z_0)\ni (q_{\text{accept}}, Z_0)

This ensures:

  • we have consumed the final bb,
  • and the stack has no remaining XX (so u=w|u|=|w|).

Common pitfall: accepting too early

If you allowed acceptance whenever the stack becomes empty without consuming the final 'b', you could accept strings not of the exact form uawbu a w b. The transition that consumes 'b' from qpopq_{\text{pop}} with stack top Z0Z_0 prevents that.

Why this PDA recognizes exactly LL

Soundness (\Rightarrow):
Assume MM accepts some string. Acceptance requires reaching qacceptq_{\text{accept}} via the transition that consumes the final bb when the stack top is Z0Z_0. That implies:

  • all XX pushed during reading uu have been popped during reading ww, so u=w|u|=|w|,
  • exactly one middle symbol aa was consumed to switch from push to pop,
  • and the last consumed symbol is bb. Thus the input is uawbuawb with u,w{a,b}u,w\in\{a,b\}^* and u=w|u|=|w|.

Completeness (\Leftarrow):
Given any string uawbuawb with u=w|u|=|w|, the PDA can:

  • push one XX per symbol in uu,
  • nondeterministically choose the correct position of the middle aa (the one shown),
  • then pop one XX per symbol of ww,
  • and finally read the ending bb when stack top is Z0Z_0. So the PDA has an accepting run.

Key terms: nondeterminism , acceptance by final state , unary counting with stack.

Visual summary (state machine view)

Length constraint enforced by stack push/pop

Each symbol of uu contributes one XX; each symbol of ww pops one XX. Acceptance requires the stack to be empty before consuming the final bb.

FAQ / Edge cases

Knowledge Check

Question 1 of 4
Q1Single choice

In the construction, what does the PDA push for each symbol read during the uu-prefix?