Fall 2000, CSE 468: Solution of Assignment 4

Exercise 1

  1. No. It can be shown by using the Pumping Lemma: for any positive n, consider the string x = ((...(p)...)) where the symbol "(" occurs n times. Let uvw = x with |uv| not greater than n, and |v| > 0. Then v contains only the symbol "(". By the Pumping Lemma, xv2w should be in the language, but that's not possible because xv2w contains more "(" than ")".
  2. There are several cases of ambiguity: associativity of the operators "and", "or" and "implies", and precedence between them. The following is an example of ambiguity related to precedence, showing that the string "p and q or r" has two different derivation trees:
             S            S
           / | \        / | \
          S  or S      S and S
        / | \   |      |   / | \
       S and S  r      p  S  or S
       |     |            |     |
       p     q            q     r    
    

  3. A possible grammar is the following:
       S -> T implies S | T
       T -> T or U | U
       U -> U and V | V
       V -> p | q | r | (S)
    

Exercise 2

  1. The grammar is ambiguous because of the associativity problem with the concatenation (production S -> SS). For instance, the string ( )( )( ) has two different derivation trees:
             S              S
           /   \          /   \
          S     S        S     S
        /  \   /|\      /|\   /  \
       S    S ( S )    ( S ) S    S
      /|\  /|\  |        |  /|\  /|\    
     ( S )( S )            ( S )( S )      
       |    |                |    |
    

  2. A possible non-ambiguous grammar G for the same language is:
       S -> lambda | ( S ) S 
    

    We prove now G is not ambiguous. By structural induction: Consider a string x in L(G). We have two cases:

Exercise 3

  1. A possible grammar G is the following:
       S -> a b b | a S b b
    

    We prove now that L(G) = L where L = {anb2n | n > 0}

  2. A possible grammar G is the following:
       S -> T U
       T -> lambda | a T b
       U -> lambda | b U c
    

    We show now that L(G) = L, where L = {akbmcn | m = k + n}.

    Let G1 be the subgrammar with productions

       T -> lambda | a T b
    
    and let G2 be the subgrammar with productions
       U -> lambda | b U c
    
    Clearly, L(G) = L(G1)L(G2). In fact, x is in L(G) iff S -> T U ->* x, but this is possible iff there exist y, z such that T ->* y, U ->* z, and x = y z.

    Let L1 be the language {akbk | k >= 0} , and let L2 be the language {bncn | n >= 0}. We are going to show that L(G1) = L1 and L(G2) = L2. Since L = L1L2, we can conclude that L(G) = L.