MELJUN CORTES Automata Lecture Non-context-free Languages 2

Embed Size (px)

Citation preview

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    1/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 1 of 34

    Non-Context-Free Languages

    NON-CONTEXT-FREE

    LANGUAGES

    Non-Context-Free Languages

      Introduction

    •   Recall the following:

     A regular language has either 

    a nondeterministic automatonthat recognizes it or a regular 

    expression that can generate

    its strings.

     A context-free language haseither a pushdown automaton

    that recognizes it or a context-

    free grammar that can

    generate its strings.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    2/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 2 of 34

    Non-Context-Free Languages

    •   Questions:

    1. Are there non-context-free

    languages?

    2. How can a language beproven to be non-context-

    free?

    •   The language   L= {an

    bn

    c n

    n ≥1} is non-context-free since

    there is no PDA that can

    recognize it.

    •   To prove that a language is

    non-context-free, it must be

    shown that there is no PDA or 

    CFG for that language (difficult

    to do).

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    3/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 3 of 34

    Non-Context-Free Languages

    •   Recall that the pumpinglemma was used to prove that

    a language is not regular.

    The pumping lemma for 

    regular languages exploited aproperty of regular languages

    to prove the non-regularity of 

    languages.

    Strings of regular languageshave the property that if they

    are long enough, then they

    have a substring that can be

    repeated an arbitrary number 

    of times and the resultingstrings will still be in language.

    Languages which have strings

    that do not have this property

    are considered not regular.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    4/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 4 of 34

    Non-Context-Free Languages

    •   To prove that a language is

    non-context-free, there mustbe a pumping lemma for 

    context-free languages.

    This pumping lemma should

    also exploit a certain property

    of context-free languages.

    Languages whose strings do

    not have this property areconsidered non-context-free.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    5/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 5 of 34

    Non-Context-Free Languages

     Context-Free Grammars Revisited

    •   Assume the following context-

    free grammar   G   for language

    L:

    S → A1

     A → 0A1   ε 

    •   The leftmost derivation for the

    string 0001111 is

    S → A1

    → 0A11

    → 00A111

    → 000A1111

    → 0001111

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    6/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 6 of 34

    Non-Context-Free Languages

    •   The parse tree for the leftmostderivation for the string

    0001111 is

    A 10

    ε

    A 10

    A 10

    A 1

    S

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    7/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 7 of 34

    Non-Context-Free Languages

    •   Notice that because the string

    being derived is long, some

    variables were used more than

    once.

    •   In the pumping lemma for 

    regular languages, strings

    whose length is greater than or 

    equal to the number of DFA

    states (the pumping length)have a substring that can be

    repeated an arbitrary number 

    of times, and the resulting

    strings will still be in the

    language.

    •   For the pumping lemma for 

    context-free languages, what

    is then the pumping length?

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    8/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 8 of 34

    Non-Context-Free Languages

     Deriving the Pumping Length

    •   Assume that for a certain

    parse tree, the non-leaf nodes

    have a maximum number of 

    children equal to  c  (this implies

    that the right-hand side of the

    rules of the grammar has a

    string whose length is ≤   c ).

     Assume also that in the

    succeeding discussions c ≥ 2.

    •   Assume further that the height

    of the parse tree for the

    derivation of a certain string   w 

    is h.

    •   A tree with height h can have a

    maximum number of leaf 

    nodes equal to c h.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    9/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 9 of 34

    Non-Context-Free Languages

    •   Since the leaf nodes of the

    parse tree represent the

    symbols of the string   w  being

    derived, then this means that

    w  ≤ c h

    •   Assume now that the length of 

    a certain string w  is

    w  ≥ c v  + 1

    where   v    is the number of 

    variables in the grammar.

    This implies that the height of 

    the parse tree will be

    h ≥ v  + 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    10/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 10 of 34

    Non-Context-Free Languages

    •   If   h ≥   v  + 1, then there is a

    path from the root node to a

    leaf node that is greater than

    or equal to v  + 1.

    This path will have at least  v  +

    2 nodes. If the last node is a

    terminal, then the remainingnodes are variables.

    Since the derivation used

    more than v  + 1 variables, then

    at least one variable was

    repeated (by the pigeonhole

    principle).

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    11/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 11 of 34

    Non-Context-Free Languages

    •   This is illustrated below:

    S uAz  uvAyz  uvxyz 

     Assume that this parse tree

    represents the shortest

    derivation for the string S.

    S

    A. . . . . .

    u z 

    A. . . . . .

    v  y 

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    12/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 12 of 34

    Non-Context-Free Languages

    •   If the derivation   A     vAy   is

    repeated before the derivation

     A       x , then the parse tree

    would look like:

    S uAz  uvAyz  uvvAyyz 

    uvvxyyz  uv 2 xy 2z 

    S

    A. . . . . .

    u z 

    A. . . . . .

    v  y 

    A. . . . . .

    v  y 

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    13/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 13 of 34

    Non-Context-Free Languages

    •   If the derivation   A     vAy   isrepeated several times, then

    the string derived will look like

    uv i  xy i z 

    and this string would still

    belong to the language

    represented by the grammar.

    •   In the pumping lemma for regular languages, the input

    string w  can then be written as

    w  =  xy i z 

    where the middle substring   y 

    can be pumped several times

    and the resulting string will still

    belong to the language

    concerned.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    14/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 14 of 34

    Non-Context-Free Languages

    •   In the pumping lemma for 

    context-free languages, theinput string   w   can then be

    written as

    w   = uv i  xy i z 

    where the 2nd and 4th

    substrings   v   and   y   can be

    pumped several times and the

    resulting string will still belongto the language concerned.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    15/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 15 of 34

    Non-Context-Free Languages

    •   In the pumping lemma for regular languages, the pumping

    length  p refers to the number of 

    states of the DFA recognizing

    the language.

    Hence,   w  ≥   p for the pumping

    lemma to be used.

    •   In the pumping lemma for 

    context-free languages, thepumping length  p is equal to the

    term   c v  + 1 where   c  is the length

    of the longest right-hand side

    string of any rule in the

    grammar and  v  is the number of variables of the grammar.

    Similarly, the length of the string

    w    being analyzed must be

    greater than c v  + 1.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    16/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 16 of 34

    Non-Context-Free Languages

     The Pumping Lemma for Context-

    Free Languages

    •   If  L is a context-free language,

    then there is a number   p (the

    pump ing leng th  ) where, if   w 

    is any string in  L whose length

    is greater than or equal to   p,

    then  w  may be divided into five

    parts,  w  =  uvxyz , satisfying the

    following conditions:

    1. for each i ≥ 0, uv i xy iz 

    L,

    2.   vy  ≥ 1, and

    3.   vxy p.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    17/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 17 of 34

    Non-Context-Free Languages

    •   Major points:

    1. The string   w  =   uv i xy iz L

    for each i ≥ 0.

    This condition simply

    states that strings

    belonging to language   L

    that are longer than thepumping length can be

    divided into 5 parts  u,  v ,  x ,

    y , and z .

    The 2nd and 4th parts can

    be repeated several times

    and the resulting string will

    still belong to language L.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    18/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 18 of 34

    Non-Context-Free Languages

    2.   vy  ≥ 1 refers to the fact

    that substrings   v   and   y 

    cannot both be empty

    strings.

    If   v   =   y   = , then the

    derivation   A     vAy 

    becomes  A  A. This is a

    useless derivation and

    therefore can be removed.

    This, however, violates the

    assumption that the parse

    tree presented depicts theshortest derivation for the

    string S.

    Therefore   vy  ≥ 1.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    19/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 19 of 34

    Non-Context-Free Languages

    3.   vxy p

    Consider again the

    following parse tree:

    If    w  ≥  p, then the height

    of the tree h ≥ v + 1.

    S

    A. . . . . .

    u z 

    A. . . . . .

    v  y 

    h ≥ v + 1 

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    20/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 20 of 34

    Non-Context-Free Languages

    Consider now the following

    subtree (rooted at the

    upper variable   A)   of the

    given parse tree:

    A

    A. . . . . .

    v  y 

    h ≤ v + 1 

    root of

    the

    subtree

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    21/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 21 of 34

    Non-Context-Free Languages

    Take note that no other 

    variable is repeated below

    the root node of this

    subtree.

    It follows that this subtree

    has a height:

    h ≤ v  + 1

    The string formed by this

    subtree then has a length:

    vxy c  v  + 1

    vxy p

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    22/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 22 of 34

    Non-Context-Free Languages

    •   As in the pumping lemma for regular languages, proving a

    language is non-context-free

    will use proof by contradiction:

    1. First, assume that thelanguage L is context-free.

    2. Since  L is context-free, the

    pumping lemma states that

    all strings of  L that are long

    enough can be pumped.

    3. Find a string  w  that is long

    enough that cannot be

    pumped.

    4. If there is such a string,

    then the pumping lemma

    was contradicted. Hence,

    language   L is non-context-

    free.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    23/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 23 of 34

    Non-Context-Free Languages

    • Example 1:   Prove that the

    language   L1 = {anbnc n n ≥ 1}

    is not context-free.

     Assume first that  L1 is context-free.

    Let  p be the pumping length of 

    L1.

    Let the string to be pumped be

    w  = a pb pc  p.

    Since   w  ≥ p, the string chosen

    is long enough to be pumped.

    Consider now the various

    ways in which   w    can be

    divided into the u, v , x , y , and z 

    substrings.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    24/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 24 of 34

    Non-Context-Free Languages

    Option 1:   Let vxy  be all a’s.

    Pumping   v   and   y   would

    create a string where there

    are more   a’s   than   b’s  and

    c ’s. Hence, the resulting

    string will not be in   L1.

    This violates condition 1 of 

    the pumping lemma.

    Option 2:   Let   vxy  be somea’s followed by some b’s.

    Pumping   v   and   y   would

    create a string where there

    are more   a’s  and   b’s   thanc ’s. Hence, the resulting

    string will not be in   L1.

    This violates condition 1 of 

    the pumping lemma.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    25/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 25 of 34

    Non-Context-Free Languages

    Option 3:   Let vxy  be all b’s.

    Pumping   v   and   y   would

    create a string where there

    are more   b’s   than   a’s  and

    c ’s. Hence, the resulting

    string will not be in   L1.

    This violates condition 1 of 

    the pumping lemma.

    Option 4:   Let   vxy  be someb’s followed by some c ’s.

    Pumping   v   and   y   would

    create a string where there

    are more   b’s  and   c ’s   thana’s. Hence, the resulting

    string will not be in   L1.

    This violates condition 1 of 

    the pumping lemma.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    26/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 26 of 34

    Non-Context-Free Languages

    Option 5:   Let vxy  be all c ’s.

    Pumping   v   and   y   would

    create a string where thereare more   c ’s   than   a’s  and

    b’s. Hence, the resulting

    string will not be in   L1.

    This violates condition 1 of 

    the pumping lemma.

    Since   L1   has a string that

    cannot be pumped, then this

    contradicts the original

    assumption that   L1   is context-

    free. Therefore,   L1   is non-

    context-free.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    27/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 27 of 34

    Non-Context-Free Languages

    • Example 2:   Prove that the

    language   L2  = {ww w    {0,

    1}*} is not context-free.

     Assume first that  L2 is context-free.

    Let  p be the pumping length of 

    L2.

    Let the string to be pumped be

    w  = 0 p1 p0 p1 p.

    Since   w  ≥ p, the string chosen

    is long enough to be pumped.

    Consider now the various

    ways in which   w    can be

    divided into the u, v , x , y , and z 

    substrings.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    28/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 28 of 34

    Non-Context-Free Languages

    Option 1:   Let   vxy  be all 0s

    from the first or second 0 p of the string.

    Pumping   v   and   y   would

    create a string where the

    first half is not equal to thesecond half. Hence, the

    resulting string will not be

    in   L2. This violates

    condition 1 of the pumping

    lemma.

    Option 2:   Let   vxy   be all 1s

    from the first or second 1 p of 

    the string.

    Pumping   v   and   y   would

    create a string where the

    first half is not equal to the

    second half. Hence, the

    resulting string will not be

    in L2.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    29/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 29 of 34

    Non-Context-Free Languages

    Option 3:   Let  vxy  be some 0s

    followed by some 1s from the

    first or second half of the

    string.

    Pumping   v   and   y   would

    create a string where thefirst half is not equal to the

    second half. Hence, the

    resulting string will not be

    in   L2. This violates

    condition 1 of the pumpinglemma.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    30/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 30 of 34

    Non-Context-Free Languages

    Option 4:   Let  vxy  be some 1sfollowed by some 0s

    somewhere in the middle

    portion of the string.

    Pumping   v   and   y   wouldcreate a string where the

    first half is not equal to the

    second half. Hence, the

    resulting string will not be

    in   L2. This violatescondition 1 of the pumping

    lemma.

    Since   L2   has a string that

    cannot be pumped, then this

    contradicts the original

    assumption that   L2   is context-

    free. Therefore,   L2   is non-

    context-free.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    31/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 31 of 34

    Non-Context-Free Languages

    • Example 3:   Prove that the

    language L3 = {ai b j c k  k ≥ j ≥ i ≥

    0} is not context-free.

     Assume first that  L3 is context-free.

    Let  p be the pumping length of 

    L3.

    Let the string to be pumped be

    w  = a pb pc  p.

    Since   w  ≥ p, the string chosen

    is long enough to be pumped.

    Consider now the various

    ways in which   w    can be

    divided into the u, v , x , y , and z 

    substrings.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    32/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 32 of 34

    Non-Context-Free Languages

    Option 1:   Let vxy  be all a’s.

    Pumping   v   and   y   would

    create a string where there

    are more   a’s   than   b’s  and

    c ’s. Hence, the resulting

    string will not be in   L3.

    This violates condition 1 of 

    the pumping lemma.

    Option 2:   Let   vxy  be somea’s followed by some b’s.

    Pumping   v   and   y   would

    create a string where there

    are more   a’s  and   b’s   thanc ’s. Hence, the resulting

    string will not be in   L3.

    This violates condition 1 of 

    the pumping lemma.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    33/34

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 33 of 34

    Non-Context-Free Languages

    Option 3:   Let vxy  be all b’s.

    Pumping   v   and   y   would

    create a string where there

    are more   b’s   than   c ’s.

    Hence, the resulting stringwill not be in   L3. This

    violates condition 1 of the

    pumping lemma.

    Option 4:   Let   vxy  be someb’s followed by some  c ’s.

    Pumping down   v   and   y 

    would create a string

    where there are more   a’s

    than   b’s  and   c ’s. Hence,

    the resulting string will not

    be in   L3. This violates

    condition 1 of the pumping

    lemma.

  • 8/21/2019 MELJUN CORTES Automata Lecture Non-context-free Languages 2

    34/34

    Theory of Computation (With Automata Theory)

    Option 5:   Let vxy  be all c ’s.

    Pumping down   v   and   y 

    would create a string

    where there are more   a’s

    and   b’s   than   c ’s. Hence,

    the resulting string will not

    be in   L3. This violates

    condition 1 of the pumping

    lemma.

    Since   L3   has a string that

    cannot be pumped, then this

    contradicts the originalassumption that   L3   is context-

    free. Therefore,   L3   is non-

    context-free.