Compilers - Problem Set #1

Attempt by: Tuesday, February 19, 2008
Submit by: Thursday, February 21, 2008

  1. Write a regular expression that captures the same language as the following context-free grammar:

      S → p R v
      S → q R v
      R → u R
      R → u
    

  2. Give two six-symbol strings that are in the language defined by the following context-free grammar:

      S → a S d
      S → a T d
      T → b c
      T → b T c
    
    Then give a concise English language description for the set of strings specified by the grammar.

  3. Consider the following context-free grammar:

      A → t A e A
      A → t A
      A → x
    
    1. Write two sentences consisting of five or more symbols that are in the language specified by the grammar.

    2. Is the grammar ambiguous? If so, write a sentence and two different parse trees that illustrate that fact. If not, give an argument why not.

  4. Consider this Python fragment for a predictive parser:
          def matchE(tokens):
               if len(tokens) == 0:
                    raise ParseError('unxpected end of input')
               elif tokens[0] == "n":
                    matchChar(tokens, 'n')
               else:
                    matchChar(tokens, '(')
                    matchE(tokens)
                    matchChar(tokens, '+')
                    matchE(tokens)
                    matchChar(tokens, ')')
    
          def matchChar(tokens, c):
               if len(tokens) == 0:
                    raise ParseError('unxpected end of input')
               elif tokens[0] == c:
                    tokens.pop(0)   # eat character c from token stream
               else:
                    raise ParseError('%c expected' % c)
    
    1. Write a context-free grammar that corresponds to this parser.

    2. Suppose we wrote a similar parser for this grammar:

        E → n
        E → E + E
      
      What problems might we encounter when we run our program?

  5. Do one of the following:

    1. Write a computer program in a language of your choosing that takes a context-free grammar for an input and returns true if and only if the grammar is unambiguous.

      or

    2. Write a brief explanation of why you feel that the first part of this question is not a fair question to ask on this problem set.