TOC GTU Study Material Presentations Unit-4 05052020031442AM

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 57

2160704

Theory of Computation

Unit-4
Pushdown
Automata, CFL &
NCFL
Prof. Dixita B. Kagathara
dixita.kagathara@darshan.ac.in

Theory of Computation (2160704) 1 Darshan Institute of Engineering & Technology


Topics to be covered
• Why pushdown Automata?
• Definition: PDA
• Acceptance by PDA
• Design PDA
• CFG to PDA
• PDA to CFG
• Intersection and complement of CFL
• Pumping lemma for CFG

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 2
(2160704) Darshan Institute of Engineering & Technology
Why Pushdown Automata?

Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 3 Darshan Institute of Engineering & Technology
Why Pushdown Automata?
Exercise
• Draw Finite automata for language

Not b
Possible??

• FA have limited capability.


• This is due to the "finite memory” and "no external memory"
involved with FA.

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 4
(2160704) Darshan Institute of Engineering & Technology
Pushdown Automata
• A pushdown Automata is essentially a finite Automata with a stack data
structure.

Finite
Input Stack Accept | Reject
Control

Stack

• A PDA can write an unbounded no. of symbols on the stack and read
these symbols later.
• Writing a symbol on to the stack is called “PUSH” operation.
• Removing a symbol off the stack is called “POP” operation.
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 5
(2160704) Darshan Institute of Engineering & Technology
Definition of PDA
• A pushdown Automata(PDA) is a 7-tuple ,
where
is finite set of states
and are finite sets of i/p symbol
is stack alphabet
the initial state, is an element of Q
the initial stack symbol, is an element of
is a set of accepting states, is a subset of Q
the set of finite subsets of
The function is called transition function of .

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 6
(2160704) Darshan Institute of Engineering & Technology
Acceptance by PDA
• If is a PDA and , is accepted by if

for some and some . A language is said to be accepted by if L is


precisely the set of strings accepted by , in this case, we write .

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 7
(2160704) Darshan Institute of Engineering & Technology
Deterministic PDA
• Let be a pushdown Automata. is deterministic if there is no
configuration for which has a choice of more than one move. In
other words is deterministic if it satisfies both the following
conditions:
1. For any and , the set has at most one element.
2. For any and , if , then for every .
A language L is a deterministic context free language (DCFL) if
there is deterministic PDA accepting L.

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 8
(2160704) Darshan Institute of Engineering & Technology
Applications of PDA
• A pushdown automata is a way to implement a context free
grammar.
• PDA is used in parser design for syntactic analysis.

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 9
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for L={anbn|a,b ∈ Ʃ, n≥0} 𝒂
𝒂
𝒂
anbn If n=3 then a 3 b3 = a a a b b b

𝒃
𝒃
𝒃
𝒛𝟎
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 10
(2160704) Darshan Institute of Engineering & Technology
How to Represent PDA?
Move No State Input Stack Symbol Move(s)

a, z0|az0 Input symbol, stack symbol | PUSH/POP

b, a | ^
𝑞0 𝑞1 FIRST 2 SYMBOLS
OF THE STACK
^

a, a | aa

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 11
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for L={anbn|a,b ∈ Ʃ, n≥0}
Move No State Input Stack Symbol Move(s)

a, z0|az0 b, a | ^

b, a | ^ ^,z0 | z0
𝑞0 𝑞1 𝑞2
a, a | aa ^,z0 | z0

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 12
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for same no. of a’s & b’s.
Move No State Input Stack Symbol Move(s)

a, z0|az0
a, b|^ b, z0|bz0
b, a|^ ^,z0 | z0
𝑞0 𝑞1
a, a | aa
b, b | bb
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 13
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {anbn+m cm | n,m>=1}

an bn+m cm = an bn bm cm

If n=2 and m=3 then

= a 2 b2 b3 c 3
= aa bb bbb ccc

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 14
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {anbn+m cm | n,m>=1}
Move No State Input Stack Symbol Move(s)

a, a| aa b, a| ^ b, b| bb

b, z0| bz0
𝑞0
a, z0| az0
𝑞1
b, a| ^
𝑞2 𝑞3

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 15
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {anbn+m cm | n,m>=1}
Move No State Input Stack Symbol Move(s)

c, b| ^

c, b| ^
𝑞4
^, z0| z0

𝑞5
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 16
(2160704) Darshan Institute of Engineering & Technology
Exercise: Design PDA
1. Design PDA for anbncmdm , n,m>=1
2. Design PDA for anbmcmdn , n,m>=1
3. Design PDA for an+mbncm , n,m>=1

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 17
(2160704) Darshan Institute of Engineering & Technology
Design PDA for palindrome with middle symbol c.
• Design a PDA for following CFG.
SaSa/bSb/c
• Design PDA for L={xcxr/x∈{a,b}*}. The string in L are odd length
palindromes over {a,b}
• Strings accepted in the language are:
1.aba c aba
2.aaa c aaa
3.bab c bab
4.ab c ba

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 18
(2160704) Darshan Institute of Engineering & Technology
Design PDA for palindrome with middle symbol c. 𝒂
𝒃
𝒂
a b a ca b a

𝒂
𝒃
𝒂
𝒛𝟎
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 19
(2160704) Darshan Institute of Engineering & Technology
Design PDA for palindrome with middle symbol c.
Move No State Input Stack Symbol Move(s)

b, z0|bz0
a, z0|az0 a, a | aa

𝑞0
b, b | bb b, a | ba
a, b | ab

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 20
(2160704) Darshan Institute of Engineering & Technology
Design PDA for palindrome with middle symbol c.
Move No State Input Stack Symbol Move(s)

b, z0|bz0
a, z0|az0 a, a | aa
c,a | a

𝑞0 c,b | b

c,z0 | z0
𝑞1
b, b | bb b, a | ba
a, b | ab

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 21
(2160704) Darshan Institute of Engineering & Technology
Design PDA for palindrome with middle symbol c.
Move No State Input Stack Symbol Move(s)

b, z0|bz0 b, b | ^
a, z0|az0 a, a | aa
c,a | a

𝑞0 c,b | b

c,z0 | z0
𝑞1 ^,z0 | z0
𝑞2
b, b | bb b, a | ba
a, b | ab a, a | ^

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 22
(2160704) Darshan Institute of Engineering & Technology
String Tracing
b, z0|bz0 b, b | ^
a, z0|az0 a, a | aa
c,a | a

𝑞0 c,b | b

c,z0 | z0
𝑞1 ^,z0 | z0
𝑞2
b, b | bb b, a | ba
a, b | ab a, a | ^
Move No Resulting State Unread Input Stack

𝒒
𝑞𝟐2

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 23
(2160704) Darshan Institute of Engineering & Technology
Exercise
Trace the following string:
1. bcb
2. ab
3. acaa

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 24
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {aibjck | i,j,k>=1 & j=i or j=k}
Move No State Input Stack Symbol Move(s)

a, a| aa b, a| ^ c, z0| z0

c, z0| z0
𝑞0 a, z0| az0
𝑞1
b, a| ^
𝑞2 𝑞3

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 25
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {aibjck | i,j,k>=1 & j=i or j=k}
Move No State Input Stack Symbol Move(s)

a, a| aa b, a| ^ c, z0| z0

c, z0| z0
𝑞0 a, z0| az0
𝑞1
b, a| ^
𝑞2 𝑞3 ^, z0| z0
𝑞7
a, z0| z0 a, z0| z0 b, b| bb c, b| ^

𝑞 4 b,z | bz 𝑞5
0 0 c, b| ^
𝑞6
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 26
(2160704) Darshan Institute of Engineering & Technology
Design PDA for {aibjck | i,j,k>=1 & j=i or j=k}
Move No State Input Stack Symbol Move(s)

i=jj=i

J= ^, z0| z0
𝑞7
j=k
a, z0| z0 b, b| bb c, b| ^
a, z0| z0
^, z0| z0
J=

𝑞4 b,z0| bz0
𝑞5 c, b| ^
𝑞6
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 27
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for L={anb2n|a,b ∈ Ʃ, n≥0} 𝒂
𝒂
anb2n If n=1 then a 1 b2 = a b b

𝒃
𝒃
𝒃
𝒛𝟎
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 28
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for L={anb2n|a,b ∈ Ʃ, n≥0}
Move No State Input Stack Symbol Move(s)

a, z0|aa b, a | ^

b, a | ^ ^,z0 | z0
𝑞0 𝑞1 𝑞2
a, a | aa ^,z0 | z0

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 29
(2160704) Darshan Institute of Engineering & Technology
Design PDA for even odd length palindrome (Nondeterministic)
Move No State Input Stack Symbol Move(s)

b, z0|bz0
a, z0|az0 a, a | aa b, z0| z0
za,0 z0| z0
𝑞0 a,a | a
𝑞1
b,a | a
b, b | bb b, a | ba
a,b | b
a, b | ab
b,b | b
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 30
(2160704) Darshan Institute of Engineering & Technology
Design PDA for even odd length palindrome
Move No State Input Stack Symbol Move(s)

^,b | b
b, z0|bz0 ^,a | a
^,z0 | z0
a, z0|az0 a, a | aa a,z0 | z0

𝑞0 b,z0|z0
𝑞1
a,a | a
b,a | a
b, b | bb b, a | ba
a, b | ab a,b | b
b,b | b

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 31
(2160704) Darshan Institute of Engineering & Technology
Design PDA for even odd length palindrome
Move No State Input Stack Symbol Move(s)

^,b | b
b, z0|bz0 ^,a | a
^,z0 | z0 b, b | ^
a, z0|az0 a, a | aa a,z0|z0

𝑞0 b,z0|z0
𝑞1 ^,z0 | z0
𝑞2
a,a | a
b,b | b
b, b | bb b, a | ba
a, b | ab a,b | b a, a | ^
b,a | a

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 32
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for grammar SSS|[S]|{S}|˄
Move No State Input Stack Symbol Move(s)

{, [ | {[
{, z0|{z0 [, [|[[ [, { | [{

𝑞0 [, z0|[z0
𝑞1
{, { | {{

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 33
(2160704) Darshan Institute of Engineering & Technology
Design DPDA for grammar SSS|[S]|{S}|˄
Move No State Input Stack Symbol Move(s)

{, [ | {[
{, z0|{z0 [, [|[[ [, { | [{

𝑞0 [, z0|[z0
}, { | ^
𝑞1
^,z0 | z0 {, { | {{
], [ | ^
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 34
(2160704) Darshan Institute of Engineering & Technology
Design PDA to accept string with more a’s than b’s.
Move No State Input Stack Symbol Move(s)

a, b | ^ a, a | aa
b, z0| bz0 ^, a | a
a, z0|az0
𝑞0 𝑞1
b, a | ^
b, b | bb
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 36
(2160704) Darshan Institute of Engineering & Technology
CFG to PDA

Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 40 Darshan Institute of Engineering & Technology
Steps to convert CFG to PDA
• From given CFG G=(V,Σ,S,P), we can construct a PDA.
• The PDA’s δ is given by :
1. For each non terminal A, include a transition
Include a transition δ(q, 𝜖, A){(q, α)|A α is a production in G}
2. For each terminal a, include a transition
Include a transition δ(q, a, a){(q, 𝜖)}

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 41
(2160704) Darshan Institute of Engineering & Technology
Example: CFG to PDA
Find PDA for following grammar:
S0S1 | 00 | 11
The equivalent PDA, M is given by:
δ(q, 𝜖, A){(q, α)|A α is a production in G
Non Terminal
δ(q, 𝜖, S)= {(q, 0S1), (q, 00), (q,11)}

δ(q, a, a){(q, 𝜖)}


δ(q, 0, 0)={(q, 𝜖)}
Terminal
δ(q, 1, 1)={(q, 𝜖)}

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 42
(2160704) Darshan Institute of Engineering & Technology
Exercise: CFG to PDA
1. Given a CFG , G =( {S,A,B},{0,1},P,S) with P as follows:
S  0B| 1A
A  0S|1AA|0
B  1S| 0BB | 1
2. Design a PDA M corresponding to CFG:
I  a | b | Ia | Ib | I0 | I1
E  I | E * E | E + E | (E)

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 43
(2160704) Darshan Institute of Engineering & Technology
PDA to CFG

Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 44 Darshan Institute of Engineering & Technology
Rule to convert PDA to CFG
1. Add the following production for the start symbol S.
S[q0 z qi]
q0 initial state
z initial stake symbol
qi ∈ Q
2. For each transition of the form δ( qi, a, B) (qj, C)
where qi, qj ∈ Q
a ∈ (Ʃ U ^)
B, C ∈ (┌ U ^)
Then for each q∈ Q, add the production
[qi, B, q]a[qj, C, q]
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 45
(2160704) Darshan Institute of Engineering & Technology
Rule to convert PDA to CFG
3. For transition of the form δ( qi, a, B) (qj,C1C2)
where qi, qj ∈ Q
a ∈ (Ʃ U ^)
B, C1, C2 ∈ (┌ U ^)
Then for each p1, p2 ∈ Q, add the production
[qi, B, p1]a[qj C1 p2] [p2 C2 p1]

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 46
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack
q 1 z q xz
q 1 x q xx
q ^ x q ^ S[q0 z qi]
q 0 x p x
p 1 x p ^ q0 initial state
p 0 z q z
z initial stack symbol
qi ∈ Q (all state)

Step 1: Add the production for the start symbol


S[q z q]
S[q z p]

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 47
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack
For transition of the form δ( qi, a, B) (qj,C1C2)
q 1 z q xz
q 1 x q xx where qi, qj ∈ Q
q ^ x q ^ a ∈ (Ʃ U ^)
q 0 x p x B, C1, C2 ∈ (┌ U ^)
p 1 x p ^ Then for each p1, p2 ∈ Q, add the
p 0 z q z production
[qi, B, p1]a[qj C1 p2] [p2 C2 p1]
δ( qi, a, B) (qj,C1C2)
Step 2: Add production for the δ( q, 1, z) (q ,xz)
[q z q]1[q x q][q z q]
[q z q]1[q x p][p z q] [qi, B, p1]a[qj C1 p2] [p2 C2
[q z p]1[q x q][q z p] p1]P1=q p2=q
[q z p]1[q x p][p z p] P1=q p2=p
P1=p p2=q
P1=p p2=p
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 48
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack
q 1 z q xz
q 1 x q xx
q ^ x q ^
q 0 x p x
p 1 x p ^
p 0 z q z

Step 3: Add production for the δ( q, 1, x) (q ,xx)


[q x q]1[q x q][q x q]
[q x q]1[q x p][p x q]
[q x p]1[q x q][q x p]
[q x p]1[q x p][p x p]

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 49
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack
q 1 z q xz
q 1 x q xx
q ^ x q ^
q 0 x p x
p 1 x p ^
p 0 z q z

Step 4: Add the production for δ( q, ^, x) (q ,^)


[q x q]^

state stack New input


state

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 50
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack Each transition of the form δ( qi, a, B) (qj, C)
q 1 z q xz where qi, qj ∈ Q
q 1 x q xx a ∈ (Ʃ U ^)
q ^ x q ^ B, C ∈ (┌ U ^)
q 0 x p x Then for each q∈ Q, add the production
p 1 x p ^ [qi, B, q]a[qj, C, q]
p 0 z q z

Step 5: Add the production for δ( q,


qi, 0, x) (q
a, B) (pj, ,x)
C)
[q x q]0 [p x q]
[q x p]0 [p x p]
[qi, B, q]a[qj, C, q]

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 51
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack
q 1 z q xz
q 1 x q xx
q ^ x q ^
q 0 x p x
p 1 x p ^
p 0 z q z

Step 6: Add the production for δ( p, 1, x) (p ,^)


[p x p]1

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 52
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
State Input Stack New state Stack
q 1 z q xz
q 1 x q xx
q ^ x q ^
q 0 x p x
p 1 x p ^
p 0 z q z

Step 7: Add the production for δ(


δ( p,
qi, 0, z) (q
a, B) (qj,,z)
C)
[p z q]0 [q z q]
[p z p]0 [q z p]
[qi, B, q]a[qj, C, q]

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 53
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
Step 8: Renaming variable name Original name New
The set of production can be written as: name
[q z q] A
SA|B [q z p] B
A1EA|1FC [p z q] C
[p z p] D
B1EB|1FD [q x q] E
[q x p] F
E1EE|1FG [p x q] G
F1EF|1FH [p x p] H
E^
E0G
F0H Step1:
S[q z q]
H1 S[q z p]
C0A
D0B
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 54
(2160704) Darshan Institute of Engineering & Technology
Example: PDA to CFG
Step 9: simplification of grammar Original name New
name
Symbol G is not available on left side of [q z q] A
[q z p] B
grammar so, it can be eliminated. [p z q] C
SA|B [p z p] D
[q x q] E
A1EA|1FC [q x p] F
[p x q] G
B1EB|1FD [p x p] H
E1EE|^
F1EF|1FH|0H
H1
C0A
D0B
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 55
(2160704) Darshan Institute of Engineering & Technology
Intersection and complement of
CFL (Non CFL)

Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 56 Darshan Institute of Engineering & Technology
Intersection and complement of CFL
• Theorem:-There are CFLs L1 and L2 so that L1∩ L2 is not a CFL, and
there is a CFL L so that L’ is not a CFL.
• We observed that
L= { aibjck| i < j and i < k }
• is not context-free. However, although no PDA can test both
conditions i < j and i< k simultaneously, it is easy enough to build
two PDAs that test the conditions separately. In other words,
although the intersection L of the two languages
L1 = {a i bjck | i < j}
and
L2 = {aibjck | i < k}
• is not a CFL, both languages themselves are.
Unit – 4of: Pushdown
Theory Automata,
Computation CFL & NCFL 57
(2160704) Darshan Institute of Engineering & Technology
Intersection and complement of CFL
• Another way to verify that L1 is a CFL is to check it is generated by
the grammar with productions
SABC AaAb|˄ BbB|b CcC|˄
• Similarly, L2is generated by the CFG with productions
SAC AaAc|B BbB|˄ CcC|c
• The second statement in the theorem follows from the first and
the formula
L1∩ L2 =( L1’U L2’)’
• If complements of CFLs were always CFLs, then for any CFLs L1 and
L2, the language L1’and L2’ would be CFLs, so would their union,
and so would its complement. We know now that this is not the
Unit
Theory
case.
– 4of: Pushdown Automata,
Computation CFL & NCFL 58 Darshan Institute of Engineering & Technology
(2160704)
Pumping lemma for CFG

Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 59 Darshan Institute of Engineering & Technology
Pumping lemma for CFG
• Suppose L is a context-free language. Then there is an integer n so
that for every u ∈ L with |u| ≥ n, there are strings v, w, x, y, and z
satisfying,
u = vwxyz
|wy| > 0
|wxy| ≤ n
for every m ≥ 0, vwmxymz ∈ L

Unit – 4of: Pushdown


Theory Automata,
Computation CFL & NCFL 60
(2160704) Darshan Institute of Engineering & Technology
End of Unit - 4

Theory
Unit – 4of: Pushdown
Computation (2160704)
Automata, CFL & NCFL 61 Darshan Institute of Engineering & Technology

You might also like