Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 36

PDA Execution and Stack Trace

Recommended resources

Hello! Welcome back to our study of Pushdown Automata.

In our last lesson, we formally defined the Pushdown Automaton (PDA) as a 7-tuple, (Q, Σ, Γ, δ, q₀, Z₀, F). We established that its key enhancement over a finite automaton is the addition of a stack, which is governed by a more powerful transition function, δ.

Today, we bring this static definition to life. Our goal is to learn how to trace the execution of a PDA on an input string, including all its stack manipulations. This will allow us to determine whether a string is accepted or rejected by the machine. We will explore both an intuitive, graphical approach and a more formal method using Instantaneous Descriptions, which will satisfy both the need for intuition and the mathematical rigor you appreciate.


1. Visualizing PDA Execution: The Graphical Notation

Just as with finite automata, we can represent PDAs with state transition diagrams. However, the transitions are more complex. A transition in a PDA depends not just on the input symbol, but also on the symbol at the top of the stack.

The common notation for a PDA transition is:
a, b / c

This means:

  • a: The input symbol to be read from the tape. This can be ε for a transition that doesn't consume input.
  • b: The symbol that must be at the top of the stack. The transition can only be taken if the top of the stack is b. This symbol is popped from the stack.
  • c: The string of symbols to be pushed onto the stack. If c is ε, nothing is pushed, resulting in a pure pop operation.

The video below explains this graphical notation and then provides an excellent step-by-step trace of a PDA executing on an input string.

Pushdown Automata (Graphical Notation)

This video from Neso Academy clearly explains the graphical notation for PDA transitions and then walks through a complete example of tracing an input string.

Please watch from 00:51 to 03:25 to understand the graphical notation. Then, watch from 06:37 to 09:18 to see a step-by-step trace of the PDA for the language L = {0ⁿ1ⁿ} on the input string '0011'. Focus on how each transition affects the current state, the remaining input, and the stack's contents.

As you saw in the video, the PDA for works by:

  1. Pushing a special symbol (like or $) to mark the bottom of the stack.
  2. For every 0 it reads in the input, it pushes a 0 onto the stack.
  3. When it sees the first 1, it transitions to a new state and starts popping.
  4. For every 1 it reads, it pops a 0 from the stack.
  5. If the input is exhausted and the stack contains only the initial marker , it can move to a final state.

This visual trace gives us a strong intuitive feel for how a PDA computes. Now, let's formalize this process.



2. Formalizing Execution: Instantaneous Descriptions (IDs)

To trace a PDA's execution with mathematical precision, we use a notation called an Instantaneous Description (ID). An ID is a snapshot of the PDA's entire configuration at a single moment in time.

An ID is represented as a triple: (q, w, γ)

  • q: The current state of the PDA.
  • w: The portion of the input string that has not yet been read.
  • γ: The current content of the stack, written with the top of the stack as the leftmost symbol.

A single computational step, or "move," is denoted by the turnstile symbol, ⊢.
(q₁, aw, Xβ) ⊢ (q₂, w, αβ)

This expression means that the PDA, in state q₁ with a as the next input symbol and X on top of the stack, can move to state q₂, consume the a, and replace X on the stack with the string α. This corresponds to a transition δ(q₁, a, X) containing (q₂, α).

For a more detailed and formal definition of IDs and the ⊢ notation, please review the following material.

Pushdown automata

These lecture slides from the University of Waterloo provide a clear, formal definition of Instantaneous Descriptions and the notation for single and multiple computation steps.

Read the content on slides 10 through 14. Start at the slide titled 'Instantaneous description of a PDA' and continue through 'Longer derivations'. Focus on understanding the formal definition of the ID triple (q, w, γ) and the meaning of the single-step (⊢) and multi-step (⊢*) notations.



3. A Step-by-Step Formal Trace

Let's revisit our PDA for and trace the input 0011 using the formal ID notation.

Here is a diagram of the PDA. Let's name the states from left to right. The initial state is and the final state is .

PDA Example for {0^n1^n}
A PDA for the language L = {0ⁿ1ⁿ | n ≥ 0}. We'll use this diagram for our trace. Note the transition notation 'a, b -> c' is equivalent to our 'a, b / c'. Let's use Z for the initial stack symbol instead of $.

The initial configuration of the PDA for the input 0011 is: (q₀, 0011, Z).

Here is the sequence of moves:

  1. (q₀, 0011, Z) ⊢ (q₀, 011, 0Z)

    • Transition: 0, Z / 0Z (This is a simplified notation used in some diagrams. A more explicit set of transitions would be used, but the logic is to push a 0 while reading a 0). In our chosen diagram above, this corresponds to the loop 0, Z / 0Z on state .
    • Explanation: In state q₀, we read 0 with Z on the stack. We stay in q₀, consume the 0, and push 0Z onto the stack (effectively, push 0 on top of Z).
  2. (q₀, 011, 0Z) ⊢ (q₀, 11, 00Z)

    • Transition: 0, 0 / 00 on state .
    • Explanation: In q₀, read 0 with 0 on top of the stack. Stay in q₀, consume the 0, and push 00 (push another 0).
  3. (q₀, 11, 00Z) ⊢ (q₁, 1, 0Z)

    • Transition: 1, 0 / ε from q₀ to q₁.
    • Explanation: In q₀, read 1 with 0 on top. We transition to state q₁, consume the 1, and push ε (pop the 0).
  4. (q₁, 1, 0Z) ⊢ (q₁, ε, Z)

    • Transition: 1, 0 / ε on state q₁.
    • Explanation: In q₁, read 1 with 0 on top. Stay in q₁, consume the 1, and pop the 0.
  5. (q₁, ε, Z) ⊢ (q₂, ε, Z)

    • Transition: ε, Z / Z from q₁ to q₂.
    • Explanation: In q₁, with no more input (ε), we see Z on the stack. We move to the final state q₂. The stack content remains Z.

Since we have consumed the entire input (ε is left) and ended in a final state (q₂), the string 0011 is accepted.

The following video provides another narrated example of tracing a PDA using IDs, which can help reinforce the concept.

41-INSTANTANEOUS DESCRIPTION (ID) OF PDA IN AUTOMATA THEORY || ID OF PDA ||ACCEPTANCE OF STRING||TOC

This video provides another narrated walkthrough of tracing a PDA using Instantaneous Descriptions. Seeing a second example can help solidify your understanding of the process.

Watch from the beginning to 09:34. The video introduces the ID notation and then applies it to trace the string 'aaabbb' for the same language L = {aⁿbⁿ}. Note that it uses 'z_0' as the stack bottom marker and the notation is slightly different, but the core concept is identical.

Test your understanding!

Consider a PDA designed to accept the language of palindromes with a center marker: .

Here are its transitions:

  • δ(q₀, a, X) = {(q₀, aX)} for any X ∈ {a, b, Z} (Push a's and b's)
  • δ(q₀, b, X) = {(q₀, bX)} for any X ∈ {a, b, Z}
  • δ(q₀, c, X) = {(q₁, X)} for any X ∈ {a, b, Z} (Switch to popping mode)
  • δ(q₁, a, a) = {(q₁, ε)} (Match a's)
  • δ(q₁, b, b) = {(q₁, ε)} (Match b's)
  • δ(q₁, ε, Z) = {(q₂, Z)} (Accept if stack is empty)

The states are , the start state is , the final state is , and the initial stack symbol is Z.

Trace the execution of this PDA for the input string abacaba. Is it accepted? Wait, that's not in the language. Let's trace the input string abccba. Write out the sequence of Instantaneous Descriptions.

Show answer

The initial configuration is (q₀, abccba, Z).

  1. (q₀, abccba, Z) ⊢ (q₀, bccba, aZ)

    • Uses δ(q₀, a, Z) = {(q₀, aZ)}.
  2. (q₀, bccba, aZ) ⊢ (q₀, ccba, baZ)

    • Uses δ(q₀, b, a) = {(q₀, ba)}.
  3. (q₀, ccba, baZ) ⊢ (q₁, cba, baZ)

    • Uses δ(q₀, c, b) = {(q₁, b)}.
  4. (q₁, cba, baZ) ⊢ (q₁, ba, aZ)

    • Uses δ(q₁, c, b). Whoops, there's no transition for δ(q₁, c, b). Let's re-read the transitions. Ah, the c transition δ(q₀, c, X) = {(q₁, X)} means it keeps the stack top the same. The logic is: read w, then c, then w^R.

    Let's restart the trace correctly.

    Initial ID: (q₀, abccba, Z)

    1. (q₀, abccba, Z) ⊢ (q₀, bccba, aZ) (Reads a, pushes a)
    2. (q₀, bccba, aZ) ⊢ (q₀, ccba, baZ) (Reads b, pushes b)
    3. (q₀, ccba, baZ) ⊢ (q₁, cba, baZ) (Reads c, switches to state q₁, stack unchanged)
    4. (q₁, cba, baZ) ⊢ Hmm, input is c, stack top is b. There is no transition δ(q₁, c, b). The machine crashes. The string abccba is rejected.

    This highlights a key part of tracing: if no valid transition exists for a given configuration, that path of computation halts.

    Let's trace a string that is in the language, like abcba.
    Initial ID: (q₀, abcba, Z)

    1. (q₀, abcba, Z) ⊢ (q₀, bcba, aZ)
    2. (q₀, bcba, aZ) ⊢ (q₀, cba, baZ)
    3. (q₀, cba, baZ) ⊢ (q₁, ba, baZ)
    4. (q₁, ba, baZ) ⊢ (q₁, a, aZ)
    5. (q₁, a, aZ) ⊢ (q₁, ε, Z)
    6. (q₁, ε, Z) ⊢ (q₂, ε, Z)

    Since we end in q₂ (a final state) with the input fully consumed, the string abcba is accepted.



Conclusion

In this lesson, we have demystified the execution of a Pushdown Automaton. We've learned how to follow the machine's computation step-by-step, both visually and through a formal mathematical notation.

Key Takeaways:

  • The execution of a PDA is determined by its current state, the remaining input, and the contents of its stack.
  • A transition a, b / c means: read a from input, pop b from the stack, and push c onto the stack.
  • An Instantaneous Description (ID), (q, w, γ), provides a formal snapshot of the PDA's configuration.
  • The turnstile notation (⊢) formally describes a single computation step, transforming one ID into another.
  • A string is accepted (by final state) if the PDA can process the entire string and land in one of its final states, regardless of what's left on the stack.

Preview of the Next Lesson:
Now that we can trace a PDA's execution, the next logical step is to build them ourselves. In the upcoming lesson, we will focus on how to design PDAs that accept specific languages by final state. We will develop strategies for translating language descriptions into the states and transitions of a working Pushdown Automaton.