0% found this document useful (0 votes)
49 views22 pages

Push Down Automata

The document provides an introduction to Pushdown Automata (PDA), highlighting its ability to recognize context-free languages that cannot be recognized by finite automata. It explains the structure and functioning of PDAs, including their deterministic and nondeterministic variants, and their equivalence to context-free grammars. Additionally, it discusses the transition functions, examples of PDAs, and the significance of PDAs in parsing algorithms.

Uploaded by

Subrata Bose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views22 pages

Push Down Automata

The document provides an introduction to Pushdown Automata (PDA), highlighting its ability to recognize context-free languages that cannot be recognized by finite automata. It explains the structure and functioning of PDAs, including their deterministic and nondeterministic variants, and their equivalence to context-free grammars. Additionally, it discusses the transition functions, examples of PDAs, and the significance of PDAs in parsing algorithms.

Uploaded by

Subrata Bose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Pushdown Automata (PDA)

An Introduction

1
Introduction to PDA
• Consider the language L = {anbn | n ≥ 1}.
• This language can not be generated by a FA
as FA can not remember the number of a’s to
match with the b’s. To remember, FA requires
infinite number of states.
• This can be avoided by adding an additional
stack with the FA.
• This machine is PDA
• Just as a DFA is a way to implement a regular
expression, a PDA is a way to implement a
context free grammar
• A PDA is an abstract model machine similar to
2
the FSA
Pushdown Automaton
• PDA has a finite set of states. However, in
addition, it has a pushdown stack. Moves of
the PDA are as follows:
a. An input symbol is read and the top symbol on
the stack is read.
b. Based on the input and the top of the stack, the
machine enters a new state and writes zero or
more symbols onto the pushdown stack or may
even remove a symbol from the stack.
c. Acceptance of a string occurs if the stack is
empty. or if the PDA is in a final state.
d. Both models can be shown to be equivalent.

3
Model of a PDA

Finite
input Accept or reject
State
Control
Reads input symbol by symbol
Can write to stack
Makes transitions based on
input, top of stack

Push Down Store (PDS) Stack

4
Informal PDA Example
 Consider the language L = {0n1n | n 
0 }.
 This is not regular
 A finite automaton is unable to recognize this
language because it cannot store an
arbitrarily large number of values in its finite
memory.

 A PDA is able to recognize this language


 Can use its stack to store the number of 0’s it
has seen.
 As each 0 is read, push it onto the stack
 As soon as 1’s are read, pop a 0 off the stack
 If reading the input is finished exactly when the
stack is empty, accept the input else reject the
input
5
PDA and Determinism
 The description of the previous PDA was
deterministic
 However, in general the PDA is
nondeterministic.
 This feature is crucial because, unlike finite
automata, nondeterminism 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
6
Informal Non-Deterministic Example
 L = { wwR | w є (0+1)* } - the language of even
length palindromes
 Informal PDA description
 Start in state q0. 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.

7
Formal Definition of a PDA

 P = (Q, , Γ, δ, q0, Z0, 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
 q0 = start state
 Z0 = 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.
8
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 γ = A then we pop the stack (remove one symbol from
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 γ
9
PDA Example 1
 A PDA that recognizes L = {anbn | n > 0 }.
 A = (Q, , Γ, δ, q0, Z0, F) where
 Q = { q 0 , q1 }
  = {a, b}
 Γ = {a, Z0}
 F=Ø
 and δ is given by
 δ(q0, a, Z0) = { (q0, aZ0) }
 δ(q0, a, a) = { (q0, aa) }
 δ(q0, b, a) = { (q1, ^) }
 δ(q1, b,a) = {(q1, ^) }
 δ(q1, ^, Z0) = {(q1, ^) }

10
Example 2
 Here is the graphical description of the PDA
that accepts the language
 L = { wwR | w is in (0+1)* }
 Stays in state q0 when we are reading w, saving the input symbol.
Every time we “guess” that we have reached the end of w and are
beginning wR by going to q1 on an epsilon-transition.
 In state q1 we pop off each 0 or 1 we encounter that matches the
input. Any other input will “die” for this branch of the PDA. If we
ever reach the bottom of the stack, we go to an accepting state.

0, ^  0
0,0 ^
1, ^  1
1,1  ^

Start q0 q1 q2
^, ^  ^ ^, Z0  ^

11 Requires Z0 initially on stack


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.
12
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 | (q0, w, Z0) ├* (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.

13
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 | (q0, w, Z0) ├* (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.
14
Equivalence of PDA and CFG
 A context-free grammar and pushdown
automata are equivalent in power.
 Theorem: Given a CFG grammar G, there
exists some pushdown automata P that
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 CFGPDA process, but skip the
PDACFG (more details in the textbook, as
indicated in last slide).

15
Proof for CFG to PDA
 Proof idea:
 The PDA P will work by accepting its input w, if
G generates that input, by determining
whether there is a derivation for w.
 Design rules for P such that the transitions
match the production rules in the grammar
 But the PDA can access only the top symbol on the
stack and that might be a terminal
 But if our PDA makes transitions only for variables, we we
won’t know to do
 To get around this problem, we’ll use the non-
determinism of the PDA to match terminal symbols on
the stack with symbols in the input string before the
first variable
 This “chomps” any leading terminals until we can process
a variable

16
State Diagram for an Arbitrary CFG

Start qstart

ε, ε  S S is the start symbol

qloop ε, AY For rule AY, Y is a string


a,a  ε For terminal symbol a

ε, $/ε $ initially the top of the stack

qaccept

17
Example
 Consider the grammar
S aTb | b
T  Ta | ε
Start qstart
Given the string “aab” derived by
S  aTb  aTab  aab ε, ε  S
We have the corresponding moves: ε, SaTb
(qs, aab, $) ├ (qloop, aab, S$) ├ ε, TTa
(qloop, aab, aTb$) ├ (qloop, ab, Tb$) ├ qloop ε, Sb
ε, Tε
(qloop, ab, Tab$) ├ (qloop, ab, ab$) ├ a, aε
(qloop, b, b$) ├ (qloop, ε, $) ├ b, bε
ε, $ε
(qaccept, ε,ε)
qaccept

18
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
19
implemented using a DPDA.
NDPDAs are different from DPDAs
 What is the relationship between deterministic
 PDAs and nondeterministic PDAs? They are different.

 Consider the set of palindromes, strings reading the same


forward and backward, generated by the grammar
 S  0S0 | 1S1 | 2
 We can recognize such strings by a deterministic PDA:
 1. Stack all 0s and 1s as read.
 2. Enter a new state upon reading a 2.
 3. Compare each new input to the top of stack, and pop stack.

 However, consider the following set of palindromes:


 S  0S0 | 1S1 | 0 | 1
 In this case, we never know where the middle of the
string is. To recognize these palindromes, the automaton
must guess where the middle of the string is (i.e., is
nondeterministic).
20
Language-machine equivalence
 Already shown:
• Regular languages = FSA = NDFSA
• Context free languages = NDPDA. It can be shown that
NDPDA not the same as DPDA

 For context sensitive languages, we have Linear Bounded


Automata (LBA)

 For unrestricted languages we have Turing machines (TM)

 Unrestricted languages = TM = NDTM

 Context sensitive languages = NDLBA.

 It is still unknown if NDLBA=DLBA

21
General parsing algorithms
 Knuth in 1965 showed that the deterministic
PDAs were equivalent to a class of grammars
called LR(k) [Left-to-right parsing with k
symbol lookahead]
• Create a PDA that would decide whether to stack
the next symbol or pop a symbol off the stack
by looking k symbols ahead.
• This is a deterministic process.
• For k=1 process is efficient.
• Tools built to process LR(k) grammars (YACC -
Yet Another Compiler Compiler)
• LR(k), SLR(k) [Simple LR(k)], and LALR(k)
[Lookahead LR(k)] are all techniques used today
to build efficient parsers.
22

You might also like