Download Pushdown Automata: A Way to Implement Context-Free Grammars and more Study notes Computer Science in PDF only on Docsity!
Pushdown Automata
Pushdown Automata (PDA)
- Just as a DFA is a way to implement a regular
expression, a pushdown automata is a way to
implement a context free grammar
- PDA equivalent in power to a CFG
- Can choose the representation most useful to our particular problem
- Essentially identical to a regular automata except
for the addition of a stack
- Stack is of infinite size
- Stack allows us to recognize some of the non-regular languages
PDA
• Can visualize a PDA with the schematic
input
Finite State Control
Accept or reject
Stack
Reads input symbol by symbol Can write to stack Makes transitions based on input, top of stack
Implementing a PDA
- In one transition the PDA may do the following:
- Consume the input symbol. If is the input symbol, then no input is consumed.
- Go to a new state, which may be the same as the previous state.
- Replace the symbol at the top of the stack by any string.
- If this string is then this is a pop of the stack
- The string might be the same as the current stack top (does nothing)
- Replace with a new string (pop and push)
- Replace with multiple symbols (multiple pushes)
Informal Non-Deterministic
Example
- L = { wwR^ | w is in (0+1)* }
- i.e. the language of even length palindromes
- Informal PDA description
- Start in state q0 that represents the state where we haven’t yet seen the reversed part of the string. While in state q0 we read each input symbol and push them on the stack.
- At any time, assume we have seen the middle; i.e. “fork” off a new branch that assumes we have seen the end of w. We signify this choice by spontaneously going to state q1. This behaves just like a nondeterministic finite automaton
- We’ll continue in both the forked-branch and the original branch. One of these branches may die, but as long as one of them reaches a final state we accept the input.
- In state q1 compare input symbols with the top of the stack. If match, pop the stack and proceed. If no match, then the branch of the automaton dies.
- If we empty the stack then we have seen wwR^ and can proceed to a final accepting state.
Formal Definition of a PDA
- P = (Q, , , , q 0 , $, F)
- Q = finite set of states, like the finite automaton
- = finite set of input symbols, the alphabet
- = finite stack alphabet, components we are allowed to push on the stack
- q 0 = start state
- $ = start symbol. Initially, the PDA’s stack consists of one instance of this start symbol and nothing else. We can use it to indicate the bottom of the stack. We can place $ explicitly on the stack initially, or assume it is there already.
- F = Set of final accepting states.
PDA Transition Function
- = transition function, which takes the triple: (q, a, X) where - q = state in Q - a = input symbol in - X = stack symbol in
- The output of is the finite set of pairs (p, ) where p is a new state and is a new string of stack symbols that replaces X at the top of the stack. - If = then we pop the stack - if = X the stack is unchanged - if = YZ then X is replaced by Z and Y is pushed on the stack. Note the new stack top is to the left end. - If X = then we push on
Formal PDA Example
- Here is a formal description of the PDA that recognizes L = {0n 1 n^ | n ≥ 0 }. - Q = { q 1 , q 2 , q 3 , q 4 } - = {0, 1} - = {0, $} - F = {q1, q4}
- And is described by the table below
Input: 0 1
Top Stack: 0 $ 0 $ 0 $
q q q q
q2,$ q2,0 q3, q3, q4,
Moves of a PDA
- To describe the process of taking a transition, we can adopt a notation similar to like we used for DFA’s. In this case we use the “turnstile” symbol which is used as:
(q, aw, Xβ) (p, w, αβ)
- In other words, we took a transition such that we went from state q to p, we consumed input symbol a, and we replaced the top of the stack X with some new string α.
- We can extend the move symbol to taking many moves:
* represents zero or more moves of the PDA.
Language of a PDA
- The PDA consumes input and accepts input when
it ends in a final state. We can describe this as:
L(P) = {w | (q 0 , w, Z 0 ) * (q, , α) } where q ∈ F
- That is, from the starting state, we can make
moves that end up in a final state with any stack
values. The stack values are irrelevant as long as
we end up in a final state.
Alternate Definition for L(PDA)
- It turns out we can also describe a language of a PDA by ending up with an empty stack with no further input
N(P) = {w | (q 0 , w, Z 0 ) * (q, , ) } where q is any state.
- That is, we arrive at a state such that P can consume the entire input and at the same time empty its stack.
- It turns out that we can show the classes of languages that are L(P) for some PDA P is equivalent to the class of languages that are N(P) for some PDA P.
- This class is also exactly the context-free languages. See the text for the proof.
Equivalence of PDA and CFG
- A context-free grammar and pushdown automata
are equivalent in power.
- Theorem: Given a CFG grammar G, then some
pushdown automata P recognizes L(G).
- To prove this, we must show that we can take any CFG and express it as a PDA. Then we must take a PDA and show we can construct an equivalent CFG.
- We’ll show the CFGPDA process, but skip the PDACFG (more details in the textbook, as indicated in last slide).
Example
- Consider the grammar S aTb | b T Ta |
Start qstart
qloop
qaccept
, $
, SaTb , TTa , Sb , T a, a b, b
Given the string “aab” derived by S aTb aTab aab We have the corresponding moves: (qs, aab, $) (qloop, aab, S$) (qloop, aab, aTb$) (qloop, ab, Tb$) (qloop, ab, Tab$) (qloop, ab, ab$) (qloop, b, b$) (qloop, , $) (qaccept, ,)
, S
Proof for PDA to CFG
• Harder direction; not as easy to “program” a
CFG as it is a PDA
Deterministic PDA
- A DPDA is simply a pushdown automata without
non-determinism.
- i.e. no epsilon transitions or transitions to multiple states on same input
- Only one state at a time
- DPDA not as powerful a non-deterministic PDA
- This machine accepts a class of languages somewhere between regular languages and context-free languages.
- For this reason, the DPDA is often skipped as a topic
- In practice the DPDA can be useful since determinism is much easier to implement.
- Parsers in programs such as YACC are actually implemented using a DPDA.
Context-Free Languages and
Regular Languages
• Every regular language is context free.
- How can we argue this from what we know
about PDA’s and context free languages?