You are on page 1of 47

Unit 5: PDA

5.1. Introduction to Push Down Automata (PDA),


Representation of PDA,
Operations of PDA, Move of a PDA,
Instantaneous Description for PDA

5.2. Deterministic PDA, Non Deterministic PDA,


Acceptance of strings by PDA, Language of PDA

5.3. Construction of PDA by Final State , Construction of PDA by Empty Stack,


Conversion of PDA by Final State to PDA accepting by Empty Stack and vice-versa,
Conversion of CFG to PDA, Conversion of PDA to CFG
PDA
A FSA with a memory device is called a pushdown automaton (PDA).
PDA is an automaton with finite states and the memory can be
unbounded.

A PDA is simply an NFA with a stack.


q x, y → z q
1 2

This means:
read x from the input;
pop y off the stack;
push z onto the stack NOTE: Any of x,y,z may be ε.
Pushdown automaton (PDA) SOME BOOK:: 6 tuple system A = (Q, Σ, Γ, s, Δ, F)
NOT considering z0
A PDA is defined by seven tuples (Q, Σ, Г, δ, q0, z0, F)
Where,
Q- Finite set of states.
Σ- Finite set of input symbols | alphabets.
Г- Finite set of stack alphabet
q0- Start state of PDA q0 ε Q
z0- Initial stack symbol; z0 ε Г
F- Set of final states; F is subset of Q
δ- Transition function that maps Q × (Σ U {Є}) × Г à Q × Г*

It is useful to imagine that the automaton has a “stack”


which can be filled by letters in Γ at one end, and the
letters can
be extracted from the stack at the same end, according to
the principle “first in – last out”.
Design PDA which accepts anbn for n > 0
Accepted strings: ab, aabb …
Rejected strings: abab, aab, ba , …
How to represendt PDA processing:
1st way: State transition diagram
2nd Way: Transition Function
NOTE:
PDA can be accepted by 2 way:
=> Accepted by final state
=> Accepted by empty stack

PDA (2nd way)


State transition diagram

PDA (1st way)


Transition Function
Basic Structure of PDA
A pushdown automaton is a way to implement a context-free grammar in a similar
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.
Basically a pushdown automaton is −
"Finite state machine" + "a stack"
A pushdown automaton has three components −
• an input tape,
• a control unit, and
• a stack with infinite size.
The stack head scans the top symbol of the stack.
A stack does two operations −
• Push − a new symbol is added at the top.
• Pop − the top symbol is read and removed.
A PDA may or may not read an input symbol, but it has to read the top of the
stack in every transition.

The following diagram shows a


transition in a PDA from a state
q1 to state q2, labeled as a,b → c −

1 2

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.
Informal Introduction
The context free languages have a type of automaton that defines them. This automaton is called “pushdown
automaton” which can be thought as a Є-NFA with the addition of stack. The presence of a stack means that, the
pushdown automata can remember infinity amount of information. However, the pushdown automaton can only
access the information on its stack in a Last-in-first-out way.
We can define PDA informally as device shown in figure:
PDA is an abstract machine determined by following three things:
• Input table Input tape

• Finite stack control


• A stack
Each moves of the machine is determined by three things;
• The current state
• Next input symbol
• Symbol on the top of stack
The moves consist of;
• Changing state | staying on same state
• Replacing the stack top by string of zero or more symbols.
Popping the top symbol off the stack means replacing it by Є.
Pushing Y on the stack means replacing stack’s top, say X, by YX.
Assuming the left end of stack corresponds to stack‘s top.
The single move of the machine contains only one stack operation either push or pop.
Formal Definition:
A PDA is defined by seven tuples (Q, Σ, Г, δ, q0, z0, F)
Where,
Q- Finite set of states.
Σ- Finite set of input symbols | alphabets.
Г- Finite set of stack alphabet
q0- Start state of PDA q0 ε Q
z0- Initial stack symbol; z0 ε Г
F- Set of final states; F is subset of Q
δ- Transition function that maps Q × (Σ U {Є}) × Г à Q × Г*
as for finite automata, δ governs the behavior of PDA. Formally, δ takes as argument a triple δ (q,a,X)
where
q is a state.
‘a’ is either an input symbol in Σ or Є. ( note Є does not belongs to Σ)
X is a stack symbol.

The output of δ is a finite set of pairs (p, γ), where p is the new state and γ is the string on the stack after transition.
i.e the moves of PDA can be interpreted as;
δ(q, a, z) = {(P1, γ 1) (p2, γ 2) …………….(pm, γ m)} here q, pi ε Q, a ε Σ U Є & z ε Г, γ i ε Г*
There are two types of PDA:
Deterministic Push Down Automata aka DPDA
=> structure above with output function as
Output Function: Q × (Σ U {Є}) × Г à Q × Г*

Non Deterministic Push Down Automata aks NPDA


=> The only difference is of output function as,
Output Function: Q x {Σ U {Є}} x Г -> 2 (Q x Г*)
Why NPDA is more powerful than DPDA?
NPDA is more powerful than DPDA so we can't say for a given NPDA, a DPDA will exist:
- The set of languages that can be accepted by DPDA is not equal to NPDA. For example we
can accept the following language using NPDA but there is no DPDA which can accept it.

- Why DPDA can't accept 𝐿? Because D in DPDA stands for Determinism. That is DPDA can not
decide in nondeterministic way where is the middle of input string (for accepting 𝑤𝑤𝑅 it
should find its middle first! check it one time!).
- So Class of languages NPDA can accept is bigger than DPDA.
Graphical notation of PDA

We can use transition diagram to represent a PDA, where


• Any state is represented by a node in graph (diagram)
• Any arc labeled with “start” indicates the start state and doubly circled states
are accepting / final states.
• The arc corresponds to transition of PDA as:
Arc labeled as a, x | α means the transition δ(q, a, x) = (p,α) for arc from state p
to q.
Example: A PDA accepting a string over {a, b} such that number of a’s and b’s are equal.
i.e. L={w | w ε {a, b}* and a’s and b’s are equal}.
The PDA that accepts the above language can be constructed using the idea that the PDA
should push the input symbol if the top of the stack symbol is same as it otherwise Pop the
stack.
For this, let us construct a PDA as;
P = {Q, Σ, Г, δ, q0, z0, F} be the PDA recognizing the given language. where, let us suppose
Q = {q0, q1, q2} Σ = {a, b} Г = {a, b, z0} z0 = z0 q0 = q0 F = {q2}
Now δ is defined as;
1. δ(q0, Є, Є) = (q1, z0) //initialize the stack to indicate the bottom of stack.
2. δ(q1, a, z0) = (q1, az0)
3. δ(q1, b, z0) = (q1, bz0) Graphical Notation will look like
4. δ(q1, a, a) = (q1, aa)
5. δ(q1, b, b) = ( q1, bb)
6. δ(q1, a, b) = (q1, Є)
7. δ(q1, b, a) = (q1, Є)
8. δ(q1, Є, z0) = (q2, Є) // indicate the acceptance of string.
Let us trace for w= aabbbaab
The execution of PDA on the given string can be traced as shown on the table;
S.N State unread string Stack Transition Used
1 q0 aabbbaab Є --
2 q1 aabbbaab z0 1
3 q1 abbbaab az0 2
4 q1 bbbaab aaz0 4
5 q1 bbaab az0 7
6 q1 baab z0 7
7 q1 aab bz0 3
8 q1 ab z0 6
9 q1 b az0 2
10 q1 Є z0 7
11 q2 Є Є 8
Finally we have q2 state. Hence accepted.
Exercise
Trace as above for string aababb
Trace as above for string ababa (note this string will be not accepted.i.e. PDA will
not reach to final state)
Instantaneous Description
The instantaneous description (ID) of a PDA is represented by a triplet (q, w, Г)
where
• q is the state
• w is unconsumed input
• Г is the stack contents
Instantaneous Description of PDA (ID)
Any configuration of a PDA can be described by a triplet (q, w, Г). Where, q- is the state. w- is
the remaining input & Г - is the stack contents.
Such a description by triple is called an instantaneous description of PDA (ID).

For finite automaton, the δ’ notation was sufficient to represent sequences of instantaneous
description through which a finite automaton moved, since the ID for a finite automaton is just
its state. However, for PDA’s we need a notation that describes changes in the state, the input
and stack. Thus, we adopt the notation of instantaneous description.
Let P = {Q, Σ, Г, δ, q0, z0, F} be a PDA. Then, we define a relation ├, yields as
(p, aw, zα)├ (q, w, βα)
This move reflects the idea that when in state p, by consuming “a” from the input, and
replacing z on the top of stack by β, we can go from state p to state q.
Note that what remains on the input w, and what is below the top of stack, β, do not influence
the action of the PDA.
For the PDA described earlier accepting language of equal a’s and b’s, [see example 1] the
accepting sequence of ID’s for string 1001 can be shown as;
For the PDA described earlier accepting language of equal a’s and b’s, [see
example 1] the accepting sequence of ID’s for string 1001 can be shown as;
(q0, 1001, Є )
├ (q1, 1001, z0)
├ (q1, 001, 1z0)
├ (q1, 01, z0)
├ (q1, 1, 0z0)
├ (q1, Є, z0 )
|- (q2, Є , Є ) Accepted

Therefore (q0, 1001, z0)├* (q2, Є, Є)


Pushdown Automata Acceptance:

We can define acceptance of any string by a PDA in two ways;


1) Acceptance by final state:
- Given a PDA P=(Q, ∑, S, δ, q0, z0, F), the language accepted by final state, L(P)
is;
{w | (q0, w, z0) ├* (q, Є, x)} where q ε F and x ε Г*.
2) Acceptance by empty stack:
- Given a PDA P=(Q, ∑, S, δ, q0, z0, F), the language accepted by empty stack,
L(P) is
{w | (q0, w, z0) ├* (q, Є, Є)} where q ε Q.
Pushdown Automata Acceptance
There are two different ways to define PDA acceptability.
• Final State Acceptability
In final state acceptability, a PDA accepts a string when, after reading the entire string,
the PDA is in a final state. 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.
For a PDA (Q, ∑, S, δ, q0, z0, F), the language accepted by the set of final states F is −
L(PDA) = {w | (q0, w, z0) ⊢* (q, ε, x), q ∈ F} for any input stack string x.

• Empty Stack Acceptability


Here a PDA accepts a string when, after reading the entire string, the PDA has emptied
its stack.
For a PDA (Q, ∑, S, δ, q0, z0, F), the language accepted by the empty stack is −
L(PDA) = {w | (q0, w, z0) ⊢* (q, ε, ε), q ∈ Q}
• Example:: Construct a PDA that accepts L = {0n 1n | n ≥ 0}
This language accepts L = {ε, 01, 0011, 000111, ............................. }
Here, in this example, the number of ‘a’ and ‘b’ have to be same.
•Initially we put a special symbol ‘$’ into the empty stack.
•Then at state q2, if we encounter input 0 and top is Null, we push 0 into stack. This
may iterate. And if we encounter input 1 and top is 0, we pop this 0.
•Then at state q3, if we encounter input 1 and top is 0, we pop this 0. This may also
iterate. And if we encounter input 1 and top is 0, we pop the top element.
•If the special symbol ‘$’ is encountered at top of the stack, it is popped out and it
finally goes to the accepting state q4.
We can define acceptance of any string by a PDA in two ways;
1) Acceptance by final state:
- Given a PDA P=(Q, ∑, S, δ, q0, z0, F), the language accepted by final state, L(P)
is;
{w | (q0, w, z0) ├* (q, Є, x)} where q ε F and x ε Г*.

2) Acceptance by empty stack:


- Given a PDA P=(Q, ∑, S, δ, q0, z0, F), the language accepted by empty stack,
L(P) is
{w | (q0, w, z0) ├* (q, Є, Є)} where q ε Q.
Deterministic Pushdown Automata (DPDA)
While the PDAs are by definition, allowed to be Non-deterministic, the
deterministic PDA is quite important. In practice the parsers generally behave
like Deterministic PDA, so the class of language accepted by these PDAs are
practically important. DPDA are suitable for use in programming language.

A pushdown automata P = (Q, Σ, Г, δ, q0, z0, F) is deterministic pushdown


automata if there is no configuration for which P has a choice of more than one
moves. i.e. P is deterministic if following two conditions are satisfied;
1) For any q ε Q, x ε Г, If δ(q, a, x) ≠Φ for some a ε Σ then δ(q, Є, x) = Φ
i.e. if δ(q, a, x) is non-empty for some a, then δ(q, Є, x) must be empty.
2) For any q ε Q, a ε Σ U {Є} and x ε Г, δ(q, a, x) has at most one element.
Example: A DPDA accepting language L = {w c wR | w ε (0+1)*} is constructed as;

P = ({q0, q1, q2}, {0, 1}, {0, 1, z0}, δ, q0, z0, {q2}) where δ is defined as;

1. δ(q0, Є, Є) = (q0, z0) //initialize the stack


2. δ(q0, 0, z0) = (q0, 0z0)
NOTE:
3. δ(q0, 1, z0) = (q0, 1z0) PDA are designed when there is subtle decision like arrival of ‘c’ in:
4. δ(q0, 0, 0) = (q0, 00) wcwR where w ε (a,b)*
NPDA designed when there is no subtle decision like:
5. δ(q0, 1, 1) = (q0, 11)
wwR where w ε (a,b)*
6. δ(q0, 0, 1) = (q0, 01)
7. δ(q0, 1, 0) = (q0, 10)
8. δ(q0, C, 0) = (q1, 0) //change the state when center is reached.
9. δ(q0, C, 1) = (q1, 1) // change the state when center is reached
10. δ(q0, C, z0) = (q1, z0)// change the state when center is reached
11. δ(q1, 0, 0) = (q1, Є)
12. δ(q1, 1, 1) = (q1, Є)
13. δ(q1, Є, z0) = (q2, Є)
The graphical notation for the above PDA is:
Example: A DPDA accepting strings of balanced ordered parenthesis P;
Q = (q0, q1, q2), Σ = {{,}, (,), [,]}, Г = Σ U {z0}, δ, q0, z0, {q2}
Where δ is defined as:
1. δ(q0, Є, Є) = (q0, z0)
2. δ(q0, {, z0) = (q1, {z0) Solution:
3. δ(q0, [, z0) = (q1, [z0) Evaluate or trace for the string [{ }]
4. δ(q0, (, z0) = (q1, (z0) (q0, [{ }], z0)
5. δ(q1, {, { ) = (q1, {{ ) ├ (q1, { }], [z0)
6. δ(q1, [, [ ) = (q1, [[ ) ├ (q1,}], {[z0)
7. δ(q1, (, ( ) = (q1, (( ) ├ (q1,], [z0)
8. δ(q1, }, { ) = (q1, Є) ├ (q1, Є, z0)
9. δ(q1, ], [ ) = (q1, Є) ├ (q2, Є, Є) Accepted
10. δ(q1, ), ( ) = (q1, Є)
11. δ(q1, (, { ) = (q1, ({ ) 1.Try for the string [{( ) }].
12. δ(q1, {, [ ) = (q1, {[ )
13. δ(q1, Є, z0) = (q2, Є) 2.try for the string [( ) ( ) ]
Q.Evaluate or trace for the string [{ }] 3.Try for the string {[ } ]. Note: this should be rejected.
Construct a PDA accepting Language L = {wwR | w ε ( a+b)* and wR is reversal
of w}
Solution:
NPDA for wwR w ε (a,b)* where wR is reversal of w}
STACK Transiton Function:
δ(q0, a, Z) = (q0, aZ)
δ(q0, a, a) = (q0, aa)
δ(q0, b, Z) = (q0, bZ)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)
δ(q0, a, a) = (q1, ε) // decision step
δ(q0, b, b) = (q1, ε) // decision step
δ(q1, b, b) = (q1, ε)
δ(q1, a, a) = (q1, ε)
δ(q1, ε, Z) = (qf, Z)
NOTE: NPDA are designed when there is no subtle decision like arrival of ‘c’ on wcwR w ε (a,b)*
Lets see, how this NPDA is working:
We will take one input string: "abbbba”
• Scan string from left to right
• First input is 'a' and follow the rule: on input 'a' and STACK alphabet Z, push the two 'a's into STACK as : (a,Z/aZ)
and state will be q0
• Second input is 'b' and so follow the rule: on input 'b' and STACK alphabet 'a', push the 'b' into STACK as :
(b,a/ba) and state will be q0
• Third input is 'b' and so follow the rule: on input 'b' and STACK alphabet 'b', push the 'b' into STACK as :
(b,b/bb) and state will be q0
Here we will assume that one delimiter comes either that could be 'a' or 'b’
• Fourth input is 'b' and so follow the rule:on input 'b' and STACK alphabet 'b' (state is q1), pop one 'b' from
STACK as : (b,b/ε) and state will be q1
• Fifth input is 'b' and so follow the rule: on input 'b' and STACK alphabet 'b' (state is q1), pop one 'b' from STACK
as : (b,b/ε) and state will be q1
• Sixth input is 'a' and so follow the rule: on input 'a' and STACK alphabet 'a' and state q1, pop one 'a' from STACK
as : (a,a/ε) and state will remain q1
• We reached end of the string, so follow the rule:on input ε and STACK alphabet Z, go to final state(qf) as : (ε,
Z/Z)
Here a decision tree will get formed to come to the solution.

Thus NPDA is more powerful than DPDA


Construct a PDA accepting Language L = {wwR | w ε ( 0,1)* and wR is
reversal of w}

SOLUTION: Similar to previous only alphabets are 0,1 instead of a,b


Equivalence of CFG and PDA
Now we will show that the language defined by PDA’s are exactly the context
free language. The goal is to prove that the CFG and PDA accept the same class
of language. For this we show:

1) From CFG to PDA:


2) From PDA to CFG:
1) Context-Free Grammar to PDA
If a grammar G is context-free, we can build an equivalent nondeterministic PDA which accepts
the language that is produced by the context-free grammar G. A parser can be built for the
grammar G.
Also, if P is a pushdown automaton, an equivalent context-free grammar G can be constructed
where. L(G) = L(P)
1) Algorithm to find PDA corresponding to a given CFG:: i.e. From CFG to PDA
Input − A CFG, G = (V, T, P, S)
Output − Equivalent PDA, P = (Q, Σ, Г, δ, q0, z0, F)
Step 1 − Convert the productions of the CFG into GNF. (NOT MANDATORY; See Example 2)
Step 2 − The PDA will have only one state {q}.
Step 3 − The start symbol of CFG will be the start symbol in the PDA.
Step 4 − All non-terminals, terminals 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.
Step 5: For non-terminal symbol, add the following rule:
δ(q, ε, A) = (q, α) Where the production rule is A → α
Step 6: For each terminal symbols, add the following rule:
δ(q, a, a) = (q, ε) for every terminal symbol
Example1: Convert the following grammar to a PDA that accepts the same language.
S → 0S1 | A
A → 1A0 | S | ε
SOLUTION:
Step1:
The CFG can be first simplified by eliminating unit productions:
S → 0S1 | 1S0 | ε
Now we will convert this CFG to GNF:
S → 0SX | 1SY | ε
X→1
Y→0
The PDA’s rules can be:
R1: δ(q, ε, S) = {(q, 0SX) | (q, 1SY) | (q, ε)}
R2: δ(q, ε, X) = {(q, 1)}
R3: δ(q, ε, Y) = {(q, 0)}
R4: δ(q, 0, 0) = {(q, ε)}
R5: δ(q, 1, 1) = {(q, ε)}
R6: δ(q, ε, z0)={(q, ε)} (IF WE ASSUME z0 as initial stack symbol)
P = ({q}, {0, 1}, {0,1,S,X,Y}, δ,q,z0,q) (Q, Σ, Г, δ, q0, z0, F)
Example2: Draw a PDA for the CFG given below:
S → aSb
S→a|b|ε
SOLUTION: (conversion without converting to GNF)
The PDA can be given as:
P = ({q}, {a, b}, {S, a, b, z0}, δ, q, z0, q)
The mapping function δ will be:
R1: δ(q, ε, S) = {(q, aSb)}
R2: δ(q, ε, S) = {(q, a) | (q, b) | (q, ε)}
R3: δ(q, a, a) = {(q, ε)}
R4: δ(q, b, b) = {(q, ε)}
R5: δ(q, ε, z0) = {(q, ε)}
Consider the string aaabb
δ(q, aaabb, S) OR δ(q, εaaabb, S)
⊢ δ(q, aaabb, aSb) R3
⊢ δ(q, aabb, Sb) OR ⊢ δ(q, εaabb, Sb) R1
⊢ δ(q, aabb, aSbb) R3
⊢ δ(q, εabb, Sbb) R2
⊢ δ(q, abb, abb) R3
⊢ δ(q, bb, bb) R4
⊢ δ(q, b, b) R4
⊢ δ(q, ε, z0) R5
⊢ δ(q, ε) RULES FROM PREVIOUS SLIDE:
R1: δ(q, ε, S) = {(q, aSb)}
ACCEPT R2: δ(q, ε, S) = {(q, a) | (q, b) | (q, ε)}
R3: δ(q, a, a) = {(q, ε)}
R4: δ(q, b, b) = {(q, ε)}
R5: δ(q, ε, z0) = {(q, ε)}
Example 3: Construct a PDA from the following CFG.
G = ({S, X}, {a, b}, P, S)
where the productions are −
S → XS | ε , A → aXb | Ab | ab
Solution:
Let the equivalent PDA,
P = ({q}, {a, b}, {a, b, X, S}, δ, q,z0, q) P = (Q, Σ, Г, δ, q0, z0, F)
where δ −
δ(q, ε , S) = {(q, XS), (q, ε )}
δ(q, ε , X) = {(q, aXb), (q, Ab), (q, ab)}
δ(q, a, a) = {(q, ε )}
δ(q, 1, 1) = {(q, ε )}
Example 4: CFG to PDA
Let G = (V, T, P and S) where,P is
EàT | E + T
Tà F | T*F
Fà a | (E)
We can define a PDA equivalent to this grammar as;
M = ({q0}, {a, *, +, (, )},{a, *, +, (, ), E, T, F}, δ, q0, z0, Φ}
Where δ can be defined as;
δ(q0, Є, E) = {(q0, T), (q0,E + T)}
δ(q0, Є, T) = {(q0, F), (q0,T*F)}
δ(q0, Є, F) = {(q0, a), (q0,(E))}
δ(q0, a, a) = {(q0, Є)}
δ(q0, *, *) = {(q0, Є)}
δ(q0, +, +) = {(q0, Є)}
δ(q0, (, ( ) = {(q0, Є)}
δ(q0, ),) ) = {(q0, Є)}
Now with this PDA, M, let us trace out acceptance of a + (a*a);
(q0, a + (a*a), E)
├ (q0, a + (a*a), E + T)
├ (q0, a + (a*a), T + T)
├ (q0, a + (a*a), F + T)
├ (q0, a + (a*a), a + T) In CFG
├ (q0, + (a*a), + T) Eà E + T
├ (q0, (a*a), T) àE+T
àT+T
├ (q0, (a*a), F)
àF + T
├ (q0, (a*a), (E)) àa + T
├ (q0, a*a), E)) àa + F
àa + (E)
├ (q0, a*a), T))
àa + (T*F)
├ (q0, a*a), T*F) à a + (F*F)
├ (q0, a*a), F*F) àa + (a*F)
├ (q0, a*a), a*F) àa + (a*a)

├ (q0, *a), *F)


├ (q0, a), F))
├ (q0, a), a))
├ (q0,),))
├ (q0, Є, Є) Accepted (acceptance by empty stack).
• Exercise
• Convert the grammar defined by following production into PDA;
• Sà0S1 | A
• Aà 1S0 | S | .Also we trace acceptance of aaabaaaaa.
2. PDA to CFG:
The construction of a context-free grammar from a PDA is not really difficult,
but it is quite messy.
The construction is simplified if we first convert a PDA to an equivalent PDA
such that for every move (q, Г) ∈ δ(p, a, Z) (where a ∈ Σ ∪ {ǫ}), we have | Г | ≤
2. In some sense, we form a kind of PDA in Chomsky Normal Form.
2) From PDA to CFG:
Input − PDA, P = (Q, Σ, Г, δ, q0, z0, F)
Output − Equivalent CFG, G = (V, T, P, S)

The productions in P are induced by moe of PDA as follows:


Step1: For initial symbol (S) productions are given by:
S → [ q0 z0 q] for every q ε Q
Step2: For every popping move
δ(q, a, z) = (q’ , ε) induces production
[q z q’] → a
Step3: For every push move
δ(q, a, z) = (q1, z1z2…zn) induces production
[q z q’] → a [q1 z1 q2][q2 z2 q3]…..[qm zm q’]
Where each state q’ , q1, q2, …. qm can be any state.
Example: Convert following PDA to CFG
States: q0, q1 Input alphabet: {0, 1} Stack alphabet: {A, B, C}
Transition function:
δ(q0, ε, ε) = (q1, $) δ(q1, 0, $) = (q1, A$) δ(q1, 1, A) = (q1, AB) δ(q1, 0, A) = (q1, AC) δ(q1, ε, $) = (q0, ε)
This PDA recognizes the language {0^n 1^n | n ≥ 0}.
Solution:
Step 1: Create a new start symbol S, and add a production S → [q0 ε q1] (NOTE: OR S → [q0 z0 q1] )
Step 2: For each popping move, add a production of the form [q z q'] → a.
In this PDA, the only popping move is δ(q1, ε, $) = (q0, ε), so we add the production [q1 $ q0] → ε.
Step 3: For each pushing move, add a production of the form [q z q'] → a [q1 z1 q2][q2 z2 q3]...[qm zm q'], where q1, q2, ..., qm
are any states in the PDA. In this PDA, there are three pushing moves:
•δ(q1, 0, $) = (q1, A$) induces the production [q1 $ q1] → 0[q1 A q1]
•δ(q1, 1, A) = (q1, AB) induces the production [q1 A q1] → 1[q1 A q1][q1 B q1]
•δ(q1, 0, A) = (q1, AC) induces the production [q1 A q1] → 0[q1 A q1][q1 C q1]

Putting all these productions together, we get the following CFG:


S → [q0 ε q1]
[q1 $ q0] → ε
[q1 $ q1] → 0[q1 A q1]
[q1 A q1] → 1[q1 A q1][q1 B q1] | 0[q1 A q1][q1 C q1]
[q1 B q1] → 1[q1 B q1] | ε
[q1 C q1] → 0[q1 C q1] | ε
This CFG generates the same language as the original PDA, namely {0^n 1^n | n ≥ 0}.
Pushdown Automata & 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 −
•Top-Down Parser − Top-down parsing starts from the top with the
start-symbol and derives a string using a parse tree.
•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 −
•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.
•If the top symbol of the stack matches with the input symbol being read, pop it.
•Push the start symbol ‘S’ into the stack.
•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
If the PDA is (Q, ∑, S, δ, q0, I, F), then the top-down parsing is −
(x+y*z, I) ⊢(x +y*z, SI) ⊢ (x+y*z, S+XI) ⊢(x+y*z, X+XI)
⊢(x+y*z, Y+X I) ⊢(x+y*z, x+XI) ⊢(+y*z, +XI) ⊢ (y*z, XI)
⊢(y*z, X*YI) ⊢(y*z, y*YI) ⊢(*z,*YI) ⊢(z, YI) ⊢(z, zI) ⊢(ε, I)
Design of a Bottom-Up Parser
For bottom-up parsing, a PDA has the following four types of transitions −
•Push the current input symbol into the stack.
•Replace the right-hand side of a production at the top of the stack with its left-
hand side.
•If the top of the stack element matches with the current input symbol, pop it.
•If the input string is fully read and only if the start symbol ‘S’ remains in the stack,
pop it and 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
If the PDA is (Q, ∑, S, δ, q0, I, F), then the bottom-up parsing is −
(x+y*z, I) ⊢ (+y*z, xI) ⊢ (+y*z, YI) ⊢ (+y*z, XI) ⊢ (+y*z, SI)
⊢(y*z, +SI) ⊢ (*z, y+SI) ⊢ (*z, Y+SI) ⊢ (*z, X+SI) ⊢ (z, *X+SI)
⊢ (ε, z*X+SI) ⊢ (ε, Y*X+SI) ⊢ (ε, X+SI) ⊢ (ε, SI)
Construction of PDA by Final State: To construct a PDA by final state, we follow these
steps:
1.Start with an empty stack and push a special symbol, say Z0, onto the stack.
2.Read the input string from left to right and use the current input symbol and the top
stack symbol to determine the next move.
3.Based on the move, push or pop a symbol from the stack or leave it unchanged.
4.When the entire input has been read, check if the PDA has reached a final state. If it
has, the input string is accepted; otherwise, it is rejected.
Construction of PDA by Empty Stack: To construct a PDA by empty stack, we follow these
steps:
1.Start with an empty stack and push a special symbol, say Z0, onto the stack.
2.Read the input string from left to right and use the current input symbol and the top
stack symbol to determine the next move.
3.Based on the move, push or pop a symbol from the stack or leave it unchanged.
4.If the PDA reaches a state where the input has been completely read and the stack is
empty, the input string is accepted; otherwise, it is rejected.
Conversion of PDA by Final State to PDA accepting by Empty Stack:
To convert a PDA by final state to a PDA accepting by empty stack, we follow these steps:
1.Add a new start state q0 and an epsilon transition from q0 to the original start state q1.
2.For every final state in the original PDA, add epsilon transitions to a new final state qf
and make it the only final state.
3.Replace every transition of the form (q, a, z, q', z') with (q, a, zX, q', z’) & (q', ε, X, qf, ε),
where X is a new symbol not in the original PDA's alphabet.
4.Finally, add a transition from qf to q0 with epsilon on the input and $ on the stack.

Conversion of PDA by Empty Stack to PDA accepting by Final State:


To convert a PDA by empty stack to a PDA accepting by final state, we follow these steps:
1.Add a new start state q0 and an epsilon transition from q0 to the original start state q1.
2.Add a new symbol Z not in the original PDA's alphabet and add a transition from every
state that can reach the empty stack to a new state qf with Z on the stack.
3.For every transition of the form (q, a, z, q', epsilon), add a transition (q, a, z, qf, Z).
4.Finally, make qf the only final state.

You might also like