NPDAs Accept ContextFree Languages Fall 2004 COMP 335

  • Slides: 69
Download presentation
NPDA’s Accept Context-Free Languages Fall 2004 COMP 335 1

NPDA’s Accept Context-Free Languages Fall 2004 COMP 335 1

Theorem: Context-Free Languages (Grammars) Fall 2004 Languages Accepted by NPDAs COMP 335 2

Theorem: Context-Free Languages (Grammars) Fall 2004 Languages Accepted by NPDAs COMP 335 2

Proof - Step 1: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any context-free

Proof - Step 1: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any context-free grammar to a NPDA with: Fall 2004 COMP 335 3

Proof - Step 2: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any NPDA

Proof - Step 2: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any NPDA grammar with: Fall 2004 COMP 335 to a context-free 4

Proof - step 1 Converting Context-Free Grammars to NPDAs Fall 2004 COMP 335 5

Proof - step 1 Converting Context-Free Grammars to NPDAs Fall 2004 COMP 335 5

We will convert any context-free grammar to an NPDA automaton Such that: Simulates leftmost

We will convert any context-free grammar to an NPDA automaton Such that: Simulates leftmost derivations of Fall 2004 COMP 335 6

Leftmost derivation Input processed Stack contents leftmost variable Simulation of derivation Stack Input Fall

Leftmost derivation Input processed Stack contents leftmost variable Simulation of derivation Stack Input Fall 2004 COMP 335 7

Leftmost derivation string of terminals Simulation of derivation Stack Input end of input is

Leftmost derivation string of terminals Simulation of derivation Stack Input end of input is reached Fall 2004 COMP 335 8

An example grammar: What is an equivalent NPDA? Fall 2004 COMP 335 9

An example grammar: What is an equivalent NPDA? Fall 2004 COMP 335 9

Grammar: NPDA: Fall 2004 COMP 335 10

Grammar: NPDA: Fall 2004 COMP 335 10

Grammar: A leftmost derivation: Fall 2004 COMP 335 11

Grammar: A leftmost derivation: Fall 2004 COMP 335 11

Derivation: Input Time 0 Fall 2004 Stack COMP 335 12

Derivation: Input Time 0 Fall 2004 Stack COMP 335 12

Derivation: Input Time 0 Fall 2004 Stack COMP 335 13

Derivation: Input Time 0 Fall 2004 Stack COMP 335 13

Derivation: Input Time 1 Fall 2004 Stack COMP 335 14

Derivation: Input Time 1 Fall 2004 Stack COMP 335 14

Derivation: Input Time 2 Fall 2004 Stack COMP 335 15

Derivation: Input Time 2 Fall 2004 Stack COMP 335 15

Derivation: Input Time 3 Fall 2004 Stack COMP 335 16

Derivation: Input Time 3 Fall 2004 Stack COMP 335 16

Derivation: Input Time 4 Fall 2004 Stack COMP 335 17

Derivation: Input Time 4 Fall 2004 Stack COMP 335 17

Derivation: Input Time 5 Fall 2004 Stack COMP 335 18

Derivation: Input Time 5 Fall 2004 Stack COMP 335 18

Derivation: Input Time 6 Fall 2004 Stack COMP 335 19

Derivation: Input Time 6 Fall 2004 Stack COMP 335 19

Derivation: Input Time 7 Fall 2004 Stack COMP 335 20

Derivation: Input Time 7 Fall 2004 Stack COMP 335 20

Derivation: Input Time 8 Fall 2004 Stack COMP 335 21

Derivation: Input Time 8 Fall 2004 Stack COMP 335 21

Derivation: Input Time 9 Stack accept Fall 2004 COMP 335 22

Derivation: Input Time 9 Stack accept Fall 2004 COMP 335 22

In general: Given any grammar We can construct a NPDA With Fall 2004 COMP

In general: Given any grammar We can construct a NPDA With Fall 2004 COMP 335 23

Constructing NPDA from grammar : For any terminal For any production Fall 2004 COMP

Constructing NPDA from grammar : For any terminal For any production Fall 2004 COMP 335 24

Grammar generates string if and only if NPDA Fall 2004 accepts COMP 335 25

Grammar generates string if and only if NPDA Fall 2004 accepts COMP 335 25

Therefore: For any context-free language there is a NPDA that accepts the same language

Therefore: For any context-free language there is a NPDA that accepts the same language Context-Free Languages (Grammars) Fall 2004 Languages Accepted by NPDAs COMP 335 26

Proof - step 2 Converting NPDAs to Context-Free Grammars Fall 2004 COMP 335 27

Proof - step 2 Converting NPDAs to Context-Free Grammars Fall 2004 COMP 335 27

For any NPDA we will construct a context-free grammar Fall 2004 COMP 335 with

For any NPDA we will construct a context-free grammar Fall 2004 COMP 335 with 28

Intuition: The grammar simulates the computation of the machine A derivation in Grammar terminals

Intuition: The grammar simulates the computation of the machine A derivation in Grammar terminals : variables Input processed Stack contents Current configuration in NPDA Fall 2004 COMP 335 29

Some Necessary Modifications Modify (if necessary) the NPDA so that: 1) The stack is

Some Necessary Modifications Modify (if necessary) the NPDA so that: 1) The stack is never empty 2) has a single final state and empties the stack when it accepts a string 3) Has transitions in a special form: (qi, a, A)={c 1, c 2, …, cn}, where ci=(qj, ) or ci=(qj, BC) Fall 2004 COMP 335 30

Example of a NPDA in correct form: Fall 2004 COMP 335 31

Example of a NPDA in correct form: Fall 2004 COMP 335 31

The Grammar Construction In grammar : Stack symbol Variables: states Terminals: Input symbols of

The Grammar Construction In grammar : Stack symbol Variables: states Terminals: Input symbols of NPDA Fall 2004 COMP 335 32

For each transition We add production Fall 2004 COMP 335 33

For each transition We add production Fall 2004 COMP 335 33

For each transition We add productions For all possible states in the automaton Fall

For each transition We add productions For all possible states in the automaton Fall 2004 COMP 335 34

Stack bottom symbol Start Variable: Start state Fall 2004 final state COMP 335 35

Stack bottom symbol Start Variable: Start state Fall 2004 final state COMP 335 35

Example: Grammar production: Fall 2004 COMP 335 36

Example: Grammar production: Fall 2004 COMP 335 36

Example: Grammar productions: Fall 2004 COMP 335 37

Example: Grammar productions: Fall 2004 COMP 335 37

Example: Grammar production: Fall 2004 COMP 335 38

Example: Grammar production: Fall 2004 COMP 335 38

Resulting Grammar: Fall 2004 COMP 335 39

Resulting Grammar: Fall 2004 COMP 335 39

Fall 2004 COMP 335 40

Fall 2004 COMP 335 40

Derivation of string Fall 2004 COMP 335 41

Derivation of string Fall 2004 COMP 335 41

In general: if and only if the NPDA goes from to by reading string

In general: if and only if the NPDA goes from to by reading string and the stack doesn’t change below and then is removed from stack Fall 2004 COMP 335 42

Therefore: if and only if is accepted by the NPDA Fall 2004 COMP 335

Therefore: if and only if is accepted by the NPDA Fall 2004 COMP 335 43

Therefore: For any NPDA there is a context-free grammar that accepts the same language

Therefore: For any NPDA there is a context-free grammar that accepts the same language Context-Free Languages (Grammars) Fall 2004 Languages Accepted by NPDAs COMP 335 44

Deterministic PDA DPDA Fall 2004 COMP 335 45

Deterministic PDA DPDA Fall 2004 COMP 335 45

Deterministic PDA: DPDA Allowed transitions: (deterministic choices) Fall 2004 COMP 335 46

Deterministic PDA: DPDA Allowed transitions: (deterministic choices) Fall 2004 COMP 335 46

Allowed transitions: (deterministic choices) Fall 2004 COMP 335 47

Allowed transitions: (deterministic choices) Fall 2004 COMP 335 47

Not allowed: (non deterministic choices) Fall 2004 COMP 335 48

Not allowed: (non deterministic choices) Fall 2004 COMP 335 48

DPDA example Fall 2004 COMP 335 49

DPDA example Fall 2004 COMP 335 49

The language is deterministic context-free Fall 2004 COMP 335 50

The language is deterministic context-free Fall 2004 COMP 335 50

Definition: A language is deterministic context-free if there exists some DPDA that accepts it

Definition: A language is deterministic context-free if there exists some DPDA that accepts it Fall 2004 COMP 335 51

Example of Non-DPDA (NPDA) Fall 2004 COMP 335 52

Example of Non-DPDA (NPDA) Fall 2004 COMP 335 52

Not allowed in DPDAs Fall 2004 COMP 335 53

Not allowed in DPDAs Fall 2004 COMP 335 53

NPDAs Have More Power than DPDAs Fall 2004 COMP 335 54

NPDAs Have More Power than DPDAs Fall 2004 COMP 335 54

It holds that: Deterministic Context-Free Languages (DPDA) Context-Free Languages NPDAs Since every DPDA is

It holds that: Deterministic Context-Free Languages (DPDA) Context-Free Languages NPDAs Since every DPDA is also a NPDA Fall 2004 COMP 335 55

We will actually show: Deterministic Context-Free Languages (DPDA) Context-Free Languages (NPDA) We will show

We will actually show: Deterministic Context-Free Languages (DPDA) Context-Free Languages (NPDA) We will show that there exists a context-free language which is not accepted by any DPDA Fall 2004 COMP 335 56

The language is: We will show: Fall 2004 • is context-free • is not

The language is: We will show: Fall 2004 • is context-free • is not deterministic context-free COMP 335 57

Language is context-free Context-free grammar for Fall 2004 COMP 335 : 58

Language is context-free Context-free grammar for Fall 2004 COMP 335 : 58

Theorem: The language is not deterministic context-free (there is no DPDA that accepts Fall

Theorem: The language is not deterministic context-free (there is no DPDA that accepts Fall 2004 COMP 335 ) 59

Proof: Assume for contradiction that is deterministic context free Therefore: there is a DPDA

Proof: Assume for contradiction that is deterministic context free Therefore: there is a DPDA Fall 2004 that accepts COMP 335 60

DPDA with accepts Fall 2004 COMP 335 61

DPDA with accepts Fall 2004 COMP 335 61

DPDA with Such a path exists because of the determinism Fall 2004 COMP 335

DPDA with Such a path exists because of the determinism Fall 2004 COMP 335 62

Fact 1: The language is not context-free Context-free languages Regular languages (we will prove

Fact 1: The language is not context-free Context-free languages Regular languages (we will prove this at a later class using pumping lemma for context-free languages) Fall 2004 COMP 335 63

Fact 2: The language is not context-free (we can prove this using pumping lemma

Fact 2: The language is not context-free (we can prove this using pumping lemma for context-free languages) Fall 2004 COMP 335 64

We will construct a NPDA that accepts: which is a contradiction! Fall 2004 COMP

We will construct a NPDA that accepts: which is a contradiction! Fall 2004 COMP 335 65

Modify Fall 2004 Replace with COMP 335 66

Modify Fall 2004 Replace with COMP 335 66

The NPDA that accepts Connect final states of with final states of Fall 2004

The NPDA that accepts Connect final states of with final states of Fall 2004 COMP 335 67

Since is accepted by a NPDA it is context-free Contradiction! (since Fall 2004 is

Since is accepted by a NPDA it is context-free Contradiction! (since Fall 2004 is not context-free) COMP 335 68

Therefore: Not deterministic context free There is no DPDA that accepts End of Proof

Therefore: Not deterministic context free There is no DPDA that accepts End of Proof Fall 2004 COMP 335 69