Computational Models– Lecture 5 - Tel Aviv Universitybchor/CM09/Compute5.pdf · accepted only by...

186
Computational Models– Lecture 5 Equivalence of PDAs and CFLs Nondeterminism adds power to PDAs (not in book) Closure Properties of CFLs CFGs in Chomsky normal form Algorithmic Aspects of PDAs and CFLs DFAs and PDAs: Perspectives Sipser’s book, 2.2 & 2.3 Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 1

Transcript of Computational Models– Lecture 5 - Tel Aviv Universitybchor/CM09/Compute5.pdf · accepted only by...

Computational Models– Lecture 5

Equivalenceof PDAs and CFLs

Nondeterminismadds powerto PDAs(not in book)

Closure Properties of CFLs

CFGs inChomsky normal form

Algorithmic Aspects of PDAs and CFLs

DFAs and PDAs: Perspectives

Sipser’s book, 2.2 & 2.3

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 1

Reminder

Mid-term exam on Tuesday,April 7 .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 2

Reminder

Mid-term exam on Tuesday,April 7 .

Material is firstfive lectures (i.e. up to andincluding today).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 2

Reminder

Mid-term exam on Tuesday,April 7 .

Material is firstfive lectures (i.e. up to andincluding today).

Closed books (no auxiliary material).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 2

Reminder

Mid-term exam on Tuesday,April 7 .

Material is firstfive lectures (i.e. up to andincluding today).

Closed books (no auxiliary material).

10 multiple choice (“closed, American”)questions.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 2

Reminder

Mid-term exam on Tuesday,April 7 .

Material is firstfive lectures (i.e. up to andincluding today).

Closed books (no auxiliary material).

10 multiple choice (“closed, American”)questions.

Duration 1:40 hrs.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 2

PDA Languages vs. CFLs

The set of Push-Down Automata Languages,LPDA, isthe collection of all languages that are accepted bysome PDA:

LPDA = {L : ∃PDA M ∧ L(M) = L} .

Natural questions:

LCFG ⊆ LPDA ?

LPDA ⊆ LCFG ?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 3

CFL/PDA Equivalence Theorem

Theorem: A language is context free if and only ifsome pushdown automaton accepts it.

Both the “if” part and the “only if” part arenon-trivial (unlike the proof of equivalence ofregular languages and regular expression, whereone part was trivial).

We will late present a high level view of the proof(not all details).

Full details in pp. 106–114 of Sipser’s book,and/or slides from fall 2003/4.

We will assume (and use) the theorem right away.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 4

Non-Deterministm Adds PowerTheorem:There are context free languages that areaccepted only bynon-deterministic push downautomata.

Comment: Recall that all regular languages areaccepted by deterministic finite automata. For finiteautomata, non-determinism doesnotadd power.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 5

Non-Deterministm Adds Power: ProofTheorem:Let M be a PDA that accepts

L = {xnyn|n ≥ 0} ∪{

xny2n | n ≥ 0}

.

ThenM is non-deterministic.

a(prf modified fromwww.cs.may.ie/∼jpower/Courses/parsing/node38.html)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 6

Non-Deterministm Adds Power: ProofTheorem:Let M be a PDA that accepts

L = {xnyn|n ≥ 0} ∪{

xny2n | n ≥ 0}

.

ThenM is non-deterministic.Proofa: Suppose, by way ofcontradiction, thatM isdeterministic.

a(prf modified fromwww.cs.may.ie/∼jpower/Courses/parsing/node38.html)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 6

Non-Deterministm Adds Power: ProofTheorem:Let M be a PDA that accepts

L = {xnyn|n ≥ 0} ∪{

xny2n | n ≥ 0}

.

ThenM is non-deterministic.Proofa: Suppose, by way ofcontradiction, thatM isdeterministic.

Create two copies of this PDA, denoted byM1

andM2, respectively.

Two states inM1 andM2 are called“cousins”ifthey are copies of the same state in the originalPDA.

a(prf modified fromwww.cs.may.ie/∼jpower/Courses/parsing/node38.html)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 6

Non-Deterministm Adds Power (cont.)

We now modify these PDA copies to make theminto one PDA,M0, over the alphabet{x, y, z}.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 7

Non-Deterministm Adds Power (cont.)

We now modify these PDA copies to make theminto one PDA,M0, over the alphabet{x, y, z}.

The stack alphabet is not changed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 7

Non-Deterministm Adds Power (cont.)

We now modify these PDA copies to make theminto one PDA,M0, over the alphabet{x, y, z}.

The stack alphabet is not changed.

States of the newM0 are those ofM1 unionM2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 7

Non-Deterministm Adds Power (cont.)

We now modify these PDA copies to make theminto one PDA,M0, over the alphabet{x, y, z}.

The stack alphabet is not changed.

States of the newM0 are those ofM1 unionM2.

Start state of the newM0 is the start state ofM1.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 7

Non-Deterministm Adds Power (cont.)

We now modify these PDA copies to make theminto one PDA,M0, over the alphabet{x, y, z}.

The stack alphabet is not changed.

States of the newM0 are those ofM1 unionM2.

Start state of the newM0 is the start state ofM1.

The accepting states of the newM0 are theaccepting states ofM2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 7

Non-Deterministm Adds Power (cont.)

Modifications:

Eraseall x transitionsof M2 (formally, on inputletterx and any stack option,M2 moves to a new“dead end” state).

Replace every existingy transitionof M2 by anewz transition(how is this implementedformally?).

At this pointM2 got onlyz transition(so whilein M2, the lettersx andy lead immediately torejection).

Eraseall x transitionsout ofaccept statesof M1.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 8

Non-Deterministm Adds Power (cont.)

The surgery is almost done, but if we don’tconnect the two halves of its brain, the patientwill not function coherently.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 9

Non-Deterministm Adds Power (cont.)

The surgery is almost done, but if we don’tconnect the two halves of its brain, the patientwill not function coherently.

Replace every existingy transitionleading out ofaccept statesof M1 by a newz transition, andredirect itto its “cousin” in M2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 9

Non-Deterministm Adds Power (cont.)

The surgery is almost done, but if we don’tconnect the two halves of its brain, the patientwill not function coherently.

Replace every existingy transitionleading out ofaccept statesof M1 by a newz transition, andredirect itto its “cousin” in M2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 9

Non-Deterministm Adds Power (cont.)

The surgery is almost done, but if we don’tconnect the two halves of its brain, the patientwill not function coherently.

Replace every existingy transitionleading out ofaccept statesof M1 by a newz transition, andredirect itto its “cousin” in M2.

Surgery over. Patient (a deterministic PDA) stillalive. Let us now diagnose what, if anything, itcan do.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 9

Non-Deterministm Adds Power (cont.)

What language doesM0 recognizes?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 10

Non-Deterministm Adds Power (cont.)

What language doesM0 recognizes?

If M0 accepts a string, it must be of the form(x ∪ y)∗z∗.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 10

Non-Deterministm Adds Power (cont.)

What language doesM0 recognizes?

If M0 accepts a string, it must be of the form(x ∪ y)∗z∗.

Surely notall strings of that form are accepted byM0.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 10

Non-Deterministm Adds Power (cont.)

What language doesM0 recognizes?

If M0 accepts a string, it must be of the form(x ∪ y)∗z∗.

Surely notall strings of that form are accepted byM0.

For example, the(x ∪ y)∗ prefix must beacceptedby the originalM .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 10

Non-Deterministm Adds Power (cont.)

What language doesM0 recognizes?

If M0 accepts a string, it must be of the form(x ∪ y)∗z∗.

Surely notall strings of that form are accepted byM0.

For example, the(x ∪ y)∗ prefix must beacceptedby the originalM .

Otherwise there will be no switch toM2, and noacceptance byM0. (think why isL(M0) 6= ∅?Would this also be true fornon deterministicM?)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 10

Non-Deterministm Adds Power (cont.)

What language doesM0 recognizes?

If M0 accepts a string, it must be of the form(x ∪ y)∗z∗.

Surely notall strings of that form are accepted byM0.

For example, the(x ∪ y)∗ prefix must beacceptedby the originalM .

Otherwise there will be no switch toM2, and noacceptance byM0. (think why isL(M0) 6= ∅?Would this also be true fornon deterministicM?)

So the prefix of an accepted string is either of theform xnyn or xny2n.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 10

Non-Deterministm Adds Power (cont.)

What can we say about thezi part? First,i mustbe greater than0 for a transition to take place.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 11

Non-Deterministm Adds Power (cont.)

What can we say about thezi part? First,i mustbe greater than0 for a transition to take place.

By construction,M2 on zi imitates the actions ofM onyi from the same starting point.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 11

Non-Deterministm Adds Power (cont.)

What can we say about thezi part? First,i mustbe greater than0 for a transition to take place.

By construction,M2 on zi imitates the actions ofM onyi from the same starting point.

This means that ifM0 acceptsxnynzi thenM

acceptsxnyn+i.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 11

Non-Deterministm Adds Power (cont.)

What can we say about thezi part? First,i mustbe greater than0 for a transition to take place.

By construction,M2 on zi imitates the actions ofM onyi from the same starting point.

This means that ifM0 acceptsxnynzi thenM

acceptsxnyn+i.

Which is only possible ifi = n, soM0 acceptsxnynzn, n > 0.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 11

Non-Deterministm Adds Power (cont.)

What can we say about thezi part? First,i mustbe greater than0 for a transition to take place.

By construction,M2 on zi imitates the actions ofM onyi from the same starting point.

This means that ifM0 acceptsxnynzi thenM

acceptsxnyn+i.

Which is only possible ifi = n, soM0 acceptsxnynzn, n > 0.

Notice thatM0 cannot acceptsxny2nzj, becauseM does not accepts any string of the formxny2n+j, j > 0.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 11

Conclusion of ProofWe just showed that the PDAM0 accepts thelanguage{xnynzn|n ≥ 1}.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 12

Conclusion of ProofWe just showed that the PDAM0 accepts thelanguage{xnynzn|n ≥ 1}.

By the equivalence theorem, the language that aPDA accepts is a context free language. By the socalleduvxyz pumping lemma, {xnynzn|n ≥ 1}is not a CFL. Contradiction. ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 12

Conclusion of ProofWe just showed that the PDAM0 accepts thelanguage{xnynzn|n ≥ 1}.

By the equivalence theorem, the language that aPDA accepts is a context free language. By the socalleduvxyz pumping lemma, {xnynzn|n ≥ 1}is not a CFL. Contradiction. ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 12

Conclusion of ProofWe just showed that the PDAM0 accepts thelanguage{xnynzn|n ≥ 1}.

By the equivalence theorem, the language that aPDA accepts is a context free language. By the socalleduvxyz pumping lemma, {xnynzn|n ≥ 1}is not a CFL. Contradiction. ♣

This means that our initial supposition that thelanguage{xnyn} ∪

{

xny2n}

is accepted by adeterministic PDA, does not hold.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 12

Conclusion of ProofWe just showed that the PDAM0 accepts thelanguage{xnynzn|n ≥ 1}.

By the equivalence theorem, the language that aPDA accepts is a context free language. By the socalleduvxyz pumping lemma, {xnynzn|n ≥ 1}is not a CFL. Contradiction. ♣

This means that our initial supposition that thelanguage{xnyn} ∪

{

xny2n}

is accepted by adeterministic PDA, does not hold.

While thinking about the proof, where would itfail if the originalM werenon-deterministic?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 12

Chomsky Normal Form

A simplified, canonical form of context freegrammars. Elegant by itself, useful (but not crucial) inproving equivalence theorem. Can also be used toslightly simplify proof of pumping lemma.All rules are of the form

(1)A → BC

(2)A → a

(3)S → ε

whereS is the start symbol,A, B andC are anyvariable, exceptB andC not the start symbol, andAcanbe the start symbol.A “conversion recipe”, and a detailed example, will begiven later.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 13

CFL Closure Properties

We saw that Context-Free Languages are closedunderunion, concatenation,andstar?

It is time we resolve closure with respect tocomplementationandintersection.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 14

CFL Closure Properties

Are the context free languages closed underintersection?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 15

CFL Closure Properties

Are the context free languages closed underintersection?

Suggested approach: Can we intersect twocontext free languages langauges to get0n1n2n?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 15

CFL Closure Properties

Are the context free languages closed underintersection?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 16

CFL Closure Properties

Are the context free languages closed underintersection?

S1 → A1B1 S2 → A2B2

A1 → 0A11|01 A2 → 0A2|εB1 → 2B1|ε B2 → 1B22|12

L1 = 0n1n2∗ L2 = 0∗1n2n

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 16

CFL Closure Properties

Are the context free languages closed underintersection?

S1 → A1B1 S2 → A2B2

A1 → 0A11|01 A2 → 0A2|εB1 → 2B1|ε B2 → 1B22|12

L1 = 0n1n2∗ L2 = 0∗1n2n

L1 ∩ L2 = 0n1n2n

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 16

CFL Closure Properties

Are the context free languages closed underintersection?

S1 → A1B1 S2 → A2B2

A1 → 0A11|01 A2 → 0A2|εB1 → 2B1|ε B2 → 1B22|12

L1 = 0n1n2∗ L2 = 0∗1n2n

L1 ∩ L2 = 0n1n2n

L1 is a context free language,L2 is a context freelanguage, butL1 ∩ L2 is nota context freelanguages

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 16

CFL Closure Properties

Are the context free languages closed underintersection?

S1 → A1B1 S2 → A2B2

A1 → 0A11|01 A2 → 0A2|εB1 → 2B1|ε B2 → 1B22|12

L1 = 0n1n2∗ L2 = 0∗1n2n

L1 ∩ L2 = 0n1n2n

L1 is a context free language,L2 is a context freelanguage, butL1 ∩ L2 is nota context freelanguages

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 16

CFL Closure Properties

The fact that CFLs are not closed under intersectionbut are closed under union implies they arenot closed

under complementation, asL1 ∩ L2 = L1 ∪ L2.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 17

CFL Closure Properties

Can we give a simple, specific example, whereL isnot CFL butL is?

TakeL = {ww | w ∈ {0, 1}∗}.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 18

CFL Closure Properties

Can we give a simple, specific example, whereL isnot CFL butL is?

TakeL = {ww | w ∈ {0, 1}∗}.

For anyy ∈ L, eithery’s length is odd.y’s length is even,2ℓ, and there is ani ≥ 1such thatyi 6= yℓ+i.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 18

CFL Closure Properties

Can we give a simple, specific example, whereL isnot CFL butL is?

TakeL = {ww | w ∈ {0, 1}∗}.

For anyy ∈ L, eithery’s length is odd.y’s length is even,2ℓ, and there is ani ≥ 1such thatyi 6= yℓ+i.

PDA non-deterministically tries to verify one ofthe options. Employs stack for “matchinglocations”. Accepts only on a successful branch(voluntary home assignment: fill in the details!).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 18

CFL Closure Properties

Can we give a simple, specific example, whereL isnot CFL butL is?

TakeL = {ww | w ∈ {0, 1}∗}.

For anyy ∈ L, eithery’s length is odd.y’s length is even,2ℓ, and there is ani ≥ 1such thatyi 6= yℓ+i.

PDA non-deterministically tries to verify one ofthe options. Employs stack for “matchinglocations”. Accepts only on a successful branch(voluntary home assignment: fill in the details!).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 18

CFL Closure Properties

Can we give a simple, specific example, whereL isnot CFL butL is?

TakeL = {ww | w ∈ {0, 1}∗}.

For anyy ∈ L, eithery’s length is odd.y’s length is even,2ℓ, and there is ani ≥ 1such thatyi 6= yℓ+i.

PDA non-deterministically tries to verify one ofthe options. Employs stack for “matchinglocations”. Accepts only on a successful branch(voluntary home assignment: fill in the details!).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 18

CFL Closure Properties

Can we give a simple, specific example, whereL isnot CFL butL is?

TakeL = {ww | w ∈ {0, 1}∗}.

For anyy ∈ L, eithery’s length is odd.y’s length is even,2ℓ, and there is ani ≥ 1such thatyi 6= yℓ+i.

PDA non-deterministically tries to verify one ofthe options. Employs stack for “matchinglocations”. Accepts only on a successful branch(voluntary home assignment: fill in the details!).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 18

CFL Closure Properties

Are the context free languages context freelanguages closed underintersectionwith aregular language?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 19

CFL Closure Properties

Are the context free languages context freelanguages closed underintersectionwith aregular language?

That is, ifL1 is context free languages, andL2 isregular, mustL1 ∩ L2 be context free languages?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 19

CFL Closure Properties

Are the context free languages context freelanguages closed underintersectionwith aregular language?

That is, ifL1 is context free languages, andL2 isregular, mustL1 ∩ L2 be context free languages?

Run PDAL1 and DFAL2 “in parallel” (just likethe intersection of two regular languages).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 19

CFL Closure Properties

Are the context free languages context freelanguages closed underintersectionwith aregular language?

That is, ifL1 is context free languages, andL2 isregular, mustL1 ∩ L2 be context free languages?

Run PDAL1 and DFAL2 “in parallel” (just likethe intersection of two regular languages).

This is doable since DFA does not use the stack,so it does not “get in the way” of the PDA.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 19

CFL Closure Properties

Are the context free languages context freelanguages closed underintersectionwith aregular language?

That is, ifL1 is context free languages, andL2 isregular, mustL1 ∩ L2 be context free languages?

Run PDAL1 and DFAL2 “in parallel” (just likethe intersection of two regular languages).

This is doable since DFA does not use the stack,so it does not “get in the way” of the PDA.

Formal details omitted (but you should be able tofigure them out).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 19

CFL Closure Properties: Example

Is L = {(0 ∪ 1 ∪ 2)∗ : # of 0’s = # of 1’s = # of 2’s }context free?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 20

CFL Closure Properties

L , {(0 ∪ 1 ∪ 2)∗ : # 0’s = # 1’s = # 2’s }

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 21

CFL Closure Properties

L , {(0 ∪ 1 ∪ 2)∗ : # 0’s = # 1’s = # 2’s }Is L context free?

L ∩ 00∗11∗22∗ ={0n1n2n : n > 0} which isnot context free.Context free languages intersected with aregularlanguagesarecontext free.00∗11∗22∗ is regular.SoL is nota context free language!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 21

CFL Closure Properties

L , {(0 ∪ 1 ∪ 2)∗ : # 0’s = # 1’s = # 2’s }Is L context free?

L ∩ 00∗11∗22∗ ={0n1n2n : n > 0} which isnot context free.Context free languages intersected with aregularlanguagesarecontext free.00∗11∗22∗ is regular.SoL is nota context free language!

Could this be proven directly, using pumping?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 21

CFL Closure Properties

L , {(0 ∪ 1 ∪ 2)∗ : # 0’s = # 1’s = # 2’s }Is L context free?

L ∩ 00∗11∗22∗ ={0n1n2n : n > 0} which isnot context free.Context free languages intersected with aregularlanguagesarecontext free.00∗11∗22∗ is regular.SoL is nota context free language!

Could this be proven directly, using pumping?

Probably, but we prefer to work less (providedthe proofs are correct :-).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 21

Algorithmic Questions RegardingDFAs

Given a regular expression,R, find thesmallestDFA(minimum number of states) that acceptsL(R).

Initial Idea:Use the algorithm describe in class totransformR into anNFA. Then transform thisNFA into aDFA, M .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 22

Algorithmic Questions RegardingDFAs

Given a regular expression,R, find thesmallestDFA(minimum number of states) that acceptsL(R).

Initial Idea:Use the algorithm describe in class totransformR into anNFA. Then transform thisNFA into aDFA, M .

That’s very nice, but how do we knowM isminimal?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 22

Algorithmic Questions RegardingDFAs

Given a regular expression,R, find thesmallestDFA(minimum number of states) that acceptsL(R).

Initial Idea:Use the algorithm describe in class totransformR into anNFA. Then transform thisNFA into aDFA, M .

That’s very nice, but how do we knowM isminimal?

It need not be!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 22

Algorithmic Questions forDFAs (2)

Given a regular expression,R, find thesmallestDFAthat acceptsL(R) (minimum number of states).

We can enumerate all DFAs that arestrictlysmallerthanM .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 23

Algorithmic Questions forDFAs (2)

Given a regular expression,R, find thesmallestDFAthat acceptsL(R) (minimum number of states).

We can enumerate all DFAs that arestrictlysmallerthanM .

For each suchMi, test ifL(Mi) = L(M) (we sawan algorithm for this).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 23

Algorithmic Questions forDFAs (2)

Given a regular expression,R, find thesmallestDFAthat acceptsL(R) (minimum number of states).

We can enumerate all DFAs that arestrictlysmallerthanM .

For each suchMi, test ifL(Mi) = L(M) (we sawan algorithm for this).

Take thesmallestsuchMi.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 23

Algorithmic Questions forDFAs (2)

Given a regular expression,R, find thesmallestDFAthat acceptsL(R) (minimum number of states).

We can enumerate all DFAs that arestrictlysmallerthanM .

For each suchMi, test ifL(Mi) = L(M) (we sawan algorithm for this).

Take thesmallestsuchMi.

Algorithm is veryinefficient. If smallestM hasnstates, algorithm will take time that is exponentialin n.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 23

Algorithmic Questions forDFAs (2)

Given a regular expression,R, find thesmallestDFAthat acceptsL(R) (minimum number of states).

We can enumerate all DFAs that arestrictlysmallerthanM .

For each suchMi, test ifL(Mi) = L(M) (we sawan algorithm for this).

Take thesmallestsuchMi.

Algorithm is veryinefficient. If smallestM hasnstates, algorithm will take time that is exponentialin n.

More efficient algorithm is known, using theMyhill-Nerode theorem.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 23

Algorithmic Questions RegardingCFGs

Given a CFG,G, and a stringw, doesG generatew?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 24

Algorithmic Questions RegardingCFGs

Given a CFG,G, and a stringw, doesG generatew?

Initial Idea: Design an algorithm that triesallderivations.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 24

Algorithmic Questions RegardingCFGs

Given a CFG,G, and a stringw, doesG generatew?

Initial Idea: Design an algorithm that triesallderivations.

Problem:If G doesnotgeneratew, we’ll never stop.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 24

Algorithmic Questions forCFGs (2)

Lemma: If G is in Chomsky normal form,|w| = n,andw is generated byG, thenw has a derivation oflength2n − 1 or less.

We won’t prove this (butyouwill — in caseyousolvethe second home assignment!).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 25

Algorithmic Questions forCFGs (2)

Lemma: If G is in Chomsky normal form,|w| = n,andw is generated byG, thenw has a derivation oflength2n − 1 or less.

We won’t prove this (butyouwill — in caseyousolvethe second home assignment!).

Algorithm’s idea:

First, convertG to Chomsky normal form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 25

Algorithmic Questions forCFGs (2)

Lemma: If G is in Chomsky normal form,|w| = n,andw is generated byG, thenw has a derivation oflength2n − 1 or less.

We won’t prove this (butyouwill — in caseyousolvethe second home assignment!).

Algorithm’s idea:

First, convertG to Chomsky normal form.

Now need only consider afinite numberofderivations – those of length2n − 1 or less.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 25

Algorithmic Questions forCFGs (2)

Lemma: If G is in Chomsky normal form,|w| = n,andw is generated byG, thenw has a derivation oflength2n − 1 or less.

We won’t prove this (butyouwill — in caseyousolvethe second home assignment!).

Algorithm’s idea:

First, convertG to Chomsky normal form.

Now need only consider afinite numberofderivations – those of length2n − 1 or less.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 25

Algorithmic Questions forCFGs (3)

Theorem: There is an algorithm (that halts on everyinputs)A, that on inputsG andw, decides ifGgeneratesw.On input〈G,w〉, whereG is a grammar andw astring,

1. ConvertG to Chomsky normal form.

2. List all derivations with2n − 1 steps, weren = |w|.

3. If any generatesw, accept, otherwisereject. ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 26

Algorithmic Questions forCFGs (4)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 27

Algorithmic Questions forCFGs (4)

Theorem: There is an algorithm (that halts on everyinputs)A, that on inputsG andw, decides ifGgeneratesw.

Remarks:

Related to problem of compiling prog. languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 27

Algorithmic Questions forCFGs (4)

Theorem: There is an algorithm (that halts on everyinputs)A, that on inputsG andw, decides ifGgeneratesw.

Remarks:

Related to problem of compiling prog. languages.

Wouldyouwant to usethisalgorithm at work?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 27

Algorithmic Questions forCFGs (4)

Theorem: There is an algorithm (that halts on everyinputs)A, that on inputsG andw, decides ifGgeneratesw.

Remarks:

Related to problem of compiling prog. languages.

Wouldyouwant to usethisalgorithm at work?

Recall that every theorem about CFLs is alsoabout PDAs.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 27

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

In other words, is there any stringw, such thatGgeneratew?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

In other words, is there any stringw, such thatGgeneratew?

Theorem: There is an algorithm that solves thisproblem (and always halts).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

In other words, is there any stringw, such thatGgeneratew?

Theorem: There is an algorithm that solves thisproblem (and always halts).

Possible approaches for a proof:

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

In other words, is there any stringw, such thatGgeneratew?

Theorem: There is an algorithm that solves thisproblem (and always halts).

Possible approaches for a proof:Bad Idea:We know how to test whetherw ∈ L(G) forany stringw, so just try it for eachw. (criticize this...)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

In other words, is there any stringw, such thatGgeneratew?

Theorem: There is an algorithm that solves thisproblem (and always halts).

Possible approaches for a proof:Bad Idea:We know how to test whetherw ∈ L(G) forany stringw, so just try it for eachw. (criticize this...)Better Idea:Can thestart variablegenerate a string ofterminals?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

Emptiness ofCFGs

Given a CFG,G, is L(G) = ∅?

In other words, is there any stringw, such thatGgeneratew?

Theorem: There is an algorithm that solves thisproblem (and always halts).

Possible approaches for a proof:Bad Idea:We know how to test whetherw ∈ L(G) forany stringw, so just try it for eachw. (criticize this...)Better Idea:Can thestart variablegenerate a string ofterminals?Even Better Idea:Can a particular variable generate astring ofterminals?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 28

CFGEmptiness(2)

Algorithm: On inputG (a CFG),

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 29

CFGEmptiness(2)

Algorithm: On inputG (a CFG),

1. Mark all terminal symbols inG.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 29

CFGEmptiness(2)

Algorithm: On inputG (a CFG),

1. Mark all terminal symbols inG.

2. Repeat until no new variables become marked.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 29

CFGEmptiness(2)

Algorithm: On inputG (a CFG),

1. Mark all terminal symbols inG.

2. Repeat until no new variables become marked.

3. Mark anyA whereA → U1U2 . . . Uk and allUi

have already been marked.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 29

CFGEmptiness(2)

Algorithm: On inputG (a CFG),

1. Mark all terminal symbols inG.

2. Repeat until no new variables become marked.

3. Mark anyA whereA → U1U2 . . . Uk and allUi

have already been marked.

4. If start symbol marked,accept, elsereject. ♣

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 29

CFGs “Fullness”

Given a CFG,G, is L(G) = Σ∗?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 30

CFGs “Fullness”

Given a CFG,G, is L(G) = Σ∗?

We just saw an algorithm to determine, given a CFG,G, if L(G) = ∅

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 30

CFGs “Fullness”

Given a CFG,G, is L(G) = Σ∗?

We just saw an algorithm to determine, given a CFG,G, if L(G) = ∅L(G) = Σ∗ iff L(G) = ∅. Why not modify thealgorithm so it determines emptiness of thecomplement?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 30

CFGs “Fullness”

Given a CFG,G, is L(G) = Σ∗?

We just saw an algorithm to determine, given a CFG,G, if L(G) = ∅L(G) = Σ∗ iff L(G) = ∅. Why not modify thealgorithm so it determines emptiness of thecomplement?

Unfortunately, CFGs are not closed undercomplement.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 30

CFGs “Fullness”

Given a CFG,G, is L(G) = Σ∗?

We just saw an algorithm to determine, given a CFG,G, if L(G) = ∅L(G) = Σ∗ iff L(G) = ∅. Why not modify thealgorithm so it determines emptiness of thecomplement?

Unfortunately, CFGs are not closed undercomplement.

Fact:There isnoalgorithm to solve this problem.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 30

CFGs “Fullness”

Given a CFG,G, is L(G) = Σ∗?

We just saw an algorithm to determine, given a CFG,G, if L(G) = ∅L(G) = Σ∗ iff L(G) = ∅. Why not modify thealgorithm so it determines emptiness of thecomplement?

Unfortunately, CFGs are not closed undercomplement.

Fact:There isnoalgorithm to solve this problem.

We are not prepared to prove this remarkable fact(yet).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 30

When Are Two CFGsequivalent?

Given two CFGs,G,H, is L(G) = L(H)?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 31

When Are Two CFGsequivalent?

Given two CFGs,G,H, is L(G) = L(H)?

Hey, we did this already forequivalence of DFAs!

We constructedC from A andB:

L(C) =(

L(A) ∩ L(B))

∪(

L(A) ∩ L(B))

.

and tested whetherL(C) is empty.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 31

When Are Two CFGsequivalent?

Given two CFGs,G,H, is L(G) = L(H)?

Hey, we did this already forequivalence of DFAs!

We constructedC from A andB:

L(C) =(

L(A) ∩ L(B))

∪(

L(A) ∩ L(B))

.

and tested whetherL(C) is empty.

Stop! Danger! Abyss ahead!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 31

When Are Two CFGs equivalent?

This approach was fine for DFAs, butnot for CFLs!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 32

When Are Two CFGs equivalent?

This approach was fine for DFAs, butnot for CFLs!

The class of context-free languages isnot closedunder complementation or intersection.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 32

When Are Two CFGs equivalent?

This approach was fine for DFAs, butnot for CFLs!

The class of context-free languages isnot closedunder complementation or intersection.

Fact:There isnoalgorithm to solve this problem.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 32

When Are Two CFGs equivalent?

This approach was fine for DFAs, butnot for CFLs!

The class of context-free languages isnot closedunder complementation or intersection.

Fact:There isnoalgorithm to solve this problem.

We are not prepared to prove this remarkable fact(yet).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 32

Chomsky Normal Form

A simplified, canonical form of context freegrammars. Elegant by itself, useful (but not crucial) inproving equivalence theorem. Can also be used toslightly simplify proof of pumping lemma.Every rule has the form

A → BC

A → a

S → ε

whereS is the start symbol,A, B andC are anyvariable, exceptB andC not the start symbol, andAcanbe the start symbol.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 33

Theorem:Any context-free language is generated by acontext-free grammar in Chomsky normal form.Basic idea:

Add new start symbolS0.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 34

Theorem:Any context-free language is generated by acontext-free grammar in Chomsky normal form.Basic idea:

Add new start symbolS0.

Eliminate allε rules of the formA → ε.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 34

Theorem:Any context-free language is generated by acontext-free grammar in Chomsky normal form.Basic idea:

Add new start symbolS0.

Eliminate allε rules of the formA → ε.

Eliminate all “unit” rules of the formA → B.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 34

Theorem:Any context-free language is generated by acontext-free grammar in Chomsky normal form.Basic idea:

Add new start symbolS0.

Eliminate allε rules of the formA → ε.

Eliminate all “unit” rules of the formA → B.

At each step, “patch up” rules so that grammargenerates the same language.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 34

Theorem:Any context-free language is generated by acontext-free grammar in Chomsky normal form.Basic idea:

Add new start symbolS0.

Eliminate allε rules of the formA → ε.

Eliminate all “unit” rules of the formA → B.

At each step, “patch up” rules so that grammargenerates the same language.

Convert remaining “long rules” to proper form.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 34

ProofAdd new start symbolS0 and ruleS0 → S.Guarantees that new start symbol does not appear onright-hand-side of a rule.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 35

ProofEliminatingε rules.

Repeat:

remove someA → ε.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 36

ProofEliminatingε rules.

Repeat:

remove someA → ε.

for eachR → uAv, add ruleR → uv.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 36

ProofEliminatingε rules.

Repeat:

remove someA → ε.

for eachR → uAv, add ruleR → uv.

and so on: forR → uAvAw addR → uvAw,R → uAvw, andR → uvw.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 36

ProofEliminatingε rules.

Repeat:

remove someA → ε.

for eachR → uAv, add ruleR → uv.

and so on: forR → uAvAw addR → uvAw,R → uAvw, andR → uvw.

for R → A addR → ε, except ifR → ε hasalready been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 36

ProofEliminatingε rules.

Repeat:

remove someA → ε.

for eachR → uAv, add ruleR → uv.

and so on: forR → uAvAw addR → uvAw,R → uAvw, andR → uvw.

for R → A addR → ε, except ifR → ε hasalready been removed.

until all ε-rules not involving the original startvariable have been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 36

ProofEliminateunit rules.

Repeat:

remove someA → B.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 37

ProofEliminateunit rules.

Repeat:

remove someA → B.

for eachB → u, add ruleA → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 37

ProofEliminateunit rules.

Repeat:

remove someA → B.

for eachB → u, add ruleA → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)

until all unit rules have been removed.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 37

ProofFinally, convert long rules.To replace eachA → u1u2 . . . uk (for k ≥ 3),introduce new non-terminals

N1, N2, . . . , Nk−1

and rules

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 38

ProofFinally, convert long rules.To replace eachA → u1u2 . . . uk (for k ≥ 3),introduce new non-terminals

N1, N2, . . . , Nk−1

and rules

A → u1N1

N1 → u2N2

...Nk−3 → uk−2Nk−2

Nk−2 → uk−1uk ♠

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 38

Conversion Example

Initial Grammar:

S → ASA | aB

A → B | S

B → b | ε

(1) Add new start state:

S0 → S

S → ASA | aB

A → B | S

B → b | ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 39

Conversion Example (2)

S0 → S

S → ASA | aB

A → B | S

B → b | ε

(2) Removeε-ruleB → ε:

S0 → S

S → ASA | aB | a

A → B | S | ε

B → b | ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 40

Conversion Example (3)

S0 → S

S → ASA | aB | a

A → B | S | ε

B → b

(3) Removeε-ruleA → ε:

S0 → S

S → ASA | aB | a | AS | SA | S

A → B | S | ε

B → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 41

Conversion Example (4)

S0 → S

S → ASA | aB | a | AS | SA | S

A → B | S

B → b

(4) Remove unit ruleS → S

S0 → S

S → ASA | aB | a | AS | SA | S

A → B | S

B → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 42

Conversion Example (5)

S0 → S

S → ASA | aB | a | AS | SA

A → B | S

B → b

(5) Remove unit ruleS0 → S:

S0 → S | ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → B | S

B → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 43

Conversion Example (6)

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → B | S

B → b

(6) Remove unit ruleA → B:

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → B | S | b

B → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 44

Conversion Example (7)

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → S | b

B → b

Remove unit ruleA → S:

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → S | b | ASA | aB | a | AS | SA

B → b

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 45

Conversion Example (8)

S0 → ASA | aB | a | AS | SA

S → ASA | aB | a | AS | SA

A → b | ASA | aB | a | AS | SA

B → b

(8) Final simplification – treatlong rules:

S0 → AA1 | UB | a | SA | AS

S → AA1 | UB | a | SA | AS

A → b | AA1 | UB | a | SA | AS

A1 → SA

U → a

B → b√

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 46

A Short Summary

Regular Languages≡ Finite Automata.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

A Short Summary

Regular Languages≡ Finite Automata.

Context Free Languages≡ Push DownAutomata.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

A Short Summary

Regular Languages≡ Finite Automata.

Context Free Languages≡ Push DownAutomata.

Closure properties of regular languages and ofCFLs.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

A Short Summary

Regular Languages≡ Finite Automata.

Context Free Languages≡ Push DownAutomata.

Closure properties of regular languages and ofCFLs.

Most algorithmic problems for finite automataare solvable.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

A Short Summary

Regular Languages≡ Finite Automata.

Context Free Languages≡ Push DownAutomata.

Closure properties of regular languages and ofCFLs.

Most algorithmic problems for finite automataare solvable.

Some algorithmic problems for finite automataare not solvable.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

A Short Summary

Regular Languages≡ Finite Automata.

Context Free Languages≡ Push DownAutomata.

Closure properties of regular languages and ofCFLs.

Most algorithmic problems for finite automataare solvable.

Some algorithmic problems for finite automataare not solvable.

Pumping lemmata for both classes of languages.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

A Short Summary

Regular Languages≡ Finite Automata.

Context Free Languages≡ Push DownAutomata.

Closure properties of regular languages and ofCFLs.

Most algorithmic problems for finite automataare solvable.

Some algorithmic problems for finite automataare not solvable.

Pumping lemmata for both classes of languages.

There are additional languages out there.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 47

The View Over The Horizon

context free

regular

decidable

enumerable

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 48

The View Over The Horizon

context free

regular

decidable

enumerable

Happy passover (& kosher for legumes eaters)!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 48

Equivalence Theorem

Theorem: A language is context free if and only ifsome pushdown automata accepts it.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 49

Equivalence Theorem

Theorem: A language is context free if and only ifsome pushdown automata accepts it.

This time (unlike the regular expression vs. regularlanguages theorem), the proofs of both the “if ” partand the “only if” part are non trivial.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 49

If Part

Theorem:If a language is context free, then somepushdown automaton accepts it.

Let A be a context-free language.

By definition,A has a context-free grammarGgenerating it.

On inputw, the PDAP should figure out if thereis a derivation ofw usingG.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 50

CFL Implies PDA (cont.)

Where do we keep the intermediate string?

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 51

CFL Implies PDA (cont.)

Where do we keep the intermediate string?

can’t put it all on the stack

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 51

CFL Implies PDA (cont.)

Where do we keep the intermediate string?

can’t put it all on the stack

only strings whose first letter is a variable arekept on stack

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 51

CFL Implies PDA

Will be more convenient to use grammar inChomskynormal form, due to compact derivation rules.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 52

CFL Implies PDA

Will be more convenient to use grammar inChomskynormal form, due to compact derivation rules.

Informally, on input stringw ∈ Σ∗:

P pushes start variableS on stack

keeps making substitutions

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 52

CFL Implies PDA

Will be more convenient to use grammar inChomskynormal form, due to compact derivation rules.

Informally, on input stringw ∈ Σ∗:

P pushes start variableS on stack

keeps making substitutions

when popping a terminal,P checks equality withcurrent input string

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 52

CFL Implies PDA

Will be more convenient to use grammar inChomskynormal form, due to compact derivation rules.

Informally, on input stringw ∈ Σ∗:

P pushes start variableS on stack

keeps making substitutions

when popping a terminal,P checks equality withcurrent input string

rejects if not equal

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 52

CFL Implies PDA

Will be more convenient to use grammar inChomskynormal form, due to compact derivation rules.

Informally, on input stringw ∈ Σ∗:

P pushes start variableS on stack

keeps making substitutions

when popping a terminal,P checks equality withcurrent input string

rejects if not equal

when popping a variable,P pushes to top of stacka right hand side ofsomerule corresponding tovariable (zero, one, or two symbols).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 52

CFL Implies PDA

Will be more convenient to use grammar inChomskynormal form, due to compact derivation rules.

Informally, on input stringw ∈ Σ∗:

P pushes start variableS on stack

keeps making substitutions

when popping a terminal,P checks equality withcurrent input string

rejects if not equal

when popping a variable,P pushes to top of stacka right hand side ofsomerule corresponding tovariable (zero, one, or two symbols).

if EOI reached when stack is empty, accept.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 52

CFL Implies PDA (cont.)

Informal description:

pushS$ on stack

if top of stack is variableA, non-deterministicallyselect rule and substitute.

if top of stack is terminala, read next input andcompare. If they differ, reject.

if top of stack and input symbol are both$, enteraccept state. (Namely accepts only if input has allbeen read and stack is empty!).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 53

CFL Implies PDA (cont.)

Need shorthand to push strings of length 2 onto stack.For example, suppose

A → BC

is a derivation of the CFG.Then we add a “shorthand state”,qe, and the twotransitions

(qe, C) ∈ δ(qℓ, A, ε), δ(qe, ε, ε) = {(qℓ, B)}Notice that the second transition is deterministic (thefirst one may or may not be). Also notice order: PushC first, thenB.These intermediate states are different for differentderivations.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 54

CFL Implies PDA (cont.)

States ofP are

start stateqs

accept stateqa

loop stateqℓ

qe states, needed for shorthand of right hand sidesof rules

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 55

Transition FunctionInitialize stack

δ(qs, ε, ε) = {qℓ, S$}Top of stack is variable (shorthand for twotransitions)

δ(qℓ, ε, A) = {(qℓ, w)| whereA → w is a rule}Top of stack is terminal

δ(qℓ, a, a) = {(qℓ, ε)}End of Stack and End of Input

δ(qℓ, $, $) = {(qa, ε)}Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 56

Example

S → AT |εA → AB|AA|aB → b

T → TT |tTransition rules forPDA: On black/white board.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 57

Only If Part

Theorem:If a PDA accepts a language,L, thenL iscontext-free.

For each pair of statesp andq in P , we will havea variableApq in the grammarG.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 58

Only If Part

Theorem:If a PDA accepts a language,L, thenL iscontext-free.

For each pair of statesp andq in P , we will havea variableApq in the grammarG.

This variable,Apq, generates all strings that takeP from p with an empty stack toq with emptystack.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 58

Only If Part

Theorem:If a PDA accepts a language,L, thenL iscontext-free.

For each pair of statesp andq in P , we will havea variableApq in the grammarG.

This variable,Apq, generates all strings that takeP from p with an empty stack toq with emptystack.

Same string also takesp with any stackto q withsame stack!

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 58

Only If Part

Theorem:If a PDA accepts a language,L, thenL iscontext-free.

For each pair of statesp andq in P , we will havea variableApq in the grammarG.

This variable,Apq, generates all strings that takeP from p with an empty stack toq with emptystack.

Same string also takesp with any stackto q withsame stack!

Start variable isAq0,qa(assuming asingleaccept

stateqa).

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 58

PDA Implies CFL

To make things easier, we slightly modifyP

Hassingleaccept stateqa.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 59

PDA Implies CFL

To make things easier, we slightly modifyP

Hassingleaccept stateqa.

It empties stackbefore accepting.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 59

PDA Implies CFL

To make things easier, we slightly modifyP

Hassingleaccept stateqa.

It empties stackbefore accepting.

Each transition either pushes a symbol on stack,or pops a symbol from stack, butnot both.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 59

PDA Implies CFL (2)

Modify P to make things easier

single accept stateqa

ε,ε ε

ε,ε ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 60

PDA Implies CFL (2)

Modify P to make things easier

single accept stateqa

ε,ε ε

ε,ε ε

empties stack before accepting

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 60

PDA Implies CFL (2)

Modify P to make things easier

single accept stateqa

ε,ε ε

ε,ε ε

empties stack before accepting

each transition pushes or pops, but not both.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 60

PDA Implies CFL (3)Modify P to make things easier

single accept stateqa

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 61

PDA Implies CFL (3)Modify P to make things easier

single accept stateqa

empties stack before accepting

ε,ε ε

ε,ε ε

ε,ε ε

ε,ε ε

εε,a

ε,ε ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 61

PDA Implies CFL (4)Modify P to make things easier

single accept stateqa

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 62

PDA Implies CFL (4)Modify P to make things easier

single accept stateqa

empties stack before accepting√

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 62

PDA Implies CFL (4)Modify P to make things easier

single accept stateqa

empties stack before accepting√

transition either pushes or pops, but not botha,b c

a,b ε cε,ε

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 62

Proof IdeaSuppose stringx takesP from p with empty stack toqwith empty stack.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 63

Proof IdeaSuppose stringx takesP from p with empty stack toqwith empty stack.

First move that touches the stack must be apush, lastmust be apop.In between, two possibilities:

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 63

Proof IdeaSuppose stringx takesP from p with empty stack toqwith empty stack.

First move that touches the stack must be apush, lastmust be apop.In between, two possibilities:

Stack is emptyonly at start and finish, but not inmiddle.

Stack was also empty at some pointin between.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 63

Proof Idea (2)

Suppose stringx takesP from p with empty stack toqwith empty stack.First move that touches the stack must be apush, lastmust be apop.In between, two possibilities:

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 64

Proof Idea (2)

Suppose stringx takesP from p with empty stack toqwith empty stack.First move that touches the stack must be apush, lastmust be apop.In between, two possibilities:

Stack is emptyonly at start and finish, but not inmiddle.Simulate by:Apq → aArsb, wherea, b are firstand last symbols inx (shorterx will be taken careof too),r follows p, ands precedesq.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 64

Proof Idea (2)

Suppose stringx takesP from p with empty stack toqwith empty stack.First move that touches the stack must be apush, lastmust be apop.In between, two possibilities:

Stack is emptyonly at start and finish, but not inmiddle.Simulate by:Apq → aArsb, wherea, b are firstand last symbols inx (shorterx will be taken careof too),r follows p, ands precedesq.

Stack was also empty at some pointin between.Simulate by:Apq → AprArq, r is intermediatestate whereP has empty stack.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 64

Details of Simulating Grammar

Given PDAP = (Q,Σ,Γ, δ, q0, {qa}), constructgrammarG. Variables are{Apq | p, q ∈ Q}.

Start variable isAq0qa.

Rules:

Forp, q, r, s ∈ Q, t ∈ Γ, anda, b ∈ Σ, if(r, t) ∈ δ(p, a, ε) and(q, ε) ∈ δ(s, b, t),add ruleApq → aArsb .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 65

Details of Simulating Grammar

Given PDAP = (Q,Σ,Γ, δ, q0, {qa}), constructgrammarG. Variables are{Apq | p, q ∈ Q}.

Start variable isAq0qa.

Rules:

Forp, q, r, s ∈ Q, t ∈ Γ, anda, b ∈ Σ, if(r, t) ∈ δ(p, a, ε) and(q, ε) ∈ δ(s, b, t),add ruleApq → aArsb .

for everyp, q, r ∈ Q, add ruleApq → AprArq .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 65

Details of Simulating Grammar

Given PDAP = (Q,Σ,Γ, δ, q0, {qa}), constructgrammarG. Variables are{Apq | p, q ∈ Q}.

Start variable isAq0qa.

Rules:

Forp, q, r, s ∈ Q, t ∈ Γ, anda, b ∈ Σ, if(r, t) ∈ δ(p, a, ε) and(q, ε) ∈ δ(s, b, t),add ruleApq → aArsb .

for everyp, q, r ∈ Q, add ruleApq → AprArq .

for eachp ∈ Q, add ruleApp → ε .

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 65

Overall StructureShould now prove

Claim: Apq generatesx if and only if x bringsPfrom p with empty stack toq with empty stack.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 66

Only If Part

Theorem:If a PDA accepts a language,L, thenL iscontext-free.

Proof:After constructing the grammarG , shouldprove it generates exactly the same language acceptedby the PDA.This is done by induction on the length of anycomputation ofP on any input stringx.The induction argument is a bit lengthy and tedious,and we’ll skip it.

Diehards are welcome to consult pp. 106–114 inSipser’s book, and/or slides from fall 2003/4.

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p. 67