lecture 21 - non-context-free languages - the pumping lemma

Upload: deepak-kulkarni

Post on 03-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    1/25

    Non-Context-Free Languages - The PumpingLemmaLecture 21

    Section 2.3

    Robb T. Koether

    Hampden-Sydney College

    Mon, Oct 15, 2012

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma

    Mon, Oct 15, 2012 1 / 25

    http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    2/25

    1 Non-Context-Free Languages

    2 The Pumping Lemma

    3 An Example

    4 The Proof

    5 Another Example

    6 Assignment

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma

    Mon, Oct 15, 2012 2 / 25

    http://goforward/http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    3/25

    Non-Context-Free Languages

    It turns out that not all languages are context-free.

    The simplest example of a non-context-free language is

    {anbncn | n 0}.

    To process this would require two stacks.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma

    Mon, Oct 15, 2012 3 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    4/25

    The Pumping Lemma

    Theorem (The Pumping Lemma for Context-Free Grammars)

    If L is a context-free language then there exists an integer p such that

    if any string s L has length at least p, then s may be divided into five

    substrings s= uvxyz such that

    |vy| > 0,

    |vxy| p,

    uvixyiz L for all i 0.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 4 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    5/25

    The Proof

    The proof is somewhat similar to the proof of the Pumping Lemma

    for Regular Languages except that it is based on a grammar

    rather than a machine.

    But first, an example. . .

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 5 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    6/25

    An Example

    Example

    Let L = {anbncn | n 0}.We will use the Pumping Lemma to show that L is not context-free.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 6 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    7/25

    An Example

    Proof.

    Suppose L is context-free.

    Then let p be the pumping length of L (for CFLs).

    Let s= apbpcp.

    Then s= uvxyz such that |vy| > 0, |vxy| p, and uvixyiz L.

    We will show that this is not possible.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 7 / 25

    http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    8/25

    An Example

    Proof.

    vxy is the middle part of uvxyz and it has length at most p.

    Therefore, it consists of

    Case 1: All as,Case 2: Some as followed by some bs,

    Case 3: All bs,

    Case 4: Some bs followed by some cs, or

    Case 5: All cs.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 8 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    9/25

    An Example

    Proof.

    It is enough to consider the first two cases.

    The other three cases are similar.

    Case 1: Suppose vxy consists of all as.Then v= ak and y= am for some k, m, not both 0.So uv2xy2z= ap+k+mbpcp, which is not in L.This is a contradiction.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 9 / 25

    http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    10/25

    An Example

    Proof.

    Case 2: vxy consists of some as followed by some bs.There are three possibilities:

    v is all as and y is all bs,

    v is all as and y is some as followed by some bs,

    v is some as followed by some bs and y is all bs.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 10 / 25

    http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    11/25

    An Example

    Proof.Case 2, continued. . .

    It doesnt really matter which is the case because both v and y getpumped up.Let k be the number of as and m be the number of bs altogether in

    vy, m and k are not both 0 (but possibly m= k).So uv2xy2z will contain p+ k as and p+mbs, but only pcs.So uv2xy2z / L.This is a contradiction.

    Cases 3, 4, and 5 are similar.

    Therefore, L is not context-free.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 11 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    12/25

    The Idea Behind the Proof

    If a CFL contains a string w with a sufficiently long derivation

    S

    w,

    then some variable A must appear more than once in the

    derivation.

    That is, we must have

    S

    uAz

    uvAyz

    uvxyz,

    for some strings u, v, x, y, and z.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 12 / 25

    http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    13/25

    The Idea Behind the Proof

    Thus, A

    vAy and A

    x.

    We may repeat the derivation

    A vAy

    as many times as we like (including zero times), producing strings

    uvnxynz, for any n 0.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 13 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    14/25

    The Proof

    Proof.

    Let b be the largest number of symbols on the right-hand side of

    any grammar rule. (Assume b 2.)

    Let h be the height of the derivation tree of a string s.Then s can contain at most bh symbols.

    Equivalently, if s contains more than bh symbols, then the height

    of the derivation tree of s must be more h.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 14 / 25

    Th P f

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    15/25

    The Proof

    Proof.

    Now |V| is the number of variables in the grammar of L.

    So if a string in L has a length greater than b|V|+1, then the height

    of its derivation tree must be more than |V|+ 1.

    So let p= b|V|+1 and suppose that a string s L has length atleast p.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 15 / 25

    Th P f

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    16/25

    The Proof

    Proof.

    Consider the longest path through the derivation tree of s.

    It has length at least |V|+

    1.That path has |V|+ 2 nodes on it, counting the root node S andthe leaf node, which is a terminal.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 16 / 25

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    17/25

    Th P f

  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    18/25

    The Proof

    u z

    v y

    x

    A

    A

    S

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 18 / 25

    The Proof

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    19/25

    The Proof

    Proof.

    The middle part of this tree, the part that produces

    A vAy,

    may be repeated as many times as desired.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 19 / 25

    The Proof

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    20/25

    The Proof

    u z

    v yA

    A

    S

    x

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 20 / 25

    The Proof

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    21/25

    The Proof

    u z

    v y

    A

    S

    x

    Av y

    A

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 21 / 25

    The Proof

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    22/25

    The Proof

    Proof.

    Therefore, the strings uv2xy2z, uv3xy3z, etc. can also be derived.

    So can the string uxz.

    Furthermore, we may assume that this was the shortest derivation

    of s.

    It follows that v and y cannot both be empty strings.

    If they were, then the middle part of the derivation would be

    A

    A,

    which could be eliminated.

    Thus, |vy| > 0.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 22 / 25

    The Proof

    http://find/http://goback/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    23/25

    The Proof

    Proof, conclusion.

    Finally, we must show that |vxy| p.

    The subtree rooted at the second-to-last A has height at most|V|+ 1.

    So the string vxy has at most b|V|+1 = p symbols.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 23 / 25

    An Example

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    24/25

    An Example

    Example

    Let = {a,b}.

    Show that the language

    {ww | w }

    is not context-free.

    Use s= apbpapbp.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 24 / 25

    Assignment

    http://find/
  • 7/29/2019 Lecture 21 - Non-Context-Free Languages - The Pumping Lemma

    25/25

    Assignment

    Homework

    Read Section 2.3, pages 125 - 129.

    Exercise 13, page 154.

    Problems 30, 31, page 156.

    Robb T. Koether (Hampden-Sydney College)Non-Context-Free Languages - The Pumping Lemma Mon, Oct 15, 2012 25 / 25

    http://find/