Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

CMP3008

Formal Languages
and Automata Theory
Lecture Notes 6
Pushdown Automata

Sources
https://eecs.wsu.edu/~ananth/CptS317/Lectures/index.htm
"Introduction to automata theory, languages and
computation" by JE Hopcroft, R Motwani and JD Ullman.
" An Introduction to Formal Languages and Automata Theory" by
Peter Linz 1
FA vs. PDA
FA vs. PDA

• These automata are like nondeterministic finite automata but have an


extra component called a stack.
• The stack provides additional memory beyond the finite amount
available in the control.
• The stack allows pushdown automata to recognize some nonregular
languages.
Pushdown Automata
• A pushdown automaton (PDA) can write symbols on the stack and read
them back later. Writing a symbol “pushes down” all the other symbols on
the stack.
• At any time the symbol on the top of the stack can be read and removed.
The remaining symbols then move back up.
• Writing a symbol on the stack is often referred to as pushing the symbol,
and removing a symbol is referred to as popping it.
• Note that all access to the stack, for both reading and writing, may be done
only at the top.
• If certain information is written on the stack and additional information is
written afterward, the earlier information becomes inaccessible until the
later information is removed.
What is a Stack?
• Stack is a data structure in which data is added and removed at only
one end called the top.

• Examples of stacks are:


▪ Stack of books
▪ Stack of trays in a cafeteria, …

5
What is a Stack?
• Last in First Out (LIFO) data structure
• Primary operations: Push and Pop
• Push
• Add an element to the top of the stack
• Pop
• Remove the element from the top of the stack
• An example
empty stack: initial state push (A) push (B) pop
top
B
top top
A A A
top

6
Building a Stack Step-by-Step
top 2
top 8 8
top 1 1 1
Push(8) Push(2)
7 7 7
2 2 2

pop()
top 8
top 1
1
top 7 7 pop()
pop() 7
2 2
2

7
PDA - the automata for CFLs
• What is?
• FA to Reg Lang, PDA is to CFL
• PDA == [  -NFA + “a stack” ]
• Why a stack?

Input -NFA Accept/reject


string

A stack filled with “stack symbols”


8
Pushdown Automata
• A stack is valuable because it can hold an unlimited amount of
information.
• Recall that a finite automaton is unable to recognize the language
{0n1n | n ≥ 0} because it cannot store very large numbers in its finite
memory.
• A PDA is able to recognize this language because it can use its stack to
store the number of 0s it has seen.
• Thus, the unlimited nature of a stack allows the PDA to store numbers
of unbounded size.
How a PDA recognizes n
{0 1n | n ≥ 0}
• Read symbols from the input.
• As each 0 is read, push it onto the stack.
• As soon as 1s are seen, pop a 0 off the stack for each 1 read.
• If reading the input is finished exactly when the stack becomes empty
of 0s, accept the input.
• If the stack becomes empty while 1s remain or if the 1s are finished
while the stack still contains 0s or if any 0s appear in the input
following 1s, reject the input.
Deterministic vs. Nondeterminitic PDAs

• Pushdown automata may be nondeterministic.


• Deterministic and nondeterministic pushdown automata are not equivalent
in power.
• Nondeterministic pushdown automata recognize certain languages that no
deterministic pushdown automata can recognize.
• Recall that deterministic and nondeterministic finite automata do
recognize the same class of languages, so the pushdown automata
situation is different.
• We focus on nondeterministic pushdown automata because these
automata are equivalent in power to context-free grammars.
Recall that Σε = Σ∪ {ε} and Γε = Γ ∪ {ε}
old state input symb. Stack top new state(s) new Stack top(s)

δ : Q x ∑ x  => Q x 
δ : The Transition Function
δ(q,a,X) = {(p,Y), …}
1. state transition from q to p
2. a is the next input symbol X Y
a
3. X is the current stack top symbol q p
4. Y is the replacement for X;
it is in * (a string of stack symbols) Y=? Action
i. Set Y =  for: Pop(X) i) Y= Pop(X)
ii. If Y=X: stack top is unchanged
ii) Y=X Pop(X)
iii. If Y=Z1Z2…Zk: X is popped Push(X)
and is replaced by Y in reverse order iii) Y=Z1Z2..Zk Pop(X)
(i.e., Z1 will be the new stack top) Push(Zk)
Push(Zk-1)

Push(Z2)
Push(Z1)

13
Sample npda Transitions
• Next move of a pushdown automata is determined by
• The current state,
• next input symbol read,
• and top symbol of the stack
• Either symbol may be ε, causing
• the machine to move without reading a symbol from the input
• or without reading a symbol from the stack.
Sample npda Transitions
• Example below presents a sample transition rule:
δ(q1, a, b) = {(q2, cd), (q3, )}
• According to this rule, when the control unit is in state q1, the input
symbol is a, and the top of the stack is b, two moves are possible:
• New state is q2 and the symbols cd replace b on the stack
• New state is q3 and b is simply removed from the stack
• If a particular transition is not defined, the corresponding
(state, symbol, stack top) configuration represents a dead state
PDA as a state diagram

δ(qi,a, X)={(qj,Y)}

Next Current Stack


input stack Top
Current symbol top Replacement
state (w/ string Y)

a, X / Y Next
qi qj state

16
A Sample Nondeterministic Pushdown
Accepter
• Example: Consider the npda
Q = { q0, q1, q2, q3 }, Σ = { a, b }, = { 0, 1 }, z = 0, F = {q3}
with initial state q0 and transition function given by:
δ(q0, a, 0) = { (q1, 10), (q3, ) }
δ(q0, , 0) = { (q3, ) }
δ(q1, a, 1) = { (q1, 11) }
δ(q1, b, 1) = { (q2, ) }
δ(q2, b, 1) = { (q2, ) }
δ(q2, , 0) = { (q3, ) }
• As long as the control unit is in q1, a 1 is pushed onto the
stack when an a is read
• The first b causes control to shift to q2, which removes a
symbol from the stack whenever a b is read
Transition Graphs
• In the transition graph for a npda, each edge is
labeled with the input symbol, the stack top, and the
string that replaces the top of the stack
• The graph below represents the npda:

δ(q0, a, 0) = { (q1, 10), (q3, ) }


δ(q0, , 0) = { (q3, ) }
δ(q1, a, 1) = { (q1, 11) }
δ(q1, b, 1) = { (q2, ) }
δ(q2, b, 1) = { (q2, ) }
δ(q2, , 0) = { (q3, ) }
Example
State Diagram of a PDA
• We write “a,b → c” to signify that when the machine is reading an a from
the input, it may replace the symbol b on the top of the stack with a c.
• Any of a, b, and c may be ε.
• If a is ε, the machine may make this transition without reading any symbol
from the input.
• If b is ε, the machine may make this transition without reading and
popping any symbol from the stack.
• If c is ε, the machine does not write any symbol on the stack when going
along this transition.
• A special symbol, $, is added on the stack to test for an empty stack.
20
State Diagram of a PDA
• We write “a,b → c” to signify that when the machine is reading an a
from the input, it may replace the symbol b on the top of the stack
with a c.
• Any of a, b, and c may be ε.
• If a is ε, the machine may make this transition without reading any
symbol from the input.
• If b is ε, the machine may make this transition without reading and
popping any symbol from the stack.
• If c is ε, the machine does not write any symbol on the stack when
going along this transition.

21
Testing Empty Stack
Example

• State diagram?
State Diagram Example
State Diagram Example
State Diagram Example
30
Proof

32
Proof
• To push the entire string u = u1 · · · ul on the stack at the same
time, we introduce new states q1, . . . , ql−1 and setting the
transition function as:

33
Proof
• We use the notation (r, u) ∈ δ(q, a, s) to mean that

• when q is the state of the automaton,


• a is the next input symbol,
• and s is the symbol on the top of the stack,

the PDA may read the a and pop the s, then push the string u onto the stack and go
on to the state r.

34
Proof
• We use the notation (r, u) ∈ δ(q, a, s) to mean that

• when q is the state of the automaton,


• a is the next input symbol,
• and s is the symbol on the top of the stack,

the PDA may read the a and pop the s, then push the string u onto the stack and go
on to the state r.

35
Proof

36
Proof

37

You might also like