Unit IV PUSHDOWN AUTOMATA AND POST MACHINES
Unit IV PUSHDOWN AUTOMATA AND POST MACHINES
Unit IV PUSHDOWN AUTOMATA AND POST MACHINES
1
FA +Stack =Pushdown Automaton -- PDA
Input String
Stack
States
What is?
◼ FA to Reg Lang, PDA is to CFL
Formal Definition
Pushdown Automaton (PDA)
M = (Q, Σ, Γ, δ, q0 , z , F )
Accept
States states
Input Stack
alphabet Transition Initial start
Stack
function state symbol
alphabet
Transition Function: δ
Deterministic PDA:
δ(Q x {∑ U ε} x Г)=Q x Г*
Non-Deterministic PDA:
Q x Г*
δ(Q x {∑ U ε} x Г)=2
δ(Q
Q x {∑
∑ U ε}
ε xГ
Г)=Q
Q x Г*
input a, b → c
q1 q2
a
stack stack
States b top c
h h
e e
$ $
Initial Stack Symbol
Stack Stack
stack
$ z top
head
a, b → c
q1 q2
a, b → c
q1 q2
input
a a
stack
b top c
h Replace h
e e
$ $
a, b →
q1 q2
input
a a
stack
b top
h Pop h
e e
$ $
a, →
q1 q2
input
a a
stack
b top b
h No Change h
e e
$ $
Pop from Empty Stack
a, b → c
q1 q2
input
a
stack Pop Automaton halts!
top
a, b → c q2
, b → c
q1 q1 q2
a, b → c
− transition
q3
Example PDA
PDA M : L( M ) = {a b : n 1}
n n
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
L( M ) = {a b : n 0}
n n
Basic Idea:
3. Match
a, a → aa found
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
a, a → aa Transition
a,$ → a$ b, a → Diagram
q1 b, a → q2 , $ → $ q3
δ( q1x ax $)=(q1,a$)
δ( q1x ax a)=(q1,aa)
Transition
δ( q1x bx a)=(q2,ε)
function
δ( q2x bx a)=(q2,ε)
δ( q2x ε x $)=(q3,$)
PDA Acceptance by Final State
a, a → aa Transition
a,$ → a$ b, a → Diagram
q1 b, a → q2 ,$ →
δ( q1x ax $)=(q1,a$)
δ( q1x ax a)=(q1,aa)
Transition
δ( q1x bx a)=(q2,ε)
function
δ( q2x bx a)=(q2,ε)
δ( q2x ε x $)=(q2,ε)
PDA Acceptance by Empty Stack
Final State
ACCEPTANCE
PDA
Empty Stack
Execution Example: Time 0
Input
a a a b b b
$
Stack
a, a → aa
a,$ → a$ b, a →
Current/
initial b, a → q , $ → $ q
q1 2 3
state
Time 1
Input
a a a b b b a
$
Stack
a, a → aa
a,$ → a$ b, a →
b, a → q , $ → $ q
q1 2 3
Time 2
Input a
a a a b b b a
$
Stack
a, a → aa
a,$ → a$ b, a →
b, a → q , $ → $ q
q1 2 3
Time 3
a
Input
a
a a a b b b a
$
Stack
a, a → aa
a,$ → a$ b, a →
b, a → q , $ → $ q
q1 2 3
Time 4
a
Input
a
a a a b b b a
$
Stack
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Time 5
Input a
a a a b b b a
$
Stack
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Time 6
Input
a a a b b b a
$
Stack
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Time 7
Input
a a a b b b
$
Stack
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
A string is accepted if there is
a computation such that:
a, a → aa
a,$ → a$ b, a →
Current State
q1 b, a → q2 , $ → $ q3
Rejection Example: Time 2
Input
a
a a b
$
Stack
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Rejection Example: Time 3
Input a
a
a a b
$
Stack
a, a → aa
a,$ → a$ b, a →
current
state b, a → , $ → $
q1 q2 q3
Rejection Example: Time 4
Input a
a
a a b
$
Stack
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Rejection Example: Time 4
Input a
a
a a b
$
Stack
reject
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
There is no accepting computation for aab
The string aab is rejected by the PDA
a, a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Example PDA
q1 , $ → $ q2
b, b → bb b, a →
b,$ → b$ a,b →
Example PDA
a, a → aa
a,$ → a$ b, a → c,$ → $
q1 b, a → q2
c,$ → $
q3
, $ → $
Review
• PDA
• Formal Definition of PDA
• Transition Function
• Transition Diagram
• Design of PDA
• Examples
• Acceptance by Final State
• Acceptance by Empty Stack
Example PDA
a, a → aa
a,$ → a$ b, a → c,$ → $
b, a → c,$ → $ , $ → $ q
q1 q2 q3 4
a, a → aa
a,$ → a$ b, a → c,$ → $
q1 b, a → q2
c,$ → $
q3
a, a → aa
a,$ → a$ b, a → c,$ → $
q1 b, a → q2
c,$ → $
q3
Acceptance by Empty ,$ →
stack
Example PDA
PDA M : L( M ) = {a b : n 1}
n n
a, → a b, a →
q1 b, a → q2 , $ → $ q3
Problems
1. Construct PDA that accepts language
L = { an bm cn | m, n > 1}
(Nov-2017 EndSem 8 Marks)
2.Design a PDA for the language
L = {an bm cn | m, n > =l} by empty stack.
(Nov-2016 EndSem 8 Marks)
Example PDA
a, a → aa
a,$ → a$ c,a →
b,a → a
q1 b,a → a q2 c,a → q3 , $ → $ q3
q1 b,a → a q2 c,a → q3
,$ →
Acceptance by Empty Stack
Example PDA
2.Construct PDA to check for well formedness
of paranthesis.
(Nov-2017 EndSem 8 Marks)
(, (→ ((
(,$ → ($
, $ → $ q
q1 3
Acceptance by
),(→ final state
Construct a PDA for the language described as “The set of
all strings over ∑= {a, b} with equal no. of a’s and b’s.
(Nov-2017 EndSem 8 Marks)
a, a → aa
a,$ → a$
q1 , $ → $ q2
b, b → bb b, a →
b,$ → b$ a,b →
Example PDA
PDA M :L(M) such that anb2n| n>=1
(Nov-2017 EndSem 8 Marks)
a, a → aaa
a,$ → aa$ b,a →
q1 b,a → q2 , $ → $ q3
Review
• PDA Examples
• Acceptance by Final State
• Acceptance by Empty Stack
Another PDA example
L( M ) = {w {a, b} : na ( w) = nb ( w)}
*
PDA M
a, $ → 0$ b, $ →1$
a, 0 → 00 b, 1 → 11
a, 1→ b, 0 →
q1
, $ → $ q2
Example PDA
q1 a,$ → $ q2 , $ → $ q
3
b, b → bb b, a →
b,$ → b$ a,b →
Example PDA
q1 b,$ → $ q2 , $ → $ q
3
b, b → bb b, a →
b,$ → b$ a,b →
Example PDA
q1 b,a → q2 c,b → q , $ → $ q3
3
b,$ → b$
b, b → bb
Another PDA example
PDA M : L( M ) = {vcv : v {a, b} }
R
a,$ → a b,$ → b
a,a → aa b,b → bb
a,b → ab b,a → ba
a, → a a, a →
b, → b b, b →
q0 c,a → a q1 , $ → $ q2
c,b → b
Formalities for PDAs
a , w1 → w 2
q1 q2
Transition function:
(q1 , a ,w 1 ) = {(q2 ,w 2 )}
a , w1 → w 2 q2
q1
a , w1 → w 3 q3
Transition function:
q1 q2 , $ → $ q
a,$|a$ 3
q0
a,$|$
q4 q5 , $ → $ q6
PDA M : L( M ) = {vv : v {a, b} }
R
a,$ → a
a,a → aa a, a →
a,b → ab b, b →
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
PDA M : L(M ) = {vv : v {a, b} }
R
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Execution Example: Time 0
Input
a b b a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b →
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 1
Input
a b b a
a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 2
Input
a b b a b
a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 3
Input
Guess the middle
a b b a b
of string
a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 4
Input
a b b a b
a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $ q2
q0 q1
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 5
Input
a b b a
a
a,$ → a
$
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 6
Input
a b b a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2 accept
b, b →
b,$ → b
b,b → bb
b,a → ba
Rejection Example: Time 0
Input
a b b b
a,$ → a
$
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2 accept
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 1
Input
a b b b a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 2
Input
b
a b b b a
a,$ → a
$
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 3
Input
b
a b b b Guess the middle a
a,$ → a of string $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 4
Input There is no possible transition.
a
a b b b Input is not
$
a,$ → a consumed
a,a → aa a, a → Stack
a,b → ab b, b →
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Another computation on same string:
Input Time 0
a b b b
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 1
Input
a b b b a
a,$ → a
$
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 2
Input
b
a b b b a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 3
Input b
b
a b b b a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 4 b
Input b
b
a b b b a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
Time 5 b
Input There is no possible transition. b
No accept state b
a b b b is reached a
a,$ → a $
a,a → aa a, a →
a,b → ab b, b → Stack
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
There is no computation
that accepts string abbb
abbb L(M )
a,$ → a
a,a → aa a, a →
a,b → ab b, b →
a, a → , $ → $
q0 q1 q2
b, b →
b,$ → b
b,b → bb
b,a → ba
PDA for Lwwr: Transition Diagram
Grow stack
Pop stack for
0, Z0/0Z0 ∑ = {0, 1}
1, Z0/1Z0 matching
= {Z0, 0, 1}
0, 0/00 symbols
Q = {q0,q1,q2}
0, 1/01 0, 0/
1, 0/10 1, 1/
1, 1/11
, Z0/Z0
q0 , 0/0 q1 , Z0/Z0 q2
, 1/1
Switch to
Go to acceptance
popping mode
(q0,1,111Z0) (q1,11,11Z0)
( q, u , s )
Current Current
Remaining
state stack
input
contents
PDA’s Instantaneous Description (ID)
A PDA has a configuration at any given instance:
(q,w,y)
◼ q - current state
◼ w - remainder of the input (i.e., unconsumed part)
◼ y - current stack contents as a string from top to bottom of
stack
If δ(q,a, X)={(p, A)} is a transition, then the following are also true:
◼ (q, a, X ) |--- (p,,A)
q1 b, a → q2 , $ → $ q3
Example: Instantaneous Description
(q2 , bb, aa$)
a
Time 5: Input a
a a a b b b a
$
a,a → aa
Stack
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
We write:
a,a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
(q0 , aaabbb,$) (q1, aaabbb,$)
(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$)
(q2 , bb, aa$) (q2 , b, a$) (q2 , ,$) (q3 , ,$)
(q0 , aaabbb,$) (q3 , ,$)
Language of PDA
L( M ) = {w : (q 0 , w,$) (q f , ,$)}
aaabbb L(M )
PDA M :
a,a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
(q0 , a b ,$) (q3 , ,$)
n n
a b L(M )
n n
PDA M :
a,a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Therefore: L( M ) = {a b : n 0}
n n
PDA M :
a,a → aa
a,$ → a$ b, a →
q1 b, a → q2 , $ → $ q3
Review
Context-Free
Languages
(Grammars)
= Languages
Accepted by
PDAs
Proof - Step 1:
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Context-Free Grammars
to
PDAs
Take an arbitrary context-free grammar G
L(G ) = L( M )
Procedure to Convert CFG to PDA
• G = (V, ∑, S, P)
• NT(G)=(Q, ∑, Г,q0,$,A, δ)
Q = {q0, q1, q2} A = {q2} Г = V ∪ ∑ ∪ {$}
• On initial state, δ(q0,λ, λ) = {(q1, S)}
• The moves from q1 are the following:
For every A ∈ V ,
δ(q1,λ,A) = {(q1, α) | A → α is a production in G}
For every terminal a∈ ∑,
δ(q1, a, a) = {(q1, λ)}
• For accepting state
δ(q1, λ,$) = {(q2,$)}
S→bAb
Conversion Procedure: A→aA|a
For each For each
Variable/production in G terminal in G
A→a a
A → aA
Add transitions
S → bAb b
, A → a
, A → aA
a, a →
, S → bAb
b, b →
q0 , → S q1 , $ → $ q2
Formal construction of PDA
from CFG Note: Initial stack symbol (S)
same as the start variable
in the grammar
◼ Given: G= (V,T,P,S)
◼ Output: PN = ({q}, T, V U T, δ, q, S)
◼ δ:
For all A V , add the following
Before: After:
◼
A
transition(s) in the PDA:
…
…
◼ δ(q, ,A) = { (q, ) | “A ==> ” P}
Before:
◼ For all a T, add the following After: a…
a transition(s) in the PDA: a pop
…
δ(q,a,a)= { (q, ) }
…
◼
Example
Grammar
S → aSTb
PDA
S →b
T → Ta , S → aSTb
T → , S → b
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Example:
Input a b a b
, S → aSTb $
Time 0
, S → b Stack
, T → Ta a, a →
, T → b, b →
, → S , $ → $
q0 q1 q2
Derivation: S
Input a b a b
S
, S → aSTb $
Time 1
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb a
S
Input a b a b T
b
, S → aSTb $
Time 2
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb a
S
Input a b a b T
b
, S → aSTb $
Time 3
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb
b
Input a b a b T
b
, S → aSTb $
Time 4
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb
b
Input a b a b T
b
, S → aSTb $
Time 5
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb abTab
T
Input a b a b a
b
, S → aSTb $
Time 6
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb abTab abab
T
Input a b a b a
b
, S → aSTb $
Time 7
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb abTab abab
Input a b a b a
b
, S → aSTb $
Time 8
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb abTab abab
Input a b a b
b
, S → aSTb $
Time 9
, S → b Stack
, T → Ta a, a →
, T → b, b →
q0 , → S q1 , $ → $ q2
Derivation: S aSTb abTb abTab abab
Input a b a b
, S → aSTb $
Time 10
, S → b Stack
, T → Ta a, a →
, T → b, b →
accept
q0 , → S q1 , $ → $ q2
Grammar PDA Computation
Leftmost Derivation
(q0 , abab,$)
S (q1 , abab, S $)
aSTb (q1 , bab, STb$)
(q1 , bab, bTb$)
abTb
(q1 , ab, Tb$)
abTab (q1 , ab, Tab$)
abab (q1 , ab, ab$)
(q1 , b, b$)
(q1 , ,$)
(q2 , ,$)
Example: CFG to PDA
1,1 /
0,0 /
,A / 01
◼ G = ( {S,A}, {0,1}, P, S) ,A / A1
,A / 0A1
,S /
◼ P: ,S / AS
◼ S ==> AS | , / S
,$ / $
◼ A ==> 0A1 | A1 | 01
q0 q p
◼ PDA = ({q0,q,p}, {0,1}, {0,1,A,S}, δ, q0, S)
◼ δ:
◼ δ(q0, , )=(q,S)
◼ δ(q, 0, 0) = { (q, ) }
◼ δ(q, 1, 1) = { (q, ) }
◼ δ(q, , $) = { (P, $ ) }
How will this new PDA work? Lets simulate string 0011
Simulating string 0011 on the
new PDA … Leftmost deriv.:
1,1 /
0,0 /
,A / 01
,A / A1
,A / 0A1 S => AS
PDA (δ): ,S /
δ(q0 , , )=(q,S) ,S / AS => 0A1S
δ(q, , S) = { (q, AS), (q, )}
,S / S
=> 0011S
δ(q, , A) = { (q,0A1), (q,A1), (q,01) }
,S / S => 0011
δ(q, 0, 0) = { (q, ) } q0 q p
δ(q, 1, 1) = { (q, ) }
0 0
A A 1 1
A 1 1 1 1 1
Accept by
S S S S S S S S
final state
0 0 1 1
Grammar G
generates PDA M
string If and accepts w
w Only if
* (q0 , w,$) (q2 , ,$)
Sw
Therefore L(G ) = L( M )
Construct PDA equivalent to the following CFG.
S->0A1 | 0BA (May-2017 EndSem 8 Marks)
A->S01 | 0
B->1B | 1 , S → 0 A1
, S → 0 BA
, A → S 01
, A → 0
, B → 1B 1 ,1 →
, B → 1 0,0 →
q0 , → S q1 , $ → $ q2
There are two types of PDAs that one can design:
those that accept by final state or by empty stack
Acceptance by…
◼ PDAs that accept by final state:
◼ For a PDA P, the language accepted by P,
denoted by L(P) by final state, is: Checklist:
◼ {w | (q0,w,Z0) |---* (q,, A) }, s.t., q F - input exhausted?
- in a final state?
,Z0/ Z0 start
start
q0 q1 q0
,Z0/ Z 0 ,Z0/ Z0
Let
P = (Q,Σ,Γ,δ,q0,Z0,F)
be a PDA. The language accepted by P by final state is:
∗
L(P) = {w| (q0,w,Z0) ⊢(q,ǫ,α),q∈F}
for some state qin F and any input stack string α.
Let
P = (Q,Σ,Γ,δ,q0,Z0,F)
be a PDA. The language accepted by P by empty stack is:
∗
N(P) = {w| (q0,w,Z0) ⊢(q,ǫ,ǫ)}
where qis any state
1,2,8,13,14,22,26,27,28,45,46,52,57,58,61,62,63,64,68,71,73,76,77
Review
• Equivalence of Context free language and PDA
From Empty stack to Final State
Theorem:
If L = N(PN ) for some PDA PN , then there exist a PDA PF , such
that
L = L(PF )
From Empty Stack to Final State
Qf = Qn ∪ {p0, pf }.
Γf = Γn ∪ {X0}.
Ff = {pf }.
δf is defined by
1.δf (p0, ε, X 0) = {(q0, Z0X0)}. In its start state, Pf makes a
spontaneous transition to the start state of Pn, pushing its start
symbol Z0 onto the stack.
2.For all state q ∈ Qn, inputs a ∈ Σ n or a = ε, and stack symbol Y ∈
Γn, δf (q, a, Y ) contains all the pairs in δn(q, a, Y ).
3.In addition to rule (2), δf (q, ε, X 0) contains (pf , ε) for every
state q ∈ Qn.
From Final State to Empty Stack
Context-free languages
are closed under: Union
L1 is context free
L1 L2
L2 is context free is context-free
Example
Language Grammar
L1 = {a b }
n n
S1 → aS1b |
L2 = {ww } R
S 2 → aS 2 a | bS 2b |
Union
L = {a b } {ww }
n n R S → S1 | S 2
In general:
For context-free languages L1, L2
with context-free grammars G1, G2
and start variables S1, S2
Context-free languages
are closed under: Concatenation
L1 is context free
L1L2
L2 is context free is context-free
Example
Language Grammar
L1 = {a b }
n n
S1 → aS1b |
L2 = {ww } R
S 2 → aS 2 a | bS 2b |
Concatenation
L = {a b }{ww }
n n R S → S1S 2
In general:
Context-free languages
are closed under: Star-operation
*
L is context free L is context-free
Example
Language Grammar
L = {a b }
n n
S → aSb |
Star Operation
L = {a b } *
n n
S1 → SS1 |
In general:
Context-free languages
are not closed under: intersection
L1 is context free
L1 L2
L2 is context free not necessarily
context-free
Example
L1 = {a b c }
n n m
L2 = {a b c }
n m m
Context-free: Context-free:
S → AC S → AB
A → aAb | A → aA |
C → cC | B → bBc |
Intersection
L1 L2 = {a b c } NOT context-free
n n n
Complement
Context-free languages
are not closed under: complement
L1 = {a b c }
n n m
L2 = {a b c }
n m m
Context-free: Context-free:
S → AC S → AB
A → aAb | A → aA |
C → cC | B → bBc |
Complement
L1 L2 = L1 L2 = {a b c }
n n n
NOT context-free
Review
• Acceptance by Empty stack to Final State
• Acceptance by final state to empty stack
Design a PDA which accepts only odd number
of a’s over Σ = {a, b}.
Simulate PDA for the string “aabab”.
(9 Marks Nov-2015)
Pumping Lemma
for
Context-free Languages
Return of the Pumping Lemma !!
154
Why pumping lemma?
155
Take an infinite context-free language
aabbccddee
A possible derivation:
A B E
b e E
A a S D
a e
b B d d
S → ABE | bBd
c c
A → Aa | a
Repeated B → bSD | cc
D → Dd | d
variable E → eE | e
Derivation Tree aabbccddee
S
A B E
b e E
A a S D
a e
b B d d
S → ABE | bBd
c c
A → Aa | a
Repeated B → bSD | cc
D → Dd | d
variable E → eE | e
B bSD bbBdD bbBdd
b S D
b B d d
S → ABE | bBd
* A → Aa | a
B bbBdd B → bSD | cc
D → Dd | d
E → eE | e
S ABE AaBE aaBE aaBeE aaBee
A B E
e E
A a
a e
* S → ABE | bBd
S aaBee A → Aa | a
B → bSD | cc
D → Dd | d
E → eE | e
B
c c
B cc S → ABE | bBd
A → Aa | a
B → bSD | cc
D → Dd | d
E → eE | e
S → ABE | bBd
A → Aa | a Putting all together
B → bSD | cc S
D → Dd | d
E → eE | e
A B E
e E
A a b S D
a e
b B d d
c c
* *
S aaBee B bbBdd B cc
We can remove the middle part
A B E
c c e E
A a
a e
S → ABE | bBd
* A → Aa | a
0 0
S aa (bb) cc(dd ) ee B → bSD | cc
D → Dd | d
E → eE | e
* *
S aaBee B bbBdd B cc
* *
0 0
S aaBee aaccee = aa (bb) cc(dd ) ee
S → ABE | bBd
A → Aa | a
0 0
aa (bb) cc(dd ) ee L(G ) B → bSD | cc
D → Dd | d
E → eE | e
We can repeated middle part two times
S
A B E
b e E
A a S D
a 1 b B d d
e
b S D
2 b B d d
S → ABE | bBd
c c A → Aa | a
B → bSD | cc
*
2 2
S aa (bb) cc(dd ) ee D → Dd | d
E → eE | e
* *
S aaBee B bbBdd B cc
* *
S aaBee aabbBddee
* *
2 2 2 2
aa (bb) B (dd ) ee aa (bb) cc(dd ) ee
S → ABE | bBd
A → Aa | a
2 2
aa (bb) cc(dd ) ee L(G )D → Dd | d B → bSD | cc
E → eE | e
We can repeat middle part three times
S
A B E
b e E
A a S D
a 1 b B d d
e
b S D
2 b B d d
b S D
3 b B d d
S → ABE | bBd
c c A → Aa | a
B → bSD | cc
*
3 3
S aa (bb) cc(dd ) ee D → Dd | d
E → eE | e
* *
S aaBee B bbBdd B cc
*
3 3
S aa (bb) cc(dd ) ee L(G )
S → ABE | bBd
A → Aa | a
B → bSD | cc
D → Dd | d
E → eE | e
Repeat middle part i times
S
A B E
b e E
A a S D
a 1 b B d d
e
b S D
i b B d d S → ABE | bBd
A → Aa | a
c c
B → bSD | cc
*
i i
S aa (bb) cc(dd ) ee D → Dd | d
E → eE | e
* *
S aaBee B bbBdd B cc
*
i i
S aa (bb) cc(dd ) ee L(G )
yield u z yield
H
yield v y yield
H
u , v, x, y , z :
Strings of terminals x yield
Example: S
A B E
b e E
A a S D
u = aa a b B d d
e
u v y z
v = bb c c
x = cc x S → ABE | bBd
y = dd B corresponds to H A → Aa | a
B → bSD | cc
z = ee D → Dd | d
E → eE | e
Possible derivations S
S uHz
u z
H
H vHy
v y
H
Hx
x
Example: u = aa
v = bb
S
A B E x = cc
e E
y = dd
A a b S D
z = ee
a e
b B d d
c c B correspond s to H
S → ABE | bBd
S uHz H vHy H x A → Aa | a
B → bSD | cc
S aaBee B bbBdd B cc D → Dd | d
E → eE | e
Remove Middle Part S
S uHz
u z
H
Hx
x
Yield: uxz = uv xy z0 0
*
S uHz uxz = uv xy z
0 0
L(G )
Repeat Middle part two times
S
S uHz
u z
H
H vHy 1
v y
H
H vHy v y
2
H
H x
x
Yield: uvvxyyz = uv xy z
2 2
S uHz H vHy Hx
* *
S uHz uvHyz uvvHyyz
*
2 2
uvvxyyz = uv xy z L(G )
Repeat Middle part i times
S
S uHz
u z
H
H vHy 1
v H
y
H
H vHy v y i
H
H x x
Yield: uv xy z
i i
S uHz H vHy H x
S uHz uvHyz uvvHyyz
uv Hy z uv xy z L(G)
i i i i
Therefore,
| w | n
If we know that: w = uvxyz L(G )
For all i 0
since
L(G ) = L − {}
i i
uv xy z L
Observation 1: S
| vy | 1
Since G has no u
H
z
unit and
-productions
v H y
x
| vxy | n
u z
since in subtree H
only variable H
is repeated v y
H
subtree of H
The Pumping Lemma:
For any infinite context-free language L
there exists an integer m such that
uv xy z L,
i i
for all i 0
Applications
of
The Pumping Lemma
Non-context free languages
{a b c : n 0}
n n n
Context-free languages
{a b : n 0}
n n
Theorem: The language
L = {a b c : n 0}
n n n
We pick: w=a b cm m m
L = {a b c : n 0}
n n n
w=a b c
m m m
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
uv xy z L
i i
for all i0
L = {a b c : n 0}
n n n
w=a b c
m m m
w = uvxyz | vxy | m | vy | 1
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
m
Case 1: vxy is in a
m m m
a ...aa ...aa ...a bbb ...bbb ccc ...ccc
u vxy z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
v =a k1
y =a k2
k1 + k2 1
m m m
a ...aa ...aa ...aa ...aa ...a bbb ...bbb ccc ...ccc
u v x y z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
v =a k1
y =a k2
k1 + k2 1
m + k1 + k2 m m
a ...aa ...aa ...aa ...aa ...a bbb ...bbb ccc ...ccc
u v2 x y 2
z
L = {a b c : n 0}
n n n
w=a b c
m m m
w = uvxyz | vxy | m | vy | 1
k1 + k2 1
However: uv xy z = a
2 2 m +k1 +k2
b c L
m m
Contradiction!!!
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
Case 2: vxy is in b m
Similar to case 1
m m m
aaa ...aaa b ...bb ...bb ...b ccc ...ccc
u vxy z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
m
Case 3: vxy is in c
Similar to case 1
m m m
aaa ...aaa bbb ...bbb c ...cc ...cc ...c
u vxy z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
m m
Case 4: vxy overlaps a and b
m m m
a ...aa ...aa b ...bb ...bbb ccc ...ccc
u vxy z
L = {a b c : n 0}
n n n
w=a b cm m m
w = uvxyz | vxy | m | vy | 1
m m m
a ...aa ...aa ...a b ...bb ...bb ...b ccc ...ccc
u v x y z
L = {a b c : n 0}
n n n
w=a b c
m m m
w = uvxyz | vxy | m | vy | 1
v =a k1
y =a k2
k1 + k2 1
m m m
a ...aa ...aa ...a b ...bb ...bb ...b ccc ...ccc
u v x y z
L = {a b c : n 0}
n n n
w=a b c
m m m
w = uvxyz | vxy | m | vy | 1
v =a k1
y =a k2
k1 + k2 1
m + k1 m + k2 m
a ...aa ...aa ...a b ...bb ...bb ...b ccc ...ccc
u v2 x y 2
z
L = {a b c : n 0}
n n n
w=a b c
m m m
w = uvxyz | vxy | m | vy | 1
k1 + k 2 1
m + k1 m + k2 m
However: uv xy z = a
2 2
b c L
Contradiction!!!
L = {a b c : n 0}
n n n
w=a b cm m m
w = uvxyz | vxy | m | vy | 1
m m m
a ...aa ...a b ...bb ...bb ...bb ...b ccc ...ccc
u v x y z
L = {a b c : n 0}
n n n
w=a b c
m m m
w = uvxyz | vxy | m | vy | 1
By assumption
v =a b k1 k2
y =a k3
k1 , k2 1
m m m
k1 k2 k3
a ...aa ...a b ...bb ...bb ...bb ...b ccc ...ccc
u v x y z
L = {a b c : n 0}
n n n
w=a b cm m m
w = uvxyz | vxy | m | vy | 1
v =a b k1 k2
y =a k3
k1 , k2 1
m m + k3 m
k1 k2 k1 k2 2k3
a ...aa ...ab ...ba ...ab ...bb ...bb ...bb ...b ccc ...ccc
u v 2 x y 2
z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
k1 , k2 1
However: uv xy z = a b a b
2 2 m k2 k1 m +k3
c L
m
Contradiction!!!
L = {a b c : n 0}
n n n
w=a b cm m m
w = uvxyz | vxy | m | vy | 1
Sub-case 3: v contains only a
y contains a and b
Similar to sub-case 2
m m m
a ...aa ...aa ...aa ...a b ...bb ...b ccc ...ccc
u v x y z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
m m
Case 5: vxy overlaps b and c
Similar to case 4
m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
L = {a b c : n 0}
n n n
w=a b c m m m
w = uvxyz | vxy | m | vy | 1
Impossible!
m m m
aaa...aaa bbb...bbb ccc...ccc
u vxy z
In all cases we obtained a contradiction
L = {a b c : n 0}
n n n
◼ Post Machine
◼ Examples
◼ Simulating PM on TM
◼ Simulating TM on PM
❑ SHIFT-RIGHT CYCLICALLY.
Recall
• Pumping Lemma for CFL
b, a,
q0 a, q1 #, q 2
b,
a b b a # В В В … ….
Example
◼ Design a post machine that accepts the
following language which end by a.
b, a,
q0 a, q1 #, q 2
b,
a b b a # В В В … ….
Example
◼ Design a post machine that accepts the
following language which end by a.
b, a,
q0 a, q1 #, q 2
b,
a b b a # В В В … ….
Example
◼ Design a post machine that accepts the
following language which end by a.
b, a,
q0 a, q1 #, q 2
b,
a b b a # В В В … ….
Example
◼ Design a post machine that accepts the
following language which end by a.
b, a,
q0 a, q1 #, q 2
b,
a b b a # В В В … ….
Example
◼ Design a post machine that accepts the
following language which end by a.
b, a,
q0 a, q1 #, q 2
b,
a b b a # В В В … ….
String is accepted..
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # . . . … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a . . … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a . . … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a . . … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a b . … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a b # … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a b # … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a b # … ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a b # # ….
Example
◼ Design a post machine that accepts the following
language. L={anbn|n >=0} (8 marks Nov-15)
q3
a, a b,b
#, #
q0 a, q1
b, q2
#, #
a a b b # a b # # ….
String is accepted..
POST Machine Contd.
#, #
#, #
q1 q4 a, a
a,
a,
b,
q0 a, b b, a
b, a,
#, # q2
b, q3 b,b
#, #
q6
#, #
q8
Construct Post Machine which accepts the string
over Σ = {a, b} containing numbers of ‘a’ and ‘b’
same.
#, # #, #
q1
a, b,
q5 a, a
q0 b, b
b, a,
#, # q2
q6
#, #
q4 #, #
Construct Post Machine which accepts the
Language over Σ = {a, b} is
L = {an b2n | n >= 0}
#, # a, a
b, b
q0
a, q1 b, q2 b, q3
#, #
q6
RECALL
•Post Machine
•Examples
Thank You!!!!!!!!!!!!!