Pushdown Automata
Pushdown Automata
Autometa Theory
Lecture Material on
PUSHDOWN AUTOMATA
The following material will give you an overview of what you learn in the class
1 Introduction
Pushdown automata are nondeterministic finite state machines augmented with addi-
tional stack memory, which is why the term “pushdown” is used, as elements are pushed
down onto the stack. Pushdown automata are computational models like finite state ma-
chines and can do more than a finite state machine but less than a Turing machine.
Pushdown automata accept context-free languages, which include the set of regular lan-
guages. Pushdown automata can be useful when considering parser design and any area
where context-free grammars are used, such as in computer language design.
In pushdown automata, state transitions include adding a symbol to the string generated,
as in FSMs. Still, state transitions can also include instructions about pushing and popping
elements to and from the stack. At each transition, a pushdown automaton can push a
symbol to the stack, pop a symbol from the stack, do both, or do no operations to the stack.
The pushdown automaton starts with an empty stack and accepts if it ends in an ac-
cepting state at the end or if the stack can be empty.
Pushdown automata can be modeled as a state transition diagram with added instruc-
tions about the stack. Where in the finite state machine, the arrows between states were
labeled with a symbol that represented the input symbol from the string, a pushdown au-
tomaton includes information in the form of input symbol followed by the symbol that is
currently at the top of the stack, followed by the symbol to replace the top of the stack with.
Commas, slashes, or arrows sometimes separate these instructions.
1
October 11, 2023 1 INTRODUCTION
• α is the string on the pushdown store. (It is customary to not write w&α completely
but to write the first symbol of w and the top most symbol of α)
• Final State: At the end of the string, the read head is in one of the final states F.
• Null Store: The pushdown store is empty at the end of the string.
Problem 1
2
October 11, 2023 1 INTRODUCTION
• For a PDA M (Q, Σ, Γ, δ, q0 , Z0 , Φ), we define L(M), the language accepted by a PDA
by Null Store as:
L(M ) = {w|(q0 , w, Z0 ) ⊢∗ (p, ϵ, ϵ) for some p ∈ Q }
3
October 11, 2023 2 EQUIVALENCE OF PDA’S
Problem 2
Practice designing PDA for the following languages and trace the acceptance of relevant
strings.
a) L = {an ban |n ≥ 0}
b) L = {an b2n |n ≥ 0}
c) L = {0n 1n 2m |n, m ≥ 0}
d) L = {0n 1n+2 |n ≥ 0}
2 Equivalence of PDA’s
Let M1 be a PDA by final state and is formally defined as a 7-tuple: M (Q, Σ, Γ, δ, q0 , Z0 , F )
and its transition diagram as:
The acceptance of a string or configuration of the machine as: (q1 , w, Z1 ) ⊢∗M1 (qf , ϵ, γ)
Let M2 be a PDA by null store and is formally defined as a 7-tuple: M (Q, Σ, Γ, δ, q0 , Z0 , Φ)
and its transition diagram as:
The acceptance of a string or configuration of the machine as: (q1 , w, Z1 ) ⊢∗M1 (qk , ϵ, ϵ)
4
October 11, 2023 3 DETERMINISTIC AND NON-DETERMINISTIC PDA
5
October 11, 2023 3 DETERMINISTIC AND NON-DETERMINISTIC PDA
For any PDA to be deterministic, both the above conditions must be satisfied; otherwise, if
any condition fails, then the PDA is non-deterministic. In other words, if there are multiple
moves from the same state on the same input symbol as well as from the same state there
are transitions on both input symbols as well as ϵ then such PDA is called non-deterministic.
The PDA in problem 1 is deterministic as the two conditions are satisfied.
Problem 3
6
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA
To trace out the string ”abbbba” the ID is as follows: δ(q0, abbbba, z) ⊢ δ(q0, bbbba, az) ⊢
δ(q0, bbba, baz) ⊢ δ(q0, bba, bbaz) ⊢ δ(q1, ba, baz) ⊢ δ(q1, a, az) ⊢ δ(q1, ϵ, z) ⊢ δ(q2, ϵ, z)
∵ after reading the string ”abbba”, the pda reaches to a final state, the string is accepted.
Since the transitions have multiple actions:
DPDA NPDA
(Deterministic Pushdown Automata) (Non-deterministic Pushdown Automata)
7
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA
• The start symbol of CFG S will act as initial pushdown store symbol of the PDA
• All non-terminals or variables of the CFG will be the stack symbols ’Γ’ of the PDA
and all the terminals of the CFG will be the input symbols ’Σ’ of the PDA.
Problem 4
Obtain an equivalent non deterministic PDA from the given CFG G({S, X}, {a, b}, P, S)
where where the productions P= {S → XS|ϵ, A → aXb|Xb|ab}.
Solution. Let the equivalent PDA M = ({q}, {a, b}, {a, b, X, S}, δ, q, S, Φ) where δ is defined
as follows:
Problem 5
8
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA
5. G5 = {S → 0CC|ab, C → 1S|0S|0}
• Rule3: The transition function involving the pop symbol from the pushdown store
such as δ(qi , a, Z) ⊢ (q, ϵ) leads to the production
[qi , Z, q] → a
• rule4: The transition function that involves pushing a symbol unto the stack my
either keep the pushdown store unchanged or push a string of symbols unto the stake
as represented by the following transition:
δ(qi , a, Z) ⊢ (q, Zz1 Z2 ....Zm ] leads to the production
[qj , Z, q] → a[q0 , z1 , q1 ][q1 , Z2 , q2 ], ....[qm − 1, Zm , Qm ]
where q1 , q2 ....qm is the possible list of states for the PDA.
• Rule5: After obtaining all the productions, identify the variables and productions
that are not participating in the grammar and remove them.
Finally, rename all the remaining variables and present the obtained grammar?
Problem 6
Solution. We need to obtain a CFG G (V,T,P,S) as follows: From the given PDA
M (Σ), T = 0,1. Let us make the Start Symbol as S. The possible variables (V) are:
9
October 11, 2023 4 CORRESPONDENCE BETWEEN CFG AND PDA
Now, we can revisit the obtained production and remove which are not necessary:
The variables [q1 , X, q0 ] and [q1 , Z0 , q0 ] never appear as a LHS of productions, drop
the respective production which contains them: 1a2, 1b2.
Similarly, [q0 , X, q0 ] and [q0 , Z0 , q0 ] always appear on the RHS of any of their own
productions (or just on the RHS), so we can drop these productions: 1a1, 1a3, 1b1,
1b3 as well as the first production from the start symbol.
After removing the productions, the remaining are:
– S → [q0 , Z0 , q1 ]
– [q0 , Z0 , q1 ] → 0[q0 , X, q1 ][q1 , Z0 , q1 ] (A)
– [q1 , Z0 , q1 ] → ϵ. (B)
– [q0 , X, q1 ] → 0[q0 , X, q1 ][q1 , X, q1 ] (C)
– [q0 , X, q1 ] → 0. (C)
– [q1 , X, q1 ] → 1. (D)
– [q1 , X, q1 ] → ϵ. (D)
10
October 11, 2023 5 DECISION ALGORITHMS FOR CFL
After renaming, the productions (P) for the obtained grammar G({S,A,B,C,D},{0,1},P,S)
as follows:
– S→A
– A → 0CB
– B→ϵ
– C → 0CD|0
– D → 1|ϵ
Problem 7
2. M = {
• δ(q1 , 0, Z0 ) ⊢ (q0 , Z0 )}
11
October 11, 2023 5 DECISION ALGORITHMS FOR CFL
• Each row of the table corresponds to one particular length of sub strings.
• Top most row from bottom corresponds to the given string of length-n.
Example: For a given string “x” of length four units, the triangular table looks like-
7cm
In the above table each cell of xij with it Vij where xij represents the sub-string of x starting
from location i and has length j.
Example: For the string x = abcd is a strin, there are n(n+1)/2 possible sub-strings.
i.e., 4 x (4+1) / 2 = 10. They are:
x14 = abcd
x13 = abc x23 = bcd
x12 = ab x22 = bc x32 = cd
x11 = a x21 = b x31 = c x41 = d
and Vij represents a set of variables in the grammar which can derive the sub string xij .
If the set of variables consists of the start symbol, then it becomes sure-
12
October 11, 2023 5 DECISION ALGORITHMS FOR CFL
Problem 8
For the given grammar, check the acceptance of string w = baaba using CYK Algo-
rithm. G={S → AB|BC, A → BA|a, B → CC|b, C → AB|a}
Solution. First, let us draw the triangular table for the given string x = baaba. Since the
length of given string = |x| = 5, the possible sub strings = (5 x 6) / 2 = 15.
The triangular table looks like:
Now fill the cells of the matrix row by row as follows: Filling of each row means finding the
possibility its parent while constructing a parse tree from bottom-up (starting from leaves)
Filling first row
let x11 , x12 , x13 , x14 , x15 = {b,a,a,b,a} be the sub-strings of length 1. (leaves of parse
tree)
• For sub-string x11 = b and ∵ B → b so, the possible parent(s) V11 = {B}
• For sub-string x12 = a and ∵ A → a & C → a so, the possible parent(s) V21 = {A, C}
• For sub-string x13 = a and ∵ A → a & C → a so, the possible parent(s) V31 = {A, C}
• For sub-string x15 = a and ∵ A → a & C → a so, the possible parent(s)V51 = {A, C}
13
October 11, 2023 5 DECISION ALGORITHMS FOR CFL
• The sub-string x31 = baa obtained from the different combinations of sub-strings such
as {b,aa} and {ba ,a} and the corresponding variables are {V11 , V22 } and {V21 , V13 }
{V11 , V22 } = {B}.{B} = {BB} and {V13 , V21 } = {S, A}.{A, C} = {sA, SC, AA, AC}
From G the corresponding productions which derives {BB}&{sA, SC, AA, AC} are
{}
So, the possible parent(s) V31 = {Φ}
• The sub-string x32 = aab obtained from the different combinations of sub-strings such
as {a,ab} and {aa ,b} and the corresponding variables are {V12 , V23 } and {V14 , V22 }
{V12 , V23 } = {A, C}.{S, C} = {AS, AC, CS, CC} and {V14 , V22 } = {B}.{B} = {BB}
From G the corresponding productions which derives {AS, AC, CS, CC}&{BB} are
{B → CC}
So, the possible parent(s) V32 = {B}
• The sub-string x33 = aba obtained from the different combinations of sub-strings such
as {a,ba} and {ab,a} and the corresponding variables are {V13 , V24 } and {V15 , V23 }
{V13 , V24 } = {A, C}.{S, A} = {AS, AA, CS, CA} and {V15 , V23 } = {A, C}.{S, C} =
{AS, AC, CS, CC}
From G the corresponding productions which derives {AS, AA, CS, CA}&{AS, AC, CS, CC}
are {B → CC}
So, the possible parent(s) V32 = {B}
14
October 11, 2023 5 DECISION ALGORITHMS FOR CFL
• The sub-string x41 = baab obtained from the different combinations of sub-strings
such as {b,aab}, {ba ,ab}, {baa,b} and the corresponding variables are {V11 , V32 },
{V21 , V23 }, {V31 , V14 }
{V11 , V32 } = {B}.{B} = {BB}
{V21 , V23 } = {S, A}.{S, C} = {sS, SC, AS, AC}
{V31 , V14 } = Φ.{B} = Φ
From G the corresponding productions which derives {BB}, {sS, SC, AS, AC}&Φ are
{}
So, the possible parent(s) V41 = {Φ}
• The sub-string x42 = aaba obtained from the different combinations of sub-strings
such as {a,aba}, {aa ,ba}, {aab,a} and the corresponding variables are {V12 , V33 },
{V22 , V24 }, {V32 , V15 }
{V12 , V33 } = {A, C}.{B} = {AB, CB}
{V22 , V24 } = {B}.{S, A} = {BS, BA}
{V32 , V15 } = {B}.{A, C} = {BA, BC}
From G the corresponding productions which derives {AB, CB}, {BS, BA}&{BA, BC}
are {S → AB, A → BA, C → AB, S → BC}
So, the possible parent(s) V42 = {S, A, C}
• The sub-string x51 = baaba obtained from the different combinations of sub-strings
such as {b,aaba}, {ba ,aba}, {baa,ba}, {baab,a} and the corresponding variables are
{V11 , V42 }, {V21 , V33 }, {V31 , V24 }, {V41 , V15 }
{V11 , V42 } = {B}.{S, A, C} = {BS, BA, BC}
{V21 , V33 } = {S, A}.{B} = {sB, AB}
{V31 , V24 } = Φ.{S, A} = Φ
{V41 , V15 } = Φ.{A, C} = Φ
From G the corresponding productions which derives {BS, BA, BC}, {sB, AB} are
{A → BA, S → BC, C → AB}
So, the possible parent(s) V51 = {S, A, C}
15
October 11, 2023 5 DECISION ALGORITHMS FOR CFL
Since the root entry such as V51 ={S,A,C} contains start symbol, the string x=baaba is
belongs to the given CFL or can be derived from the grammar G.
Problem 9
16