The Pumping Lemma for ContextFree Languages Fall 2004

  • Slides: 74
Download presentation
The Pumping Lemma for Context-Free Languages Fall 2004 COMP 335 1

The Pumping Lemma for Context-Free Languages Fall 2004 COMP 335 1

Take an infinite context-free language Generates an infinite number of different strings Example: Fall

Take an infinite context-free language Generates an infinite number of different strings Example: Fall 2004 COMP 335 2

In a derivation of a long string, variables are repeated A derivation: Fall 2004

In a derivation of a long string, variables are repeated A derivation: Fall 2004 COMP 335 3

Derivation tree Fall 2004 string COMP 335 4

Derivation tree Fall 2004 string COMP 335 4

Derivation tree string repeated Fall 2004 COMP 335 5

Derivation tree string repeated Fall 2004 COMP 335 5

Fall 2004 COMP 335 6

Fall 2004 COMP 335 6

Repeated Part Fall 2004 COMP 335 7

Repeated Part Fall 2004 COMP 335 7

Another possible derivation from Fall 2004 COMP 335 8

Another possible derivation from Fall 2004 COMP 335 8

Fall 2004 COMP 335 9

Fall 2004 COMP 335 9

A Derivation from Fall 2004 COMP 335 10

A Derivation from Fall 2004 COMP 335 10

Fall 2004 COMP 335 11

Fall 2004 COMP 335 11

Fall 2004 COMP 335 12

Fall 2004 COMP 335 12

A Derivation from Fall 2004 COMP 335 13

A Derivation from Fall 2004 COMP 335 13

Fall 2004 COMP 335 14

Fall 2004 COMP 335 14

Fall 2004 COMP 335 15

Fall 2004 COMP 335 15

Fall 2004 COMP 335 16

Fall 2004 COMP 335 16

Fall 2004 COMP 335 17

Fall 2004 COMP 335 17

A Derivation from Fall 2004 COMP 335 18

A Derivation from Fall 2004 COMP 335 18

Fall 2004 COMP 335 19

Fall 2004 COMP 335 19

Fall 2004 COMP 335 20

Fall 2004 COMP 335 20

Fall 2004 COMP 335 21

Fall 2004 COMP 335 21

In General: Fall 2004 COMP 335 22

In General: Fall 2004 COMP 335 22

Consider now an infinite context-free language Let Take Fall 2004 be a grammar for

Consider now an infinite context-free language Let Take Fall 2004 be a grammar for so that it has no unit-productions no -productions COMP 335 23

Let = (Number of productions) x (Largest right side of all productions) Let Example

Let = (Number of productions) x (Largest right side of all productions) Let Example Fall 2004 : COMP 335 24

Take a string with length We will show: in the derivation of a variable

Take a string with length We will show: in the derivation of a variable of is repeated Fall 2004 COMP 335 25

Fall 2004 COMP 335 26

Fall 2004 COMP 335 26

maximum right hand side of the productions Fall 2004 COMP 335 27

maximum right hand side of the productions Fall 2004 COMP 335 27

Number of productions in grammar Fall 2004 COMP 335 28

Number of productions in grammar Fall 2004 COMP 335 28

Number of productions in grammar Some production must be repeated Repeated variable Fall 2004

Number of productions in grammar Some production must be repeated Repeated variable Fall 2004 COMP 335 29

Derivation of string Some variable is repeated Fall 2004 COMP 335 30

Derivation of string Some variable is repeated Fall 2004 COMP 335 30

Derivation tree of string Last repeated variable repeated Strings of terminals Fall 2004 COMP

Derivation tree of string Last repeated variable repeated Strings of terminals Fall 2004 COMP 335 31

Possible derivations: Fall 2004 COMP 335 32

Possible derivations: Fall 2004 COMP 335 32

We know: This string is also generated: Fall 2004 COMP 335 33

We know: This string is also generated: Fall 2004 COMP 335 33

We know: This string is also generated: The original Fall 2004 COMP 335 34

We know: This string is also generated: The original Fall 2004 COMP 335 34

We know: This string is also generated: Fall 2004 COMP 335 35

We know: This string is also generated: Fall 2004 COMP 335 35

We know: This string is also generated: Fall 2004 COMP 335 36

We know: This string is also generated: Fall 2004 COMP 335 36

We know: This string is also generated: Fall 2004 COMP 335 37

We know: This string is also generated: Fall 2004 COMP 335 37

Therefore, any string of the form is generated by the grammar Fall 2004 COMP

Therefore, any string of the form is generated by the grammar Fall 2004 COMP 335 38

Therefore, knowing that we also know that Fall 2004 COMP 335 39

Therefore, knowing that we also know that Fall 2004 COMP 335 39

Observation: Since Fall 2004 is the last repeated variable COMP 335 40

Observation: Since Fall 2004 is the last repeated variable COMP 335 40

Observation: Since there are no unit or Fall 2004 COMP 335 -productions 41

Observation: Since there are no unit or Fall 2004 COMP 335 -productions 41

The Pumping Lemma: For infinite context-free language there exists an integer such that for

The Pumping Lemma: For infinite context-free language there exists an integer such that for any string we can write with lengths and it must be: Fall 2004 COMP 335 42

Applications of The Pumping Lemma Fall 2004 COMP 335 43

Applications of The Pumping Lemma Fall 2004 COMP 335 43

Non-context free languages Context-free languages Fall 2004 COMP 335 44

Non-context free languages Context-free languages Fall 2004 COMP 335 44

Theorem: The language is not context free Proof: Fall 2004 Use the Pumping Lemma

Theorem: The language is not context free Proof: Fall 2004 Use the Pumping Lemma for context-free languages COMP 335 45

Assume for contradiction that is context-free Since is context-free and infinite we can apply

Assume for contradiction that is context-free Since is context-free and infinite we can apply the pumping lemma Fall 2004 COMP 335 46

Pumping Lemma gives a magic number Pick any string with length We pick: Fall

Pumping Lemma gives a magic number Pick any string with length We pick: Fall 2004 COMP 335 47

We can write: with lengths Fall 2004 and COMP 335 48

We can write: with lengths Fall 2004 and COMP 335 48

Pumping Lemma says: for all Fall 2004 COMP 335 49

Pumping Lemma says: for all Fall 2004 COMP 335 49

We examine all the possible locations of string in Fall 2004 COMP 335 50

We examine all the possible locations of string in Fall 2004 COMP 335 50

Case 1: Fall 2004 is within COMP 335 51

Case 1: Fall 2004 is within COMP 335 51

Case 1: Fall 2004 and consist of only COMP 335 52

Case 1: Fall 2004 and consist of only COMP 335 52

Case 1: Repeating Fall 2004 and COMP 335 53

Case 1: Repeating Fall 2004 and COMP 335 53

Case 1: From Pumping Lemma: Fall 2004 COMP 335 54

Case 1: From Pumping Lemma: Fall 2004 COMP 335 54

Case 1: From Pumping Lemma: However: Contradiction!!! Fall 2004 COMP 335 55

Case 1: From Pumping Lemma: However: Contradiction!!! Fall 2004 COMP 335 55

Case 2: Fall 2004 is within COMP 335 56

Case 2: Fall 2004 is within COMP 335 56

Case 2: Similar analysis with case 1 Fall 2004 COMP 335 57

Case 2: Similar analysis with case 1 Fall 2004 COMP 335 57

Case 3: Fall 2004 is within COMP 335 58

Case 3: Fall 2004 is within COMP 335 58

Case 3: Similar analysis with case 1 Fall 2004 COMP 335 59

Case 3: Similar analysis with case 1 Fall 2004 COMP 335 59

Case 4: Fall 2004 overlaps COMP 335 and 60

Case 4: Fall 2004 overlaps COMP 335 and 60

Case 4: Possibility 1: Fall 2004 COMP 335 contains only 61

Case 4: Possibility 1: Fall 2004 COMP 335 contains only 61

Case 4: Possibility 1: Fall 2004 COMP 335 contains only 62

Case 4: Possibility 1: Fall 2004 COMP 335 contains only 62

Case 4: From Pumping Lemma: Fall 2004 COMP 335 63

Case 4: From Pumping Lemma: Fall 2004 COMP 335 63

Case 4: From Pumping Lemma: However: Contradiction!!! Fall 2004 COMP 335 64

Case 4: From Pumping Lemma: However: Contradiction!!! Fall 2004 COMP 335 64

Case 4: Possibility 2: Fall 2004 COMP 335 contains and contains only 65

Case 4: Possibility 2: Fall 2004 COMP 335 contains and contains only 65

Case 4: Possibility 2: Fall 2004 COMP 335 contains and contains only 66

Case 4: Possibility 2: Fall 2004 COMP 335 contains and contains only 66

Case 4: From Pumping Lemma: Fall 2004 COMP 335 67

Case 4: From Pumping Lemma: Fall 2004 COMP 335 67

Case 4: From Pumping Lemma: However: Contradiction!!! Fall 2004 COMP 335 68

Case 4: From Pumping Lemma: However: Contradiction!!! Fall 2004 COMP 335 68

Case 4: Possibility 3: Fall 2004 COMP 335 contains only contains and 69

Case 4: Possibility 3: Fall 2004 COMP 335 contains only contains and 69

Case 4: Possibility 3: contains only contains and Similar analysis with Possibility 2 Fall

Case 4: Possibility 3: contains only contains and Similar analysis with Possibility 2 Fall 2004 COMP 335 70

Case 5: Fall 2004 overlaps COMP 335 and 71

Case 5: Fall 2004 overlaps COMP 335 and 71

Case 5: Similar analysis with case 4 Fall 2004 COMP 335 72

Case 5: Similar analysis with case 4 Fall 2004 COMP 335 72

There are no other cases to consider (since overlap Fall 2004 , string ,

There are no other cases to consider (since overlap Fall 2004 , string , and COMP 335 cannot at the same time) 73

In all cases we obtained a contradiction Therefore: The original assumption that is context-free

In all cases we obtained a contradiction Therefore: The original assumption that is context-free must be wrong Conclusion: Fall 2004 is not context-free COMP 335 74