lecture 19 - iitkhome.iitk.ac.in/~akasha/mth401/lectures/lecture19.pdfcfg pdm akash anand Ø let l =...

Post on 05-May-2018

215 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MTH401ATheory of Computation

Lecture 19

CFG and PDM

Akash Anand

Equivalence of Context Free Languages and Pushdown

Machines

Conversion of CFG to PDMConversion of PDM to CFG

CFG ➜ PDM

Akash Anand

Ø Let L = L(G)Ø Construct PDM P such that N(P) = L

CFG ➜ PDM

Akash Anand

Ø Let L = L(G)Ø Construct PDM P such that N(P) = L

Ø P has :q One state qq Input symbols = terminals of Gq Stack symbols = all symbols of Gq Start symbol = start symbol of Gq Transitions ??

CFG ➜ PDM

Akash Anand

Ø P has :q Transitions :

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then

δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

Example : Grammar, G : S → 0S0 | 1S1 | ε.

Pushdown Machine, P :States, Q = {q}Input Symbols, Σ= {0, 1}Stack Symbols, Γ= {0,1, S}Start State, q0 = qFinal State, F = {q}

CFG ➜ PDM

Akash Anand

Example : Grammar, G : S → 0S0 | 1S1 | ε.

Pushdown Machine, P :Start Symbol, Z0 = S

CFG ➜ PDM

Akash Anand

Example : Grammar, G : S → 0S0 | 1S1 | ε.

Pushdown Machine, P :Start Symbol, Z0 = STransition Function, δ :

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

Example : Grammar, G : S → 0S0 | 1S1 | ε.

Pushdown Machine, P :Start Symbol, Z0 = STransition Function, δ :δ(q, 0, 0) = {(q, ε)}δ(q, 1, 1) = {(q, ε)}δ(q, ε, S) = {(q, 0S0), (q, 1S1), (q, ε)}

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1 1 0 1

q

CFG ➜ PDM

Akash Anand

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1 1 0 1

q

δ(q, ε, S) = {(q,0S0), (q,1S1), (q, ε)}

CFG ➜ PDM

Akash Anand

1

S

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1 1 0 1

q

CFG ➜ PDM

Akash Anand

1

S

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1 1 0 1

q

δ(q, 1, 1) = {(q, ε)}

CFG ➜ PDM

Akash Anand

1

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

0 1 1 0 1

q

CFG ➜ PDM

Akash Anand

1

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

0 1 1 0 1

q

δ(q, ε, S) = {(q,0S0), (q,1S1), (q, ε)}

CFG ➜ PDM

Akash Anand

1

0

S

0

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

0 1 1 0 1

q

CFG ➜ PDM

Akash Anand

1

0

S

0

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

0 1 1 0 1

q

δ(q, 0, 0) = {(q, ε)}

CFG ➜ PDM

Akash Anand

1

0

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 1 0 1

q

CFG ➜ PDM

Akash Anand

1

0

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 1 0 1

q

δ(q, ε, S) = {(q,0S0), (q,1S1), (q, ε)}

CFG ➜ PDM

Akash Anand

1

0

1

S

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 1 0 1

q

CFG ➜ PDM

Akash Anand

1

0

1

S

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 1 0 1

q

δ(q, 1, 1) = {(q, ε)}

CFG ➜ PDM

Akash Anand

1

0

1

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1

q

CFG ➜ PDM

Akash Anand

1

0

1

S

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1

q

δ(q, ε, S) = {(q,0S0), (q,1S1), (q, ε)}

CFG ➜ PDM

Akash Anand

1

0

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1

q

CFG ➜ PDM

Akash Anand

1

0

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1 0 1

q

δ(q, 1, 1) = {(q, ε)}

CFG ➜ PDM

Akash Anand

1

0

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

0 1

q

CFG ➜ PDM

Akash Anand

1

0

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

0 1

q

δ(q, 0, 0) = {(q, ε)}

CFG ➜ PDM

Akash Anand

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1

q

CFG ➜ PDM

Akash Anand

1

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

1

q

δ(q, 1, 1) = {(q, ε)}

CFG ➜ PDM

Akash Anand

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

q

CFG ➜ PDM

Akash Anand

Example : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

q

Accept !

CFG ➜ PDM

Akash Anand

Example (continued) : 101101 : S⇒1S1⇒10S01⇒101S101⇒101101

(q, 101101, S) ⊦ (q, 101101, 1S1) ⊦(q, 01101, S1) ⊦ (q, 01101, 0S01) ⊦(q, 1101, S01) ⊦ (q, 1101, 1S101) ⊦(q, 101, S101) ⊦ (q, 101, 101) ⊦(q, 01, 01) ⊦ (q, 1, 1) ⊦ (q, ε, ε).

CFG ➜ PDM

Akash Anand

Ø Formally :

(q,wx,S) ⊦* (q,x,α) ⇔ S ⇒* wα

CFG ➜ PDM

Akash Anand

Ø Formally :

(q,wx,S) ⊦* (q,x,α) ⇔ S ⇒* wα

( ⇒ ) is an induction on the number of steps made by P.

CFG ➜ PDM

Akash Anand

Ø Formally :

(q,wx,S) ⊦* (q,x,α) ⇔ S ⇒* wα

( ⇒ ) is an induction on the number of steps made by P.

Basis : (0 steps)Then, (q,wx,S) ⊦* (q,x,α) andS ⇒* wα are true with α=S, w=ε.

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

Induction : Consider n moves of P (q,wx,S) ⊦* (q,x,α) and assume the IH for n-1 moves.

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

Induction : Consider n moves of P (q,wx,S) ⊦* (q,x,α) and assume the IH for n-1 moves.

There are two cases, depending on whether the last move uses a type 1 or type 2 rule.

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

Induction : Consider n moves of P (q,wx,S) ⊦* (q,x,α) and assume the IH for n-1 moves.

There are two cases, depending on whether the last move uses a type 1 or type 2 rule.

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

1. The move sequence must be of the form (q,yax,S) ⊦* (q,ax,aα) ⊦(q,x,α), where w = ya.

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

1. The move sequence must be of the form (q,yax,S) ⊦* (q,ax,aα) ⊦(q,x,α), where w = ya.By IH (applied to first n-1 steps), S ⇒* yaα but w = ya so S ⇒* wα.

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

2. The move sequence must be of the form (q,wx,S) ⊦* (q,x,Aβ) ⊦(q,x,γβ), where A → γ is a production and α = γβ.

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

( ⇒ ) continued …

2. The move sequence must be of the form (q,wx,S) ⊦* (q,x,Aβ) ⊦(q,x,γβ), where A → γ is a production and α = γβ.By IH (applied to first n-1 steps), S ⇒* wAβ. Thus S ⇒* wγβ = wα.

1. δ(q, a, a) = {(q, ε)}.2. If A → α is a production of G then δ(q, ε, A) contains (q, α).

CFG ➜ PDM

Akash Anand

Ø (q,wx,S) ⊦* (q,x,α) ⇔ S ⇒* wα

(⇐) is an induction on the number of steps in the leftmost derivation.

CFG ➜ PDM

Akash Anand

Ø (q,wx,S) ⊦* (q,x,α) ⇔ S ⇒* wα

(⇐) is an induction on the number of steps in the leftmost derivation.

Similar idea! Complete the proof.

CFG ➜ PDM

Akash Anand

Ø (q,wx,S) ⊦* (q,x,α) ⇔ S ⇒* wα

(⇐) is an induction on the number of steps in the leftmost derivation.

Similar idea! Complete the proof.ü If it helps, you can assume that

the grammar is in Chomsky Normal form.

PDM ➜ GFG

Akash Anand

PDM ➜ GFG

Akash Anand

Ø Let L = N(P)Ø Construct CFG G such that L(G) = L

PDM ➜ GFG

Akash Anand

Ø Let L = N(P)Ø Construct CFG G such that L(G) = L

Ø If P accepts w by (p,w,X) ⊦* (q,ε,ε) then we need to have a variable that generates w and its productions :q Let’s call this variable [pXq]q [pXq] generates all and only those

strings w such that (p,w,X) ⊦* (q,ε,ε)

PDM ➜ GFG

Akash Anand

X

Simplest Case :

p

δ(p, a, X) = {…, (q, ε), …}

a

PDM ➜ GFG

Akash Anand

Simplest Case :

q

PDM ➜ GFG

Akash Anand

Simplest Case :

Then the production is : [pXq] → a

q

PDM ➜ GFG

Akash Anand

Next Simplest Case :

δ(p, a, X) = {…, (r, Y), …}

αn

:

α2

α1

X

p

a w1 w2… wn

PDM ➜ GFG

Akash Anand

αn

:

α2

α1

Y

Next Simplest Case :

r

w1 w2 … wn

PDM ➜ GFG

Akash Anand

αn

:

α2

α1

Y

Next Simplest Case :

Then the production is : [pXq] → a [rYq]

r

w1 w2 … wn

top related