Module 4
Module 4
q0 a q1 a q2 a q3
b b b b
a/b b b
a/b q7 q6 q5 q4
a a
a
Introduction
• A Finite State Automaton (FSA) is a mathematical model used in computing to
design computer programs and sequential logic circuits.
• It recognizes exactly the regular languages.
How can we design an automaton with limited states but unlimited memory?
Pushdown automaton (PDA) Model
• Pushdown automaton (PDA) is "Finite state machine" + "a stack"
• PDA has three components
• An input tape, • The stack head scans the top symbol of the stack.
• A control unit, and • A stack does two operations
• A stack with infinite size • 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 must read the top of the stack in every
transition.
• PDA is used as a language acceptor.
• It characterizes the Context Free Languages
(CFL).
Fig. Model of Pushdown Automata • Used to recognize certain types of structured
inputs.
Pushdown automaton (PDA)
A PDA is a non-deterministic machine defined by the seven-tuple
M = (Q, Σ, Г, δ, q0, z0, F) where
Q A finite set of states
Σ A finite input alphabet
Г A finite stack alphabet
q0 The initial / starting state, q0 is in Q
z0 A starting stack symbol, z0 is in Г
F A set of final / accepting states, which is a subset of Q
δ A transition function, (Triple) defines as
δ: Q x (Σ U {ε}) x Г finite subsets of Q x Г*
• The language of a PDA is the set of strings that the PDA accepts:
L(M) = { w ∈ Σ* | M accepts w }
Example PDA#1
L (M)= { anbn | n ≥ 0} over Σ = { a, b }
IDEA: An infinite pushdown store (stack) solves the problem for {anbn | n ≥ 0} as
follows:
• If ‘a’ is read on the input tape, push it on to stack.
• If character ‘b’ is read on the input tape, pop the corresponding ‘a’ from the stack
(or fail if not matched).
• When input is consumed and if the stack is empty (containing only Z0 or z0) accept
the String otherwise reject it.
Example PDA#1
L (M)= { anbn | n ≥ 0} over Σ = { a, b }
String: aaabbb
Input Stack
a a a b b b Z0
a a a b b b Z0 a
a a a b b b Z0 a a
a a a b b b Z0 a a a
Example PDA#1
L (M)= { anbn | n ≥ 0} over Σ = { a, b }
String: aaabbb
Input Stack
a a a b b b Z0 a a a
a a a b b b Z0 a a
a a a b b b Z0 a
String Accepted
Example PDA#1
String: abbb
Input Stack
a b Z0
b b
Z0 a
a b b b
a b b b Z0
IDEA:
• Length of any string w ∈ L is odd, w must have a symbol ‘#’ exactly in the middle
position;
• i.e., |w| = 2n+1 for some n ≥ 0, and the (n+1)th symbol in w is the middle one.
• If a string w of length 2n + 1 satisfies w = wR, the first n symbols must match (in
reverse order) the last n symbols, and the middle symbol doesn’t have to match
anything.
• Here, do comparison not counting.
PDA#2: PDA for Palindromes
• Place input head upon leftmost input symbol on tape.
• while symbol being scanned ≠ #
if symbol scanned = ‘a’, put a on stack
if symbol scanned = ‘b’, put b on stack
Advance input head to next symbol
• Advance input head past #
• Repeat
if (symbol scanned = ‘a’ and stack[top] = ‘a’)
or (symbol scanned = ‘b’ and stack[top] = ‘b’)
then remove top disk;
advance input head until (Stack is empty) or (no input remains) or
(no element is removed).
• If input has been read and Stack is empty, then accept
PDA#2: PDA for Palindromes
w = ab#ba
Input a b # b a
b b b
Stack a a a a a
• At the right end of the input, the machine reads the ‘a’ and removes the ‘a’ from
the stack.
• The stack is empty, accepts the string.
PDA#2: PDA for Palindromes
w = ab#b
Input a b # b
b b b
Stack a a a a a
q1 #, Z0 →Z0 (7) q2
#, a → a (8)
b, Z0 → bZ0 (4) #, b → b (9) #, Z0 →Z0 (13)
b, b → bb (5) #, a → a (14)
b, a → ba (6) #, b → b (15)
q3
What about the string ab##ba? #, Z0 →Z0 (16)
[Two or more # symbols] Reject #, a → a (17)
#, b → b (18)
PDA#2: PDA for Palindromes
M = ({q1, q2, q3}, {a, b, #}, {Z0, a, b}, δ, q1, Z0, {q2})
δ:
(1) δ(q1, a, Z0) = {(q1, aZ0)} (4) δ(q1, b, Z0)={(q1, bZ0)}
(2) δ(q1, a, a) = {(q1, aa)} (5) δ(q1, b, b) = {(q1, bb)}
(3) δ(q1, a, b) = {(q1, ab)} (6) δ(q1, b, a) = {(q1, ba)}
(7) δ(q1, #, Z0) = {(q2, Z0)}
(8) δ(q1, #, a) = {(q2, a)} (9) δ(q1, #, b) = {(q2, b)}
(10) δ(q2, a, a) = {(q2, ε)} (11) δ(q2, b, b) = {(q2, ε)}
(12) δ(q2, ε, Z0) = {(q2, ε)}
(12) δ(q2, #, Z0) = {(q3, Z0)} (16) δ(q3, #, Z0) = {(q3, Z0)}
(13) δ(q2, #, a) = {(q3, a)} (17) δ(q3, #, a) = {(q3, a)}
(14) δ(q2, #, b) = {(q3, b)} (18) δ(q3, #, b) = {(q3, b)}
PDA#3:
Language of balanced parenthesis:
Example: (())() ()()
IDEA
– On seeing a ( push “(” on to the stack
– On seeing a ) pop “(” from the stack
– If attempt to pop an empty stack, reject
– If stack not empty at the end, reject
– Else accept
M = ({q1}, {(, )}, {(, Z0}, δ, q1, Z0, Ø) where δ:
1. δ(q1, (, Z0) = {(q1, (Z0)} // L-on top-then- # lower
2. δ(q1, (, () = {(q1, (()}
3. δ(q1, ), () = {(q1, ε)}
4. δ(q1, ε, Z0) = {(q1, ε)} //if ε read & stack hits bottom, accept
5. δ(q1, ), Z0) = Ø // illegal, string rejected
6. δ(q1, ε, L) = Ø // illegal, string rejected
PDA#3:
Transition Diagram: M = ({q1}, {(, )}, {(, Z0}, δ, q1, Z0, Ø)
ε, Z0 |ε q1 (, ( | ((
String:(()) ), ( | ε
Current Input Stack Transition
(()) Z0 initial status
()) (Z0 (1) Push (
)) ((Z0 (2) Push (
) (Z0 (3) Pop L
ɛ Z0 (3) Pop L
- - (4) Pop Z0
PDA#4: L(M)= {aibjck|i,j,k ≥ 0, i = j or j = k }
Case 1:
• If the string is ai bj ck with i = j, then match for a’s and b’s irrespective of numbe
of c’s.
• i=j=1, k=0 ab
• i=j=2 k=1 aabbc
• i=j=3 k= 8 aaabbbcccccccc
Case-2 b, b → bb
ε, Z0→ Z0 a, Z0 → Z0 c, b → ε
b, Z0 → bZ0 ε, Z0 → ε q5
X q3 q4
Remove initial states of PDA for L (M)= {aibncn | j=k=n}
Case-1 & Case-2
M = ({q0, q1, q2, q3, q4, q5}, {a,b,c}, {a,b,Z0}, δ, q0, Z0, {q2, q5})
PDA#5: L(M)= {aibjck|i,j,k ≥ 0, and i+j=k }
Example Strings:
ac bc abcc aabccc
IDEA:
• For every ‘a’ and ‘b’ read, push an ‘x’ onto the stack.
• For every ‘c’ pop ‘x’ from the stack.
Pushing a and b
on to stack
c, x → ɛ
a, Z0 → xZ0
ɛ, Z0 → ɛ
a, x → xx b, x → xx
q1 b, Z0 → xZ0 c, x → ɛ q3
q2
number of a’s q2
multiple of 2 ɛ, Z0 → ɛ q6
a, Z0 → Z0 a, Z0 → xZ0
a, x → xx
a, x → x b, x → x
q1 ɛ, x → x q4
ɛ, Z0 → ɛ number of b’s
multiple of 3
b, Z0 → Z0 b, x → ɛ
b, x → x
q3 q5
PDA#7: Equal number of 'a's and 'b's
a,$ a$ b,$ b$
a,a aa b,b bb
a,b b,a
q1 , $ $ q2
M = ({q1, q2}, {a,b}, {a,b,Z0}, δ, q1, $, {q2})
PDA#8: PDA for no. of 'a’s > [Link] 'b‘s
a, Z0 → aZ0
Matching for a, a → aa
ε, Z0 → ε
a’s and b’s a, b → ε
ε, a → ε q2
q1
Checking for
more a’s than b’s
b, Z0 → bZ0
b, b → bb
b, a → ε
M = ({q1, q2}, {a,b}, {a,b,Z0}, δ, q1, Z0, {q2})
PDA#9: PDA for no. of 'a’s < [Link] 'b‘s
Example: b, abb, aabbb
a, Z0 → aZ0
a, a → aa
Matching for a, b → ε ε, Z0 → ε
a’s and b’s
q1 ε, b → ε q2
b, Z0 → bZ0 Checking for
b, b → bb more b’s than a’s
b, a → ε
b, a → a q1 a, a → ɛ
q0 q2
M = ({q0, q1, q2}, {a,b}, {a,Z0}, δ, q1, Z0, φ)
PDA#11: PDA for Arithmetic
ARITH = {w ∈ Σ* | w is a legal arithmetic expression, where Σ = { int, +, *, (, ) } }
Example Strings:
• int + int * int
• ((int + int) * (int + int)) + (int)
• Can we build a PDA for ARITH?
q1 q2 q3
q2
a, b c
q1 q1 , b c q
2
a, b c q
transition
3
Non-Deterministic PDA
a, ɛ → a
b, ɛ → b • The transition does not pop anything from the stack.
• It just pushes on a new symbol instead.
• This transition means “don't consume any input, don't change the top of the
stack, and don‘t add anything to a stack”.
• It's the equivalent of an ε-transition in NFA.
PDA#12: L {x | x = wwr and w in {0,1}*}
Note: length |x| is even
IDEA
• Push the symbols read onto the stack.
• At each point non-deterministically guess, that middle of the string has been
reached or if the next symbol read is the middle of the string and will not be
put on the stack.
• Pop off the symbols from the stack if they match the input symbols read.
• If the symbols popped are the same symbols that were pushed on earlier and
the stack empties as the input is finished, then accept. Otherwise, reject.
PDA#12: L {x | x = wwr and w in {0,1}*}
• This transition indicates lets us consume one character before start matching
what was just saw.
• This idea is useful to match Even-length palindromes
PDA#12: L {x | x = wwr and w in {0,1}*}
0, Z0 / 0 Z0
1, Z0 / 1 Z0
0, 0 / 0 0
0, 1 / 0 1
1, 0 / 1 0 0, 0 / ε
1, 1 / 1 1 1, 1 / ε
Start
q0 q1 q2
ε, Z0 / Z0 ε, Z0 / Z0
ε, 0 / 0
ε, 1 / 1
PDA#12: L {x | x = wwr and w in {0,1}*}
(q0, 1111, Z0)
ε WRONG GUESS
1 ε
(q0, 111, 1Z0) (q1, 1111, Z0) (q2, 1111, Z0)
1
ε
1
(q0, 11, 11Z0) (q1, 111, 1Z0) (q1, 11, Z0)
1
ε WRONG GUESS 1
(q0, 1, 111Z0) (q1, 11, 11Z0) 1 (q2, 11, Z0)
1
ε
(q0, ε, 1111Z0) (q1, 1, 111Z0) (q1, 1, 1Z0)
1
1 1
(q1, ε, 1111Z0) (q1, ε, 11Z0) (q1, ε, Z0)
WRONG GUESS ε
WRONG GUESS CORRECT GUESS (q2, ε, Z0)
PDA
PDA#1: L (M)= { anbn | n ≥ 0}
PDA#2: L(M) = {w|w#wR , w ɛ {a,b}*}
PDA#3: L(M) = {w| Matching of Parenthesis, w ɛ {(,)}*}
PDA#4: L(M) = {aibjck|i,j,k ≥ 0, and i = j or j = k }
PDA#5: L(M) = {aibjck|i,j,k ≥ 0, and i +j = k }
PDA#6: L(M) = {a2nb3n|n ≥ 0}
PDA#7: L(M) = {w|w has equal number of a’s and b’s}
PDA#8: L(M) = {w|w has a greater number of a’s than b’s}
PDA#9: L(M) = {w|w has a greater number of b’s than a’s}
PDA#10: L(M) = {anbman|n,m ≥ 1}
PDA#11: L(M) = {w ∈ Σ* | w is a legal arithmetic expression,
where Σ = { int, +, *, (, ) }
PDA#12: L(M) = {x | x = wwR , w ɛ {0,1}*}
Instantaneous Description (ID)
• M = (Q, Σ, Г, δ, q0, z0, F) be a PDA, and configuration of PDA at any given
instance is an instantaneous description (ID) is a triple (q, w, γ), where q is in
Q, w is in Σ* and γ is in Г*.
• q is the current state
• w is the unused input
• γ is the current stack contents
ε, Z0 → ε (3)
Language L has a PDA that accepts it by final state if and only if L has a PDA that
accepts it by empty stack
PE
PE
ε, Z0 → Z0X0
Z0
X0
Initial Stack
Figure: PF simulates PE and accepts if PE empties the stack
Stack Contents
X0 serves two purposes: by the time
• It is the start symbol of the stack. PE completed
• It acts as a marker to detect when the stack is empty in PE. X0 Empty Stack
Detecting an Empty Stack:
• When PF sees X0 at the top of stack, then it knows that PE would empty its stack on the same input.
Introducing a New Accepting State pf
PDA CONVERSION: EMPTY STACK TO FINAL STATE
The specification of PF is as follows:
PF = (QU{p0, pf}, Σ, Γ U {X0}, δF, p0, X0, {pf})
where δF is defined by:
a) δF(p0, ε, Z0) = {(q0, Z0X0)}. In its start state, PF makes a spontaneous transition to the start
state of PE, pushing its start symbol X0 onto the stack.
b) For all states q in Q, inputs a in Σ or a = ε, and stack symbols Y in Γ, δF(q, a, Y) contains all
the pairs in δE(q, a, Y).
c) In addition to rule (b), δF(q, ε, X0) contains (pf, ε) for every state q in Q.
PDA CONVERSION: EMPTY STACK TO FINAL STATE
(, Z0|(Z0
(, (|((
(, Z0|(Z0 ), (|ε
ε, Z0|ε (, (|(( ε, Z0|ε
q0
p0 q0 pf
), (|ε ε, Z0|Z0X0 ε, X0|ε
PDA for matching of PDA for matching of
Parenthesis by Empty Stack Parenthesis by Final State
PDA CONVERSION: FINAL STATE TO EMPTY STACK
Idea:
• Converting PF to PE (Accept by Empty Stack)
• Empty Stack Mechanism:
– Input is exhausted.
– Stack is empty
ε, Z0 → Z0X0
number of a’s q2
multiple of 2 ɛ, Z0 → ɛ q6
a, Z0 → Z0 a, Z0 → xZ0
a, x → xx ɛ, X0 → ɛ
a, x → x b, x → x
q1 ɛ, x → x q4 p
ɛ, Z0 → ɛ number of b’s
multiple of 3
q0 b, Z0 → Z0 b, x → ɛ
b, x → x
q3 q5
EQUIVALENCE OF PDA AND CFG
[p X _ ] → a [r Y _] [ _ Z _]
• Spaces (_) to be filled with states.
• These are variables, so format is [Variable Stack_symbol Variable]
[p X _] → a [r Y _ ] [ _ Z _]
Same
PDA to CFG
Productions of G:
Case 3: Writing the Productions for push operations
Suppose δ(p, a, X) = (r, Y1,…Yk)
for some state r and k > 3.
Same
PDA to CFG
Productions of G:
Completion of the Grammar Construction:
To prove that (q0, w, Z0)⊦*(p, ε, ε) if and only if [q0 Z0 p] =>* w.
• Here, state p can be anything.
• Thus, add to G another variable S, the start symbol, and add productions S -> [q0
Z0 p] for each state p.
PDA to CFG a, Z0 → XZ0 ɛ, Z0 → ɛ
Example: a, X → XX b, X → ɛ
q0 b,X → ɛ q1
Same
EQUIVALENCE OF PDA AND CFG
δ(p, a, X) = (r, YZ) then [p X q] -> a[r Y s][s Z q] for all states s.
[Here q can be q0, q1and s can be q0, q1]
1) δ(q0, a, Z0) = (q0, XZ0)
[q0 Z0 q0] → a [q0 X q0] [q0 Z0 q0] All Possible Combinations
[q0 Z0 q0] → a [q0 X q1] [q1 Z0 q0]
[q0 Z0 q1] → a [q0 X q0] [q0 Z0 q1] 3) δ(q0, b, X) = (q1, ε) [q0 X q1] → b
[q0 Z0 q1] →a [q0 X q1] [q1 Z0 q1]
2) δ(q0, a, X) = (q0, XX) 4) δ(q1, b, X) = (q1, ε) [q1 X q1] → b
[q0 X q0] → a [q0 X q0] [q0 X q0]
[q0 X q0] → a [q0 X q1] [q1 X q0]
5) δ(q1, ε, Z0) = (q1, ε) [q1 Z0 q1] → ε
[q0 X q1] → a [q0 X q0] [q0 X q1]
[q0 X q1] →a [q0 X q1] [q1 X q1]
Finding useful productions
Finding useful productions based on S
S → [q0 Z0 q0] – Not useful, Remove
S → [q0 Z0 q1] Since (q0, aabb, Z0)⊦*(q1, ε, ε) // (q0, aabb, Z0)⊦*(q1, ε, ε) if and only if [q0 Z0 q1] =>* w.
Since [q0 Z0 q0] is eliminated, productions involving in [q0 Z0 q0] on LHS or RHS can be removed.
[q0 X q1] → b [q0 Z0 q0] → a [q0 X q0] [q0 Z0 q0] - Remove [q0 X q0] → a [q0 X q0] [q0 X q0]
[q0 Z0 q0] → a [q0 X q1] [q1 Z0 q0] - Remove [q0 X q0] → a [q0 X q1] [q1 X q0]
[q1 X q1] → b
[q0 Z0 q1] → a [q0 X q0] [q0 Z0 q1] [q0 X q1] → a [q0 X q0] [q0 X q1]
[q1 Z0 q1] → ε [q0 Z0 q1] →a [q0 X q1] [q1 Z0 q1] [q0 X q1] →a [q0 X q1] [q1 X q1]
Finding useful productions
Finding useful productions based on only terminal or ε on RHS
[q0 X q1] → b
[q1 X q1] → b
[q1 Z0 q1] → ε
As non terminals [q0 X q1], [q1 X q1], and [q1 Z0 q1] are able to terminate.
Non terminals like [q0 X q0] are not able to terminate remove them if they are on LHS or RHS of
any production
S → [q0 Z0 q1]
a, Z0 → XZ0 ε, Z0 → ε
Matching for a, X → XX ε, X → ε
a’s and b’s a, Y → ε
ε, X → ε q2
q1
Checking for more
a’s than b’s
b, Z0 → YZ0
b, Y → YY
b, X → ε
Convert the PDA M = ({q1, q2}, {a,b}, {X,Y,Z0}, δ, q1, Z0, φ) into CFG.
[q1Z0 -] a[q1 X -][- Z0-]
Problems
Convert the following PDA M = ({q}, {0,1}, {Z, A, B}, δ, q, Z, φ) into CFG.
1. δ(q,0,Z)=(q, AZ)
2. δ(q,1,Z)=(q, BZ)
3. δ(q,0,A)=(q, AA)
4. δ(q,1,B)=(q, BB)
5. δ(q,1,A)=(q, ε) Solution
6. δ(q,0,B)=(q, ε) Start Symbol S→ [qZq] CFG,
7. δ(q, ε,Z)=(q, ε) 1. [qBq] →0 S → ε (3)
2. [qAq] →1 S →0AS (4)
3. [qZq] →ε S →1BS (5)
4. [qZq] →0[qAq][qZq] A →0AA (6)
5. [qZq] →1[qBq][qZq] B →1BB (7)
6. [qAq] →0[qAq][qAq] A →1 (2)
7. [qBq] →1[qBq][qBq] B →0 (1)
Non-deterministic PDA (NPDA) & Deterministic PDA ( DPDA)
Non-deterministic PDA (NPDA):
• An NPDA is a pushdown automaton where multiple transitions are possible for a
given input, stack symbol, and state. This means:
• The automaton may choose among different transitions.
• It accepts a string if at least one sequence of choices leads to an accepting state
or an empty stack.
Deterministic PDA
• A DPDA has at most one valid transition per input symbol, stack symbol, and
state. This means:
• It cannot "guess" the right path.
• It must have a unique transition for every input-symbol and stack-symbol pair.
• Not all context-free languages (CFLs) can be recognized by a DPDA.
Deterministic Pushdown Automata
A Pushdown Automaton (PDA) is deterministic if, for every possible configuration (state, input
symbol, and stack top), there is at most one possible transition. In other words, there is no
situation where multiple moves are possible.
• If δ(q, a, X) contains more than one pair, then PDA is non-deterministic
• If δ(q, a, X) is always a singleton, then have a choice between using a real input symbol,
or making a move on ε.
A PDA P = (Q, Σ, Γ, δ, q0, Z0, F) is deterministic, iff the following conditions are met
• δ(q, a, X) has at most one member for any q in Q, a in Σ or a = ε, and X in Γ
• δ(q, a, X) is nonempty, for some a in Σ, then δ(q, ε, X) must be empty
[q0 Z0 q0] → a [q0 X q0] [q0 Z0 q0] | a [q0 X q1] [q1 Z0 q0] | a [q0 X q2] [q2 Z0 q0]
[q0 Z0 q1] → a [q0 X q0] [q0 Z0 q1] | a [q0 X q1] [q1 Z0 q1] | a [q0 X q2] [q2 Z0 q1]
[q0 Z0 q2] →a [q0 X q0] [q0 Z0 q2] | a [q0 X q1] [q1 Z0 q2] | a [q0 X q2] [q2 Z0 q2]
[q0 X q0] → a [q0 X q0] [q0 X q0] | a [q0 X q1] [q1 X q0] | a [q0 X q2] [q2 X q0]
[q0 X q1] → a [q0 X q0] [q0 X q1] | a [q0 X q1] [q1 X q1] | a [q0 X q2] [q2 X q1]
[q0 X q2] → a [q0 X q0] [q0 X q2] | a [q0 X q1] [q1 X q2] | a [q0 X q2] [q2 X q2]
EQUIVALENCE OF PDA AND CFG
Input string: aabb
EQUIVALENCE OF PDA AND CFG
Identifying Useful Productions:
S → [q0 Z0 q2] [q1 X q1] → b [q1 Z0 q2] → ε
[q0 Z0 q0] → a [q0 X q0] [q0 Z0 q0] | a [q0 X q1] [q1 Z0 q0] | a [q0 X q2] [q2 Z0 q0]
[q0 Z0 q1] → a [q0 X q0] [q0 Z0 q1] | a [q0 X q1] [q1 Z0 q1] | a [q0 X q2] [q2 Z0 q1]
[q0 Z0 q2] → a [q0 X q0] [q0 Z0 q2] | a [q0 X q1] [q1 Z0 q2] |a [q0 X q2] [q2 Z0 q2]
[q0 X q0] → a [q0 X q0] [q0 X q0] | a [q0 X q1] [q1 X q0] | a [q0 X q2] [q2 X q0]
[q0 X q1] → a [q0 X q0] [q0 X q1] | a [q0 X q1] [q1 X q1] | a [q0 X q2] [q2 X q1]
[q0 X q2] → a [q0 X q0] [q0 X q2] | a [q0 X q1] [q1 X q2] | a [q0 X q2] [q2 X q2]
EQUIVALENCE OF PDA AND CFG
Final Grammar Productions are:
S → [q0 Z0 q2]
[q1 X q1] → b
[q1 Z0 q2] → ε
[q0 Z0 q2] → [q1 Z0 q2]
[q0 X q1] → [q1 X q1]
[q0 Z0 q2] → a [q0 X q1] [q1 Z0 q2]
[q0 X q1] → a [q0 X q1] [q1 X q1]