Professional Documents
Culture Documents
CMP3008 LN6 PushdownAutomata PDF
CMP3008 LN6 PushdownAutomata PDF
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
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?
δ : 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)}
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:
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
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
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