93
1 PDAs Accept Context-Free Languages

1 PDAs Accept Context-Free Languages. 2 Context-Free Languages (Grammars) Languages Accepted by PDAs Theorem:

Embed Size (px)

Citation preview

1

PDAs Accept Context-Free Languages

2

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

Theorem:

3

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

Proof - Step 1:

Convert any context-free grammar to a PDA with:

GM )()( MLGL

4

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

Proof - Step 2:

Convert any PDA to a context-free grammar with: G

M)()( MLGL

5

Converting

Context-Free Grammarsto

PDAs

Proof - step 1

6

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

Convert any context-free grammar to a PDA with:

GM )()( MLGL

7

to a PDA such that:M

We will convert grammar G

M simulates leftmost derivations of G

8

q0 q1 2qS, , $ $

wA, aa,

Production in

wATerminal in

aG G

Convert grammar to PDA G M

9

nkk

mk XX

S

11

11

Grammarleftmost derivation

,$),(

$),,(

$),,(

,$),(

2

111

111

110

q

XXq

Sq

q

mnk

nkk

nkk

PDA computationSimulates grammar leftmost derivations

Leftmostvariable

10

q0 q1 2qS, , $ $

Grammar

PDA

T

TaT

bS

aSTbS

,

,

,

,

T

TaT

bS

aSTbS

bb

aa

,

,

Example

11

abab

abTab

abTb

aSTb

S

,$),(

,$),(

$),,(

$),,(

$),,(

$),,(

$),,(

$),,(

$),,(

,$),(

2

1

1

1

1

1

1

1

1

0

q

q

bbq

ababq

Tababq

Tbabq

bTbbabq

STbbabq

Sababq

ababq

Grammarderivation

PDA computation

120q q1 2q

S, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab

Time 0

b

Derivation:

13

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

S

Time 0

Derivation: S

14

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

a

b

ST

q1

Time 1

Derivation: aSTbS

15

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

a

b

ST

q1

Time 2

aSTbS Derivation:

16

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

bTb

q1

Time 3

Derivation: abTbaSTbS

17

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

bTb

q1

Time 4

Derivation: abTbaSTbS

18

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

b

Ta

q1

Time 5

Derivation: abTababTbaSTbS

19

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

b

Ta

q1

Time 6

Derivation: abababTababTbaSTbS

20

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

ba

q1

Time 7

Derivation: abababTababTbaSTbS

21

q0 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

b

q1

Time 8

Derivation: abababTababTbaSTbS

22

q0 q1 2qS, , $ $

T

TaT

bS

aSTbS

,

,

,

,

bb

aa

,

,

Input

Stack

$

a ab b

accept

Time 9

Derivation: abababTababTbaSTbS

23

Grammar generates string

G

wS*

PDA accepts

M

,$),(,$),( 20 qwq

In general, it can be shown that:

w wIf andOnly if

Therefore )()( MLGL

24

Therefore:

For any context-free language there is a PDA that accepts

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

LL

25

Converting

PDAsto

Context-Free Grammars

Proof - step 2

26

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

Convert any PDA to a context-free grammar with: G

M)()( MLGL

27

We can convert PDA toM

a context-free grammar such that:G

Msimulates computations ofwith leftmost derivations

G

28

PDA

fq

x,

,

Empty stack

}{#x

Modify the PDA so that at end it empties stack and has a unique accept state

,

,

Old accept states

#,

New accept state

29

Deterministic PDAs - DPDAs

30

Deterministic PDA: DPDA

q1 q2wba ,

1q 2qwb,

Allowed transitions:

(deterministic choices)

31

Allowed transitions:

q1

q21, wb

q32, wc

q1

q21, wba

q32, wca

(deterministic choices)

32

q1

q21, wba

q32, wba

Not allowed:

(non deterministic choices)

q1

q21, wb

q32, wba

33

DPDA example

a, a

b, a q0 q1 q2 q3

b, a

, $ $

}0:{)( nbaML nn

a, a

34

}0:{)( nbaML nnThe language

is deterministic context-free

Definition:

A language is deterministic context-freeif there exists some DPDA that accepts it

L

Example:

35

Example of Non-DPDA (PDA)

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

}},{:{)( *bavvvML R

36

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

Not allowed in DPDAs

37

PDAs

Have More Power than

DPDAs

38

Deterministic

Context-FreeLanguages

(DPDA)

Context-Free

LanguagesPDAs

Since every DPDA is also a PDA

It holds that:

39

We will actually show:

We will show that there existsa context-free language which is notaccepted by any DPDA

L

LL

Deterministic

Context-FreeLanguages

(DPDA)

Context-Free

Languages(PDA)

40

The language is:

}{}{ 2nnnn babaL 0n

We will show:

L• is context-free

L• is not deterministic context-free

41

}{}{ 2nnnn babaL

Language is context-freeL

Context-free grammar for : L

21 | SSS

|11 baSS

|22 bbaSS

}{ nnba

}{ 2nnba

}{}{ 2nnnn baba

42

}{}{ 2nnnn babaL

is not deterministic context-free

Theorem:

The language

(there is no DPDA that accepts ) L

43

Proof: Assume for contradiction that

}{}{ 2nnnn babaL

is deterministic context free

Therefore:

there is a DPDA that accepts M L

44

DPDA with M

nnba nb

acceptsnnba 2

acceptsnnba

}{}{)( 2nnnn babaML

45

DPDA with M

nnba nb

}{}{)( 2nnnn babaML

Such a path exists due to determinism

M

46

The language is not context-free

}{ nnn cba

(we will prove this at a later class usingpumping lemma for context-free languages)

Fact 1:

Regular languages**ba

Context-free languagesnnba

47

The language is not context-free

}{ nnn cbaL

}){}{( 2nnnn babaL

Fact 2:

(we can prove this using pumping lemma for context-free languages)

48

We will construct a PDA that accepts:

}{ nnn cbaL

}){}{( 2nnnn babaL

which is a contradiction!

49

Modify M

M ncnnca

}{}{)( 2nnnn babaML

}{}{)( 2nnnn cacaML

Replacewith

bc

nnba nb

MDPDA

DPDA

50

A PDA that accepts }{ nnn cbaL

nnba nb

nc

M

M

nnca

Connect the final states of with the final states of M

M

51

Since is accepted by a PDA }{ nnn cbaL

it is context-free

Contradiction!

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

52

Therefore:

}{}{ 2nnnn babaL

There is no DPDA that accepts it

End of Proof

Is not deterministic context free

53

Positive Propertiesof

Context-Free languages

54

Context-free languages are closed under: Union

1L is context free

2L is context free

21 LL

is context-free

Union

55

Example

|11 baSS

|| 222 bbSaaSS

Union

}{1nnbaL

}{2RwwL

21 | SSS }{}{ Rnn wwbaL

Language Grammar

56

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

57

Context-free languages are closed under: Concatenation

1L is context free

2L is context free

21LL

is context-free

Concatenation

58

Example

|11 baSS

|| 222 bbSaaSS

Concatenation

}{1nnbaL

}{2RwwL

21SSS }}{{ Rnn wwbaL

Language Grammar

59

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

60

Context-free languages are closed under: Star-operation

L is context free*L is context-free

Star Operation

61

|aSbS }{ nnbaL

|11 SSS *}{ nnbaL

Example

Language Grammar

Star Operation

62

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

63

Negative Propertiesof

Context-Free Languages

64

Context-free languagesare not closed under: intersection

1L is context free

2L is context free

21 LL

not necessarilycontext-free

Intersection

65

Example

}{1mnn cbaL

|

|

cCC

aAbA

ACS

Context-free:

}{2mmn cbaL

|

|

bBcB

aAA

ABS

Context-free:

}{21nnn cbaLL NOT context-free

Intersection

66

Context-free languagesare not closed under: complement

L is context free L not necessarilycontext-free

Complement

67

}{2121nnn cbaLLLL

NOT context-free

Example

}{1mnn cbaL

|

|

cCC

aAbA

ACS

Context-free:

}{2mmn cbaL

|

|

bBcB

aAA

ABS

Context-free:

Complement

68

Intersectionof

Context-free languagesand

Regular Languages

69

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

1L context free

2L regular

21 LL

context-free

70

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

71

1M 2M

1q 2qcba ,

transition

1p 2pa

transition

NPDA DFA

11, pq cba ,

transition

MNPDA

22 , pq

72

1M 2M

1q 2qcb,

transition

1p

NPDA DFA

11, pq cb,

transition

MNPDA

12, pq

73

1M 2M

0q

initial state

0p

initial state

NPDA DFA

Initial state

MNPDA

00 , pq

74

1M 2M

1q

final state

1p

final states

NPDA DFA

final states

MNPDA

11, pq

2p

21, pq

75

Example:

, , ,

1, b1, a

1,d1,c

0q 1q 2q 3q

1M

}},{,},{|,|||:{ *2

*121211 dcwbawwwwwL

NPDA

context-free

76

0p

2M

*2 },{ caL

ca,

DFA

regular

77

Automaton for: }0:{21 ncaLL nn

, , ,

1, a 1,c

00, pq 01, pq 02, pq 03, pq

NPDA M

context-free

78

M simulates in parallel and 1M 2M

M accepts string w if and only if

accepts string and w1M

accepts string w2M

)()()( 21 MLMLML

In General:

79

Therefore:

)()( 21 MLML

is NPDAM

is context-free

21 LL is context-free

80

Applications of

Regular Closure

81

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

1L context free

2L regular

21 LL

context-free

Regular Closure

82

An Application of Regular Closure

Prove that: }0,100:{ nnbaL nn

is context-free

83

}0:{ nba nn

We know:

is context-free

84

}{ 1001001 baL is regular

}{}){( 100100*1 babaL is regular

We also know:

85

}{}){( 100100*1 babaL

regular

}{ nnbacontext-free

1}{ Lba nn context-free

LnnbaLba nnnn }0,100:{}{ 1

is context-free

(regular closure)

86

Another Application of Regular Closure

Prove that: }:{ cba nnnwL

is not context-free

87

}:{ cba nnnwL

}{*}**{ nnn cbacbaL

context-free regular context-free

If is context-free

Then

Impossible!!!

Therefore, is not context free L

(regular closure)

88

Decidable Propertiesof

Context-Free Languages

89

Membership Question:

for context-free grammarfind if string

G)(GLw

Membership Algorithms: Parsers

• Exhaustive search parser

• CYK parsing algorithm

90

Empty Language Question:

for context-free grammar find if

G)(GL

Algorithm:

S

1. Remove useless variables

2. Check if start variable is useless

91

Infinite Language Question:

for context-free grammar find if is infinite

G)(GL

Algorithm:

1. Remove useless variables

2. Remove unit and productions

3. Create dependency graph for variables

4. If there is a loop in the dependency graph then the language is infinite

92

Example:

cBSC

bbbBB

aaCbA

ABS

|

|

S

A

B

C

Dependency graph Infinite language

93

cBSC

bbbBB

aaCbA

ABS

|

|

acbbSbbbacBSbBaCbBABS

ii bbbSacbb

bbbSacbbacbbSbbbS

)()(

)()( 22