49
1 A Non Context-Free Language (We will prove it at the next class)

1 A Non Context-Free Language (We will prove it at the next class)

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

1

A Non Context-Free Language

(We will prove it at the next class)

2

Regular languages

**ba

Context-free languagesnnba

Non Context-free languagesnnn cba

3

Deterministic PDAs

DPDAs

4

DPDAs

Allowed: q1 q2wba ,

q1

q21, wba

q32, wca

5

q1

q21, wba

q32, wba

Not allowed:

6

Allowed:1q 2q

wb,

q1

q21, wb

q32, wc

Something must be matched from the stack

7

Not allowed:

q1

q21, wb

q32, wba

8

DPDA example

a, a

b, a q0 q1 q2 q3

b, a

, $ $

}0:{)( nbaML nn

a, a

9

}0:{)( nbaML nnThe language

is deterministic context-free

10

Definition:

A language is deterministic context-freeif some DPDA accepts it

11

Example of Non-DPDA (NPDA)

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

}{)( RwwML

12

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

Not allowed in DPDAs

13

NPDAs

Have More Power than

DPDAs

14

We will show:

there is a context-free language

which is not deterministic context-free

(accepted by a NPDA)

(not accepted by a DPDA)

L

15

The language is:

}{}{ 2nnnn babaL

0n

16

}{}{ 2nnnn babaL

The language is context-freeL

Context-free grammar for : L

21 | SSS

|11 baSS

|22 bbaSS

there is an NPDA that accepts L

17

}{}{ 2nnnn babaL

is not deterministic context-free

Theorem:

The language

(there is no DPDA that accepts ) L

18

Proof: Assume for contradiction that

}{}{ 2nnnn babaL

is deterministic context free

Therefore:

there is a DPDA that accepts M L

19

DPDA with M

nnba nb

acceptsnnba 2

acceptsnnba

}{}{)( 2nnnn babaML

20

The language is not context-free

}{ nnn cba

(we will prove it at the next class)

Fact 1:

21

The language is not context-free

}{ nnn cbaL

}){}{( 2nnnn babaL

(we will prove later today that the union oftwo context-free languages is context-free)

Fact 2:

22

We will construct a NPDA that accepts:

}{ nnn cbaL

}){}{( 2nnnn babaL

Contradiction!

23

We modify M

nc

Modified M

Replace with cb

nnca

}){}{( 2nnnn babaL

}){}{( 2nnnn cacaL

24

The NPDA that accepts }{ nnn cbaL

nnba nb

nc

Modified M

Original M

nnca

25

Since is accepted by a NPDA }{ nnn cbaL

it is context-free

Contradiction!

(since is not context-free)}{ nnn cbaL

26

Therefore:

}{}{ 2nnnn babaL

There is no DPDA that accepts

End of Proof

Not deterministic context free

27

Positive Propertiesof

Context-Free languages

28

Context-free languages are closed under: Union

1L is context free

2L is context free

21 LL

is context-free

Union

29

Example

|11 baSS

|| 222 bbSaaSS

Union

}{1nnbaL

}{2RwwL

21 | SSS }{}{ Rnn wwbaL

Language Grammar

30

In general:

The grammar of the union has new start variableand additional production 21 | SSS

For context-free languageswith context-free grammarsand start variables

21, LL

21, GG

21, SS

21 LL S

31

Context-free languages are closed under: Concatenation

1L is context free

2L is context free

21LL

is context-free

Concatenation

32

Example

|11 baSS

|| 222 bbSaaSS

Concatenation

}{1nnbaL

}{2RwwL

21SSS }}{{ Rnn wwbaL

Language Grammar

33

In general:

The grammar of the concatenation has new start variableand additional production 21SSS

For context-free languageswith context-free grammarsand start variables

21, LL

21, GG

21, SS

21LLS

34

Context-free languages are closed under: Star-operation

L is context free*L is context-free

Star Operation

35

|aSbS }{ nnbaL

|11 SSS *}{ nnbaL

Example

Language Grammar

Star Operation

36

In general:

The grammar of the star operation has new start variableand additional production

For context-free languagewith context-free grammarand start variable

LGS

*L1S

|11 SSS

37

Negative Propertiesof

Context-Free Languages

38

Context-free languagesare not closed under: intersection

1L is context free

2L is context free

21 LL

not necessarilycontext-free

Intersection

39

Example

}{1mnn cbaL

|

|

cCC

aAbA

ACS

Context-free:

}{2mmn cbaL

|

|

bBcB

aAA

ABS

Context-free:

}{21nnn cbaLL NOT context-free

Intersection

40

Context-free languagesare not closed under: complement

L is context free L not necessarilycontext-free

Complement

41

}{2121nnn cbaLLLL

NOT context-free

Example

}{1mnn cbaL

|

|

cCC

aAbA

ACS

Context-free:

}{2mmn cbaL

|

|

bBcB

aAA

ABS

Context-free:

Complement

42

Intersectionof

Context-free languagesand

Regular Languages

43

The intersection of a context-free language and a regular languageis a context-free language

1L context free

2L regular

21 LL

context-free

44

1Lfor for 2LNPDA

1M

DFA

2M

Construct a new NPDA machinethat accepts

Machine Machine

M21 LL

context-free regular

M simulates in parallel and 1M 2M

45

1M 2M

1q 2qcba ,

transition

1p 2pa

transition

NPDA DFA

11, pq cba ,

transition

MNPDA

22 , pq

46

1M 2M

0q

initial state

0p

initial state

NPDA DFA

Initial state

MNPDA

00 , pq

47

1M 2M

1q

final state

1p

final states

NPDA DFA

final states

MNPDA

11, pq

2p

21, pq

48

M simulates in parallel and 1M 2M

M accepts string w if and only if

accepts string and w1M

accepts string w2M

)()()( 21 MLMLML

49

Therefore: )()( 21 MLML

(since is NPDA) M

is context-free

21 LL is context-free