You are on page 1of 31

Chapter 4

Push Down Automata (PDA)


Push-down Automata/Machine (PDA)
• Push-down Automaton (PDA) is a nondeterministic finite
automaton equipped with an additional storage device called a
stack, and a stack head which reads from and writes to the top
of the stack.
• PDA == [Finite state machine+ “a stack” ]
• Stack is the way we arrange elements on a top of another.
PDA components
1. input tape
2. finite control unit
3. stack with infinite size
Push-down Automata/Machine (PDA)
• The stack is a first-in last-out storage device with no
predetermined size limit.
• A Pushdown Automata (PDA) can access only the stack's
topmost symbol in LIFO manner.
• The stack head always scans the top element of the stack.
• It performs two basic stack operations:
– Push: add a new symbol at the top of the stack.
– Pop: read and remove the top symbol from the stack.
• PDA are equivalent in power to context-free grammars.
• PDA recognizes strings given in a context-free grammar.
Formal Definition of PDA
• A PDA P := (Q, Σ, , δ, q0, Z0, F ):
• Q : Set of states
• Σ: input alphabet
•  : stack symbols(capital of gamma)
• δ: transition function δ : Q x  x Σ  Q x 
• q0: q0 ϵ Q start state
• Z0: Initial stack top symbol
• F: F ⊆ Q Set of Final/accepting states
PDA δ - Transition Function
• δ = transition function, which takes the triple: (q, a, X)
where:
• q = the current state, state in Q
• a = input symbol in Σ
• X = The Symbol on top of the Stack, element of 

• The output of δ is the finite set of pairs (p, Y) where


• p is a new state and
• Y is a new string of stack symbols that replaces X at the
top of the stack.
PDA - Representation
• δ(qi, a, X)={(qj, Y)}

• Y can be one of the following form:


• Y = ε , then we pop the stack
• Y = X , the stack is unchanged
• Y =YX , Y is pushed on the top the stack (above X)
How PDA computes
• At the beginning:
• a PDA is in the initial state q0,
• The PDA have an empty stack, and
• Its tape head is scanning the leftmost symbol of the input
tape.
• At the end, we say a PDA accepts the input if, in one of its
computation paths, it reads all input symbols, has an empty
stack, or reaches a final state. (Acceptance either by empty
stack or by final state.)
PDA-Acceptance

• PDA Accept input if:


• Input is consumed and stack is empty (Acceptance by
“Empty Stack”)
• Or, input is consumed and PDA is in a final state
(Acceptance by “Final State”).
PDA Rejection
• A string is rejected if there is no computation such that:

All the input is consumed


AND
The last state is an accept state
DPDA Vs NDPDA
• In general the PDA is nondeterministic.
• This feature is crucial because, unlike finite automata, non-
determinism adds power to the capability that a PDA would
have if they were only allowed to be deterministic.
 i.e. A non-deterministic PDA can represent languages that
a deterministic PDA cannot.
• A PDA is deterministic if each input string can only be
processed by the machine in one way.
• A PDA is nondeterministic if there is some string that can be
processed by the machine in more than one way.
Deterministic PDA
• A DPDA is simply a pushdown automata without non-
determinism.
– i.e. no transitions to multiple states on same input
• DPDA not as powerful as 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.
Non-Deterministic PDA
• It’s simply called PDA.
• may have more than one edge with the same label leading out
of a certain READ state or POP state.
Example: PDA
• Design a PDA to accept {0n1n | n >= 1}.
• The states:
 q = start state. We are in state q if we have seen only 0’s so
far.
 p = we’ve seen at least one 1 and may now proceed only if the
inputs are 1’s.
 f = final state; accept.
• The stack symbols:
• Z0 = start symbol. Also marks the bottom of the stack, so we
know when we have counted the same number of 1’s as 0’s.
• X = marker, used to count the number of 0’s seen on the input.
Example: PDA
• 0n1n

OR
Actions of the Example PDA

Input 000111

Stack Z0
Actions of the Example PDA

Input 00111

Stack (Push X) X
Z0
Actions of the Example PDA

Input 0111

Stack (Push X) X
X
Z0
Actions of the Example PDA

Input 111

Stack (Push X) X
X
X
Z0
Actions of the Example PDA

Input 111

Stack (pop X) X
X
X
Z0
Actions of the Example PDA

Input 11

Stack (pop X) X
X
Z0
Actions of the Example PDA

Input 1

Stack (pop X) X
Z0
Actions of the Example PDA

Input

Stack Z0
PDA and CFG
• PDA are equivalent in specification power with CFG.
• A language is context-free if and only if some pushdown
automata recognizes it.
• Note: This means that:
1. If a language L is CFL then there is a PDA ML that
recognizes it
2. If a language L is recognized by a PDA ML then there is a
CFG GL that generates L
• Some CFL are more easily described in terms of their
generator (i.e. CFG), whereas others are more easily
described in terms of their recognizers (i.e. PDA).
CFG to PDA conversion

• Given any context-free grammar G with language L,


there is a PDA accepting the same language L.
L (CFG) = L (PDA)
CFG to PDA conversion
• The PDA starts with start variable on the stack.
• At each step:
• If stack-top is a variable, then PDA guesses a
production and replaces variable by RHS of that
production.
• If stack-top is a terminal, then PDA pops it and
reads one symbol from input and checks that the
two match.
• Accept if and only if reach empty stack at end of
string. (Note that PDA not guaranteed to halt.)
CFG to PDA conversion
• S → 0S1S | 1S0S | ε
• The sequence of stacks for the accepting branch of
the PDA for 011100 starts:
CFG to PDA conversion
• So, the real PDA has to interleave the two types of
step: expanding variables by guessing grammar rules
and matching the input against the terminals at the
top of the stack.
• So the final PDA looks like:
CFG to PDA conversion
• The rules on the loop are of two types:
1. ε,A  w for every rule A  w in G
2. a, a  ε for every terminal a
• Example: consider grammar G:
S → AB, A → aAb | ε, B→ cB | ε
• the loop rules are:
S → AB,
A → aAb, A→ε
B→ cB, B→ ε
a,a/ ε b,b/ ε c,c/ ε
CFG to PDA conversion
• Example 1: S → 0S1 | ε
• Construct a PDA M such that L(M) = L(G).
CFG to PDA conversion
• Example 2: S → aS | aSbS | ε
• Construct a PDA M such that L(M) = L(G).
• Solution:
• First show a derivation for a string.
S → aS → aaSbS → aabS → aab
CFG to PDA conversion

You might also like