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

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

Verified Sources
Sep 12, 2026

We want a push-down automaton (PDA) that recognizes strings of the form [ u a w b \quad\text{where } u,w\in{a,b}^*\text{ and }|u|=|w|. ] This is a context-free constraint: the PDA uses its stack to “pair” each symbol of uu with exactly one symbol of ww (in reverse order) and then verifies the pairing completes exactly when the input has consumed ww. A PDA is formally a 7-tuple (Q,Σ,Γ,δ,q0,Z,F)(Q,\Sigma,\Gamma,\delta,q_0,Z,F) with transitions that read an input symbol (or ε\varepsilon) and replace the stack top. It accepts either by reaching a final state or by emptying the stack. We will use final-state acceptance. 2

Key idea:

  • Read and push the whole uu onto the stack (one stack symbol per input symbol of uu).
  • When the middle delimiter aa is read, switch to a phase where we read exactly ww while popping one stack symbol per symbol of ww.
  • Require that the stack is back to the bottom marker exactly when the next delimiter bb is read.
  • Accept only if uu and ww had equal length.

We can view the workflow as a 3-phase PDA:

Keywords: pushdown automaton , stack , bottom-of-stack marker , [ε\varepsilon-transition]{def="A PDA move that consumes no input symbol"}.

Footnotes

  1. Introduction of Pushdown Automata - GeeksforGeeks - Defines PDAs, stack-based recognition of context-free languages, and formal PDA notion.

  2. Pushdown automaton (Wikipedia) - Covers formal PDA definition and acceptance by final state or empty stack, plus transition concepts.

PDA Example: {0^n 1^n} (Stack pairing intuition)

Formal language restatement

Let Σ={a,b}\Sigma=\{a,b\}. The language is [ L={, u a w b \mid u\in\Sigma^,\ w\in\Sigma^,\ |u|=|w| ,}. ] So any accepted string has:

  1. An aa delimiter at position u+1|u|+1,
  2. A bb delimiter at the end (right after ww),
  3. The number of symbols in uu equals the number of symbols in ww.

This “equal-length” property is exactly what the stack can enforce: push u|u| items; pop one per symbol of ww; ensure the stack returns to its initial bottom marker when ww ends.

Acceptance by final state is described in standard PDA definitions.

Keywords: context-free language , NPDA , accepting configuration.

Footnotes

  1. Pushdown automaton (Wikipedia) - Covers formal PDA definition and acceptance by final state or empty stack, plus transition concepts.

PDA design lifecycle (constructive approach)

Push $u$

Phase 1

Read an arbitrary prefix u{a,b}u\in\{a,b\}^* and push each symbol."

Guess the middle delimiter

Phase 2

Use nondeterminism to decide where the delimiter 'a' occurs."

Pop while reading $w$

Phase 3

For each symbol in $w, pop exactly one stack symbol."

Match trailing 'b' and accept

Phase 4

Accept only if stack is back to bottom marker when 'b' is read."

PDA MM for LL

We present an NPDA that accepts by final state.

Construction

Let

  • Q={qpush,qpop,qf}Q=\{q_{\text{push}},q_{\text{pop}},q_f\}
  • Σ={a,b}\Sigma=\{a,b\}
  • Γ={A,B,Z}\Gamma=\{A,B,Z\} where:
    • ZZ is the bottom-of-stack marker,
    • AA and BB encode symbols pushed from the input (encoding details are arbitrary but explicit here).
  • Start state q0=qpushq_0=q_{\text{push}}
  • Initial stack symbol ZZ
  • Accepting states F={qf}F=\{q_f\}

We will include nondeterminism in when we decide to consume the middle delimiter aa. Concretely, in the pushing phase, on reading an aa we may either:

  • push it as part of uu, or
  • treat it as the delimiter aa that starts the ww-region.

Transition function δ\delta

We use the standard PDA transition notation: [ \delta(q, x, \text{top}) \ni (q', \text{stack-replacement}) ] meaning: in state qq, reading input symbol xx (or ε\varepsilon), if the stack top is top\text{top}, then pop it and push stack-replacement\text{stack-replacement} (possibly ε\varepsilon), move to qq'.

(1) Pushing phase: read uu and push everything

In state qpushq_{\text{push}}:

  • On input aa or bb, push an encoded symbol and stay in qpushq_{\text{push}}: [ \delta(q_{\text{push}}, a, Z)\ni(q_{\text{push}}, AZ),\quad \delta(q_{\text{push}}, a, A)\ni(q_{\text{push}}, AA),\quad \delta(q_{\text{push}}, a, B)\ni(q_{\text{push}}, AB) ] [ \delta(q_{\text{push}}, b, Z)\ni(q_{\text{push}}, BZ),\quad \delta(q_{\text{push}}, b, A)\ni(q_{\text{push}}, BA),\quad \delta(q_{\text{push}}, b, B)\ni(q_{\text{push}}, BB) ]

Additionally, nondeterministically choose the delimiter:

  • If the next symbol is aa and the current top is anything, we may instead consume that aa as the middle delimiter and switch to the popping phase without changing the stack: [ \delta(q_{\text{push}}, a, Z)\ni(q_{\text{pop}}, Z),\quad \delta(q_{\text{push}}, a, A)\ni(q_{\text{pop}}, A),\quad \delta(q_{\text{push}}, a, B)\ni(q_{\text{pop}}, B) ]

This nondeterminism ensures we can select the correct aa that is exactly after uu.

(2) Popping phase: read ww while popping exactly one per symbol

In state qpopq_{\text{pop}}:

  • For each input symbol in {a,b}\{a,b\}, pop one stack symbol (top can be AA, BB), and stay in qpopq_{\text{pop}}: [ \delta(q_{\text{pop}}, a, A)\ni(q_{\text{pop}}, \varepsilon),\quad \delta(q_{\text{pop}}, a, B)\ni(q_{\text{pop}}, \varepsilon) ] [ \delta(q_{\text{pop}}, b, A)\ni(q_{\text{pop}}, \varepsilon),\quad \delta(q_{\text{pop}}, b, B)\ni(q_{\text{pop}}, \varepsilon) ]

We do not allow aa or bb to be consumed when the stack top is ZZ, because that would mean ww continues even though u|u| is exhausted. (This is what enforces u=w|u|=|w|.)

(3) Final delimiter: read trailing bb and accept iff stack is back at ZZ

After popping exactly u|u| symbols, the stack top will be ZZ. Then: [ \delta(q_{\text{pop}}, b, Z)\ni(q_f, Z) ] and we accept in qfq_f after consuming the final bb.

This enforces the input ends right after the delimiter bb and that ww had exactly length u|u|.

Keywords: NPDA nondeterminism , LIFO , stack encoding.

Run of $M$ on a string $uaw b$ (acceptance proof idea)

  1. 1
    Step 1

    While in qpushq_{\text{push}}, for each symbol of uu read, push a corresponding marker onto the stack. After reading uu, the stack height equals u|u| plus the bottom marker.

  2. 2
    Step 2

    Use the nondeterministic delimiter transition to read the specific aa that is immediately after uu and switch to qpopq_{\text{pop}} without altering the stack.

  3. 3
    Step 3

    For each symbol of ww, read that symbol in qpopq_{\text{pop}} and pop exactly one stack symbol. After reading all of ww, exactly w|w| pops have occurred.

  4. 4
    Step 4

    If u=w|u|=|w|, then the stack will be back to the bottom marker ZZ when ww ends; then the next input symbol must be the trailing bb.

  5. 5
    Step 5

    On reading the trailing bb with stack top ZZ, take the transition to qfq_f. This yields acceptance; otherwise no transition exists and the branch rejects.

  6. 6
    Step 6

    If w<u|w|<|u|, then at input end the stack is not back to ZZ (so no accepting move is possible). If w>u|w|>|u|, you eventually try to read symbols in qpopq_{\text{pop}} while the stack top is ZZ; no transition exists for that, so the branch dies.

Pro Tip: Use nondeterminism to locate the delimiter

Because uu can be any length, the PDA cannot know in advance which aa is the delimiter. The transitions in qpushq_{\text{push}} intentionally allow both pushing that aa into uu or treating it as the middle delimiter.

Do not allow popping-phase moves when top is $Z$

In qpopq_{\text{pop}}, forbid transitions that would read extra symbols of ww when the stack top is ZZ. This is exactly what enforces u=w|u|=|w|.

Why this PDA recognizes exactly LL

Soundness (MM accepts only strings in LL):

  • The only way to leave qpushq_{\text{push}} to qpopq_{\text{pop}} is by consuming an input aa as the middle delimiter.
  • The PDA can only consume symbols in qpopq_{\text{pop}} by popping stack symbols; hence, the number of consumed symbols of ww cannot exceed the number of pushed symbols from uu.
  • The only way to enter acceptance qfq_f is by consuming a trailing bb while the stack top is ZZ, meaning all pushes from uu were popped exactly—so u=w|u|=|w|. Therefore, any accepted string must be of the form uawbuawb with u=w|u|=|w|.

Completeness (MM accepts every string in LL):

  • Given any uawbuawb with u=w|u|=|w|, there exists a computation branch where the PDA:
    1. pushes exactly the characters of uu,
    2. uses the delimiter transition on the correct aa after uu,
    3. pops once per character of ww,
    4. finally consumes the trailing bb when the stack is back to ZZ. This branch reaches qfq_f and accepts.

Keywords: soundness , completeness.

Common variations and checks

Length relationship enforced by the stack

Stack height after pushing equals u|u|; each symbol of ww decreases it by 1.

Knowledge Check

Question 1 of 4
Q1Single choice

Which action enforces the condition u=w|u|=|w|?