Jan-June 2025 Btcs 4 Sem v10 Btcs404 Btcs404 Unit4 Notes
Jan-June 2025 Btcs 4 Sem v10 Btcs404 Btcs404 Unit4 Notes
Pushdown Automata:
A pushdown automaton is a way to implement a CFG in the same way we design
DFA for a regular grammar.
A DFA can remember a finite amount of information, but a PDA can remember an
infinite amount of information.
Pushdown automata are simply an NFA augmented with an "external stack memory".
The addition of stack is used to provide a last-in-first-out memory management
capability to Pushdown automata.
Pushdown automata can store an unbounded amount of information on the stack. It
can access a limited amount of information on the stack.
A PDA can push an element onto the top of the stack and pop off an element from the
top of the stack. To read an element into the stack, the top elements must be popped
off and are lost.
A PDA is more powerful than FA. Any language which can be acceptable by FA can
also be acceptable by PDA.
PDA also accepts a class of language which even cannot be accepted by FA. Thus
PDA is much more superior to FA.
A PDA can be formally defined as a 7 tuple (Q, Ʃ, S, δ, q0, I, F), where
o Q : Finite number of states
o Ʃ : Input alphabet
o S : Stack symbols
o δ : Transition Function: Q x (Ʃ U {ϵ} x S x Q x S*
o q0 : initial state (q0 ϵ Q)
o I : Initial stack top symbol (I ϵ S)
o F : Set of accepting states (F ϵ Q)
1
PDA Components:
o Input tape: The input tape is divided in many cells or symbols. The input
head is read-only and may only move from left to right, one symbol at a time.
o Finite control: The finite control has some pointer which points the current
symbol which is to be read.
o Stack: The stack is a structure in which we can push and remove the items
from one end only. It has an infinite size. In PDA, the stack is used to store the
items temporarily.
The following diagram shows a transition in a PDA from a state q 1 to state q2, labelled
as a,b → c:
This means at state q1, if we encounter an input string ‘a’ and top symbol of the stack
is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.
2
The ID can be constructed as follows:
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
Now when we read b, we will change the state from q0 to q1 and start popping
corresponding 'a'. Hence, δ(q0, b, a) = (q1, ε)
Thus this process of popping 'b' will be repeated unless all the symbols are read. Note
that popping action occurs in state q1 only.
δ(q1, b, a) = (q1, ε)
After reading all b's, all the corresponding a's should get popped. Hence when we read
ε as input symbol then there should be nothing in the stack. Hence the move will be:
δ(q1, ε, Z) = (q2, ε)
Where, PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2}
We can summarize the ID as:
δ(q0, a, Z) = (q0, aaZ)
δ(q0, a, a) = (q0, aaa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z) = (q2, ε)
3
This scenario can be written in the ID form as:
δ(q0, 0, Z) = δ(q0, 0Z)
δ(q0, 0, 0) = δ(q0, 00)
δ(q0, 1, 0) = δ(q1, 0)
δ(q0, 1, 0) = δ(q1, 0)
δ(q1, 0, 0) = δ(q1, ε)
δ(q0, ε, Z) = δ(q2, Z)
(ACCEPT state)
4
We are going to design the first part i.e. 1 comes before 0's. The logic is that read
single 1 and push two 1's onto the stack. Thereafter on reading two 0's, POP two 1's
from the stack. The δ can be
δ(q0, 1, Z) = (q0, 11, Z) Here Z represents that stack is empty
δ(q0, 0, 1) = (q0, ε)
Now, consider the second part i.e. if 0 comes before 1's. The logic is that read first 0,
push it onto the stack and change state from q0 to q1. [Note that state q1 indicates that
first 0 is read and still second 0 has yet to read].
Being in q1, if 1 is encountered then POP 0. Being in q1, if 0 is read then simply read
that second 0 and move ahead. The δ will be:
δ(q0, 0, Z) = (q1, 0Z)
δ(q1, 0, 0) = (q1, 0)
δ(q1, 0, Z) = (q0, ε)
(Indicate that one 0 and one 1 is already read, so simply read the second 0)
δ(q1, 1, 0) = (q1, ε)
Now, summarize the complete PDA for given L is:
δ(q0, 1, Z) = (q0, 11Z)
δ(q0, 0, 1) = (q1, ε)
δ(q0, 0, Z) = (q1, 0Z)
δ(q1, 0, 0) = (q1, 0)
δ(q1, 0, Z) = (q0, ε)
δ(q0, ε, Z) = (q0, ε) ACCEPT state
Example 2:
Construct a PDA that accepts L= {0n1n | n ≥ 0}
Solution:
Example 3:
Construct a PDA that accepts L= {wwR | w = (a + b)*}
5
Solution:
6
Simulation of abaaba:
⊢ δ(q2, a, aZ)
Apply rule 8
⊢ δ(q2, ε, Z)
Apply rule 7
⊢ δ(q2, ε)
Apply rule 11
Accept
7
Pushdown Automata and Parsing:
Parsing is used to derive a string using the production rules of a grammar. It is used to
check the acceptability of a string. Compiler is used to check whether or not a string is
syntactically correct. A parser takes the inputs and builds a parse tree.
A parser can be of two types:
o Top-Down Parser: Top-down parsing starts from the top with the start-
symbol and derives a string using a parse tree.
o Bottom-Up Parser: Bottom-up parsing starts from the bottom with the string
and comes to the start symbol using a parse tree.
Design of Top-Down Parser:
For top-down parsing, a PDA has the following four types of transitions:
o Pop the non-terminal on the left hand side of the production at the top of the
stack and push its right-hand side string.
o If the top symbol of the stack matches with the input symbol being read, pop
it.
o Push the start symbol ‘S’ into the stack.
o If the input string is fully read and the stack is empty, go to the final state ‘F’.
Example:
Design a top-down parser for the expression "x + y * z" for the grammar G with the
following production rules:
P: S → S + X | X, X → X * Y | Y, Y → (S) | id
Solution: