1 pdas accept context-free languages. 2 context-free languages (grammars) languages accepted by pdas...

Post on 22-Dec-2015

254 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 will convert PDA toM

a context-free grammar such that:G

Msimulates computations ofwith leftmost derivations

G

28

Some Necessary Modifications

If necessary, modify the PDA so that:

1. The stack is never empty during computation

2. It has a single accept state and empties the stack when it accepts a string

3. Has transitions without popping

29

1. Modify the PDA so that the stack is never empty during computation

$

Stack

a

OK

$

OK NOT OK

30

$

a$

Introduce the new symbol to mark the bottom of the stack

#

$a

#$# #

31

Original PDA

$#,$

At the beginning insert into the stack#

original initial state

new initial state

32

iq jqs ,

iq jqs ,

## ,

halting state

Convert all transitions so that after popping the automaton halts $

xx,}{#x

pop $

pop$

33

PDA

fq

x,

,

Empty stack

}{#x

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

,

,

Old accept states

#,

New accept state

34

3. Modify the PDA so that it has no transitions popping :

iq jqy ,

iq jq y,

}{#

35

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)}()(:},{{)( * wnwnbawML ba

Example of a PDA in correct form:(modifications are not necessary)

36

Grammar Construction

A

In grammar :G

Terminals:

PDA stack symbolsVariables:

a PDA input symbols

Start Variable: Stack bottom symbol#or $

37

q 1qAa,PDA transition

Grammar production aA

38

PDA transition

Grammar production mBBaBA 21

q 1qmBBBAa 21,

39

nkk

mk XX

S

11

11

Grammarleftmost derivation

),,(

),,(

,$),(

2

111

111

q

XXq

q

mnk

nkk

PDA computation

Leftmostvariable

40

Example PDA:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Grammar: $0$ a000 aa1

$1$ b111 bb0 $

41

abba

abba

abb

ab

a

$

$1

$

$0

$

GrammarLeftmostderivation:

PDAComputation:

),,(

,$),(

$)1,,(

,$),(

$)0,,(

,$),(

0

0

0

0

0

fq

q

aq

baq

bbaq

abbaq

42

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

$Derivation:

a ab b Time 0

43

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

$Derivation:

a ab b Time 1

Stack

$0

$0a

44

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

$Derivation:

a ab b Time 2

Stack

$

$0a $ab

45

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

$Derivation:

a ab b Time 3

Stack

$

$0a $ab

1

$1abb

46

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Derivation:

a ab b Time 4

Stack

$

$ $0a $ab $1abb$abba

47

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Derivation:

a ab b Time 5

Stack

empty

abba

$ $0a $ab $1abb$abba

48

However, this grammar conversion does not work for all PDAs:

Ab,

0q fq

$$, Aa $, AAa

Ab,1q

$,

}1:{)( nbaML nn

49

Ab,

0q fq

$$, Aa $, AAa

Ab,1q

$,

Grammar: $$ aA $aAAbA$

50

Grammar: $$ aA $aAAbA$

Bad Derivation:

)($$$ MLaabaabaaAaAS

51

The Correct Grammar Construction

)( jiAqq

In grammar :G

Terminals: Input symbols of PDA

PDA states

PDA stack symbol

Variables:

52

q 1qAa,PDA transition

Grammar production aqAq )( 1

53

PDA transition

Grammar production

)())(()( 1322211 mmmm qBqqBqqBqaqAq

q 1qmBBBAa 21,

For all possible states in PDA 12 ,, mqq

54

Start Variable: )( foZqq

Stack bottom symbol

Start state accept state

#or $

55

Example:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

aqq )1( 00Grammar production:

56

Example:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$)(1(|)$)(1()$(

)$)(1(|)$)(1()$(

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

Grammar productions:

57

Example:

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

Grammar production: )$( 0 fqq

58

)$)(1(|)$)(1()$(

)$)(1(|)$)(1()$(

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

)1)(1(|)1)(1()1(

)1)(1(|)1)(1()1(

00000

00000000

fffff

ff

qqqqbqqqqbqq

qqqqbqqqqbqq

)$)(0(|)$)(0()$(

)$)(0(|)$)(0()$(

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

Resulting Grammar: ablestart vari:)$( 0 fqq

59

)0)(0(|)0)(0()0(

)0)(0(|)0)(0()0(

00000

00000000

fffff

ff

qqqqaqqqqaqq

qqqqaqqqqaqq

bqq

aqq

)0(

)1(

00

00

)$( 0 fqq

60

)$( 0 fqq

)$)(0( 000 fqqqqa

)$( 0 fqqab)$)(1( 000 fqqqqabb

)$( 0 fqqabba

abba ),,(

,$),(

$)1,,(

,$),(

$)0,,(

,$),(

0

0

0

0

0

fq

q

aq

baq

bbaq

abbaq

GrammarLeftmostderivation

PDAcomputation

61

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$( 0 fqqDerivation:

a ab b Time 0

62

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$( 0 fqqDerivation:

a ab b Time 1

Stack

$0

)$)(0( 000 fqqqqa

63

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$( 0 fqqDerivation:

a ab b Time 2

Stack

$

)$)(0( 000 fqqqqa )$( 0 fqqab

64

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$( 0 fqqDerivation:

a ab b Time 3

Stack

$

)$)(0( 000 fqqqqa )$( 0 fqqab

1

)$)(1( 000 fqqqqabb

65

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$( 0 fqqDerivation:

a ab b Time 4

Stack

$

)$)(0( 000 fqqqqa )$( 0 fqqab)$)(1( 000 fqqqqabb )$( 0 fqqabba

66

$,0q fq

a, $ 0$a, 0 00a,1

b, $ 1$b, 1 11b, 0

)$( 0 fqqDerivation:

a ab b Time 5

Stack

)$)(0( 000 fqqqqa )$( 0 fqqab)$)(1( 000 fqqqqabb )$( 0 fqqabba abba

empty

67

In general:

wBAqq ji

)(If andOnly if

Grammar PDA

),,(),,( BqAwq ji

68

Thus:

wqq f

)$( 0

If andOnly if

Grammargenerates PDA accepts

),,(,$),( 0 fqwq

w w

69

Therefore:

For any PDAthere is a context-free grammarthat accepts the same language

Context-Free

Languages(Grammars)

LanguagesAccepted

byPDAs

top related