On Pushdown Automaton v. Finite Automaton:
A finite automaton reads a single tape of input in one direction.
A pushdown automaton also reads an input tape, but it also has an additional tape that it can read and write to.
Stack: A PDA can write and read symbols to the stack.
Example: r,o \to i
- This transition means we can go from state 1 to 2 by:
- Reading r,
- Popping out “o” from the stack, and
- Pushing in “i” to the stack.
Formal: Let \Gamma be an alphabet, then:
Example: Marking End of Stack
- We like to push $ to the stack at the start to indicate the end.
Example: Simple PDA
- Q: Write a PDA that reads a string of { a^n b^n }
- A: Our solution will be to push “0” every time we read “a” and pop “0” every time we read “b”, and using a sentinel value to verify |a| = |b| = n eps
- We use transitions to limit the strings we can accept.
- Notice how we never counted the variables, we just used the simplest hardware
Example: Write a PDA that accepts { a^n b^{3n} }
Example: Write a PDA that accepts { a^n b^{3n+1} }
- In the above photo, we can replace the \epsilon, \epsilon \to \epsilon between the a and b loop with b, \epsilon \to \epsilon to require +1 b.
Example: Write a PDA that accepts { a^n b^{m} | m \ge 3 }
- Q: We can rewrite this as { a^n b^{3n} b^k | k \ge 0 }
- A: Use the PDA from { a^n b^{3n} }, but put a self-loop on the accepting state that is b, \epsilon \to \epsilon
A pushdown automaton is a 6-tuple (Q, \Sigma, \Gamma, \delta, q_0, F) where:
Q, \Sigma, \Gamma, and F are finite.
Suppose the grammar
S \to a^2 S b | \epsilon
We’ll write the parse tree:
We will begin copying the parse tree onto a PDA’s stack tape now:
From layer one we have:
a a S b $
When we read a terminal, we want to pop it off the stack. e.g.,
aaSb
or \epsilonSuppose we’ve read two as and now have to expand S.
S b $
a | a | S | b | b | $ |
---|
The PDA so far looks like:
Finally, to account for b, we just read the right number of bs and then pop $
to reach the accepting state.
S \to aTb|b \\ T \to Ta|\epsilon
a^i b^j c^k | i,j,k \ge 0 \text{ and } i = h \lor i = k
Divide and conquer.