Pushdown automata


In [1]:
from tock import *

Creating PDAs

A pushdown automaton (PDA) can be created with PushdownAutomaton() or by reading from a file.


In [2]:
m = read_csv("examples/sipser-2-14.csv")
m.is_finite(), m.is_pushdown(), m.is_deterministic()


Out[2]:
(False, True, True)

In [3]:
to_table(m)


Out[3]:
ε 0 1
ε $ ε 0
>q1 q2,$
q2 q2,0 q3,ε
q3 q4,ε q3,ε
@q4

The first column lists the states, just as in a FA. The first row lists input symbols and the second row lists popped stack symbols.

The cells contain pairs of new states and pushed stack symbols. So, if the machine is in state q2, and the next input symbol is 0, then the machine stays in state q2 and pushes a 0 onto the stack. If a cell has multiple tuples, then each tuple must be enclosed in parentheses (and curly braces are optional).

Here's the state transition diagram:


In [4]:
to_graph(m)


%3 _START 0 q1 _START->0 2 q2 0->2 ε,ε → $ 1 q4 2->2 0,ε → 0 3 q3 2->3 1,0 → ε 3->1 ε,$ → ε 3->3 1,0 → ε

Now it's easier to see that it accepts strings of the form $\texttt{0}^n\texttt{1}^n$. If you draw the graph in a graph editor, use -> for a right arrow.

Running PDAs


In [5]:
run(m, "0 0 0 1 1 1")


%3 _START 0 q1,ε _START->0 1 q2,$ 0->1 2 q2,[0] $ 1->2 3 q2,[0] 0 $ 2->3 4 q2,[0] 0 0 … 3->4 12 q3,[0] 0 $ 4->12 5 7 5->7 0 6 6->5 0 8 7->8 0 9 8->9 1 10 9->10 1 11 10->11 1 13 q3,[0] $ 12->13 14 q3,$ 13->14 15 q4,ε 14->15

The nodes of the run now show the contents of the stack as well. The top symbol of the stack is marked with square brackets. If the stack gets too deep, then only the top few elements are shown, with an ellipsis instead of the rest of the stack. You can change how many stack elements are displayed using the show_stack option, which defaults to 3. (More about ellipses below.)

This is a deterministic PDA. That's not very exciting, so let's try a nondeterministic PDA. This one (Example 2.18) accepts strings of the form $\{ww^R\}$.


In [6]:
m = read_csv("examples/sipser-2-18.csv")
m.is_finite(), m.is_pushdown(), m.is_deterministic()


Out[6]:
(False, True, False)

In [7]:
to_table(m)


Out[7]:
ε 0 1
ε $ ε 0 ε 1
>@q1 q2,$
q2 q3,ε q2,0 q2,1
q3 q4,ε q3,ε q3,ε
@q4

In [8]:
to_graph(m)


%3 _START 2 q1 _START->2 0 q2 0->0 0,ε → 0 1,ε → 1 1 q3 0->1 ε,ε → ε 1->1 0,0 → ε 1,1 → ε 3 q4 1->3 ε,$ → ε 2->0 ε,ε → $

In [9]:
run(m, "0 0 1 1 0 0")


%3 _START 0 q1,ε _START->0 1 q2,$ 0->1 9 q2,[0] $ 1->9 10 q3,$ 1->10 2 4 2->4 0 3 7 3->7 0 5 4->5 0 6 5->6 1 6->3 1 8 7->8 0 11 q2,[0] 0 $ 9->11 12 q3,[0] $ 9->12 13 q4,ε 10->13 14 q3,[0] 0 $ 11->14 16 q2,[1] 0 0 … 11->16 15 q3,$ 12->15 17 q4,ε 15->17 18 q3,[1] 0 0 … 16->18 19 q2,[1] 1 0 … 16->19 21 q3,[0] 0 $ 18->21 20 q3,[1] 1 0 … 19->20 22 q2,[0] 1 1 … 19->22 23 q3,[0] $ 21->23 24 q3,[0] 1 1 … 22->24 26 q2,[0] 0 1 … 22->26 25 q3,$ 23->25 29 q3,[1] 1 0 … 24->29 27 q4,ε 25->27 28 q3,[0] 0 1 … 26->28

Notice the nondeterminism: at every step, the automaton considers whether it might be at the midpoint of the string.

We saw that in a nondeterministic FA, there could be infinitely many runs for an input string, and the run graph indicated this using a cycle. With nondeterministic PDAs, we have a new problem: they can push/pop as many times as they want without reading in any input, so the infinitely many runs also go through infinitely many configurations! Consider the following PDA, which (for some reason) pushes an arbitrary number of # signs, reads in a single 0, then pops all the # signs again:


In [10]:
m = read_csv("examples/pdaloop.csv")
to_graph(m)


%3 _START 0 q1 _START->0 1 q2 0->1 ε,ε → $ 1->1 ε,ε → # 2 q3 1->2 0,ε → ε 2->2 ε,# → ε 3 q4 2->3 ε,$ → ε

In [11]:
run(m, "0")


%3 _START 7 q1,ε _START->7 0 1 1->0 0 2 q4,ε 3 q2,[#] # # … 3->3 5 q3,[#] # # … 3->5 4 q3,[#] # $ 11 q3,[#] $ 4->11 5->4 5->5 6 q2,$ 8 q2,[#] $ 6->8 9 q3,$ 6->9 7->6 10 q2,[#] # $ 8->10 8->11 9->2 10->3 10->4 11->9

The graph shows us the first few pushes, but after the stack gets deep enough, only the top few items are shown, and the pushes stop creating new nodes. What isn't apparent from the run graph is that the simulator does make sure that the same number of # signs are pushed and popped.