0% found this document useful (0 votes)
12 views8 pages

Jan-June 2025 Btcs 4 Sem v10 Btcs404 Btcs404 Unit4 Notes

This document provides an overview of Pushdown Automata (PDA), detailing its structure, components, and capabilities compared to Finite Automata (FA). It explains the formal definition of a PDA, its components, and terminology, along with examples of designing PDAs for specific languages. Additionally, it discusses acceptance criteria for PDAs, including acceptance by final state and empty stack, and highlights the relationship between PDAs and context-free grammars.

Uploaded by

ved26raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Jan-June 2025 Btcs 4 Sem v10 Btcs404 Btcs404 Unit4 Notes

This document provides an overview of Pushdown Automata (PDA), detailing its structure, components, and capabilities compared to Finite Automata (FA). It explains the formal definition of a PDA, its components, and terminology, along with examples of designing PDAs for specific languages. Additionally, it discusses acceptance criteria for PDAs, including acceptance by final state and empty stack, and highlights the relationship between PDAs and context-free grammars.

Uploaded by

ved26raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

KALINGA UNIVERSITY

Department of Computer Science


Unit – 4
COURSE: BTCSAIML SEM/Year: 4th Sem
SUBJECT: TOAFL (BTCS 404)

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.

Terminology Related to PDA:


 Instantaneous Description (ID):
o The instantaneous description of a PDA is represented by a triplet (q, w, s)
where:
 q is the state
 w is unconsumed input
 s is the stack contents
 Turnstile Notation:
o The “turnstile” notation is used for connecting pairs of ID’s that represent one
or many moves of a PDA. The process of transition is denoted by the turnstile
symbol “├”.
o Consider a PDA (Q, Ʃ, S, δ, q0, I, F). A transition can be mathematically
represented by the following turnstile notation: (p, aw, Tβ) ├ (q, w, αb).
o This implies that while taking a transition from state ‘p’ to state ‘q’, the input
symbol ‘a’ is consumed and the top of the stack ‘T’ is replaced by a new string
‘α’.
o Note: If we want zero or more moves of a PDA, we have to use the symbol
(├*) for it.
Example 1: Design a PDA for accepting a language {anb2n | n>=1}.
 Solution:
o In this language, n number of a's should be followed by 2n number of b's.
Hence, we will apply a very simple logic, and that is if we read single 'a', we
will push two a's onto the stack. As soon as we read 'b' then for every single 'b'
only one 'a' should get popped from the stack.

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, ε)

δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ) ⊢ δ(q0, abbbbbb, aaaaZ)


 Now we will simulate this PDA for the input string "aaabbbbbb".

⊢ δ(q0, bbbbbb, aaaaaaZ) ⊢ δ(q1, bbbbb, aaaaaZ)


⊢ δ(q1, bbbb, aaaaZ) ⊢ δ(q1, bbb, aaaZ)
⊢ δ(q1, bb, aaZ) ⊢ δ(q1, b, aZ)
⊢ δ(q1, ε, Z) ⊢ δ(q2, ε)
ACCEPT
Example 2: Design a PDA for accepting a language {0n1m0n | m, n>=1}.
 Solution:
o In this PDA, ‘n’ numbers of 0’s are followed by any number of 1's followed n
number of 0's. Hence the logic for design of such PDA will be as follows:
 Push all 0's onto the stack on encountering first 0's. Then if we read 1,
just do nothing. Then read 0, and on each read of 0, pop one 0 from the
stack.
 For instance:

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)

δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z)


 Now we will simulate this PDA for the input string "0011100".

⊢ δ(q0, 11100, 00Z)


⊢ δ(q0, 1100, 00Z)
⊢ δ(q1, 100, 00Z)
⊢ δ(q1, 00, 00Z)
⊢ δ(q1, 0, 0Z)
⊢ δ(q1, ε, Z)
⊢ δ(q2, Z)
ACCEPT
PDA Acceptance:
 A language can be accepted by Pushdown automata using two approaches:
 Acceptance by Final State:
o The PDA is said to accept its input by the final state if it enters any final state
in zero or more moves after reading the entire input.
o Let P = (Q, Ʃ, S, δ, q0, I, F) be a PDA. The language acceptable by the final
state can be defined as: L (PDA) = {w | q0, w, Z) ├* (p, ϵ, ϵ), q ϵ F}
 Acceptance by Empty Stack:
o On reading the input string from the initial configuration for some PDA, the
stack of PDA gets empty.
o Let P = (Q, Ʃ, S, δ, q0, I, F) be a PDA. The language acceptable by empty
stack can be defined as: N(PDA) = {w | q0, w, Z) ├* (p, ϵ, ϵ), q ϵ Q}
 Equivalence of Acceptance by Final State and Empty Stack:
o If L = N(P1) for some PDA P1, then there is a PDA P2 such that L = L(P2). That
means the language accepted by final state PDA.
o If there is a language L = L(P 1) for some PDA P1 then there is a PDA P 2 such
that L = N(P2). That means language accepted by final state PDA is also
acceptable by empty stack PDA.
Example 1:
 Construct a PDA that accepts the language L over {0, 1} by empty stack which
accepts all the string of 0's and 1's in which a number of 0's are twice of number of
1's.
Solution:
 There are two parts for designing this PDA:
o If 1 comes before any 0's
o If 0 comes before any 1's.

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:

PDA for L= {0n1n | n ≥ 0}


 This language accepts L = {ϵ, 01, 0011, 000111 ...}. Here, in this example, the
number of ‘a’ and ‘b’ have to be same.
o Initially we put a special symbol ‘$’ into the empty stack.
o 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.
o 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.

Example 3:
 Construct a PDA that accepts L= {wwR | w = (a + b)*}

5
Solution:

PDA for L= {wwR | w = (a + b)*}


 Initially we put a special symbol ‘$’ into the empty stack. At state q2, the w is being
read. In state q3, each 0 or 1 is popped when it matches the input. If any other input is
given, the PDA will go to a dead state. When we reach that special symbol ‘$’, we go
to the accepting state q4.
Non-Deterministic Pushdown Automata:
 The non-deterministic pushdown automata are very much similar to NFA. We will
discuss some CFGs which accepts NPDA.
 The CFG which accepts deterministic PDA accepts non-deterministic PDAs as well.
Similarly, there are some CFGs which can be accepted only by NPDA and not by
DPDA. Thus NPDA is more powerful than DPDA.
Example:
 Design PDA for Palindrome strips.
Solution:
 Suppose the language consists of string L = {aba, aa, bb, bab, bbabb, aabaa, ......}.
The string can be odd palindrome or even palindrome. The logic for constructing
PDA is that we will push a symbol onto the stack till half of the string then we will
read each symbol and then perform the pop operation. We will compare to see
whether the symbol which is popped is similar to the symbol which is read. Whether
we reach to end of the input, we expect the stack to be empty.
 This PDA is a non-deterministic PDA because finding the mid for the given string and
reading the string from left and matching it with from right (reverse) direction leads to
non-deterministic moves. Here is the ID.

6
Simulation of abaaba:

⊢ δ(q1, baaba, aZ)


δ(q1, abaaba, Z) Apply rule 1

⊢ δ(q1, aaba, baZ)


Apply rule 5

⊢ δ(q1, aba, abaZ)


Apply rule 4 `

⊢ δ(q2, ba, baZ)


Apply rule 7

⊢ δ(q2, a, aZ)
Apply rule 8

⊢ δ(q2, ε, Z)
Apply rule 7

⊢ δ(q2, ε)
Apply rule 11
Accept

Pushdown Automata and Context Free Grammar:


 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).
 In the next two topics, we will discuss how to convert from PDA to CFG and vice
versa.
Algorithm to find PDA corresponding to a given CFG
Input: A CFG, G= (V, Ʃ, R, S)
Output: Equivalent PDA, P= (Q, Ʃ, S, δ, q0, I, F)
 Step 1: Convert the productions of the CFG into GNF.
 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 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 each production in the form A→ aX where a is terminal and A, X are
combination of terminal and non-terminals, make a transition δ(q, a, A).
Problem:
 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, S)
where δ: δ (q, ϵ, S) = {(q, XS), (q, ϵ )}
δ(q, ϵ, X) = {(q, aXb), (q, Xb), (q, ab)}, δ(q, a, a) = {(q, ϵ)}
δ(q, 1, 1) = {(q, ϵ)}
Algorithm to find CFG corresponding to a given PDA:
Input: A CFG, G= (V, Ʃ, R, S)
Output: Equivalent PDA, P = (Q, Ʃ, S, δ, q0, I, F) such that the non- terminals of the
grammar G will be {Xwx | w, x ϵ Q} and the start state will be Aq0, F
Step 1: For every w, x, y, z ϵ Q, m ϵ S and a, b ϵ S, if δ(w, a, ϵ) contains (y, m) and (z, b, m)
contains (x, ϵ), add the production rule Xwx → a Xyzb in grammar G.
Step 2: For every w, x, y, z ϵ Q, add the production rule Xwx → XwyXyx in grammar G.
Step 3: For w ϵ Q, add the production rule Xww → ϵ in grammar G.

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:

(x + y * z, I) ⊢ (x + y * z, SI) ⊢ (x + y * z, S + XI) ⊢ (x + y * z, X + XI)


 If the PDA is (Q, Ʃ, S, δ, q0, I, F), then the top-down parsing is:

⊢(x + y * z, Y + XI) ⊢ (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) ⊢ (e, I)
Design of a Bottom-Up Parser:
 For bottom-up parsing, a PDA has the following four types of transitions:
o Push the current input symbol into the stack.
o Replace the right-hand side of a production at the top of the stack with its left-
hand side.
o If the top of the stack element matches with the current input symbol, pop it.
o 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:

(x + y * z, I) ⊢ (+y * z, xI) ⊢ (+y * z, YI) ⊢ (+y * z, XI) ⊢ (+y * z, SI)


 If the PDA is (Q, Ʃ, S, δ, q0, I, F), then the bottom-up parsing is:

⊢ (y * z, + SI) ⊢ (*z, y + SI) ⊢ (*z, Y + SI) ⊢ (*z, X + SI) ⊢ (z, *X + SI)


⊢ (e, z * X + SI) ⊢ (e, Y * X + SI) ⊢ (e, X + SI) ⊢ (e, SI)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy