CS 3723-001 Programming Languages
Spring 2001 -- Mid-term Exam

Directions: Use your own paper for answers. When you are done you may keep this exam sheet and should pick up an answer sheet.

  1. Use the algorithm from class to convert the following NFA to a DFA that recognizes the same language:

  2. Consider the formal Grammar
    
             P ---> E '#'
             E ---> E '+' T | E '-' T | T
             T ---> T '*' F | T '/' F | F
             F ---> 'a' | 'b' | 'c' | '(' E ')'
    
    1. Is this grammar ambiguous? (Yes or no.)

    2. Construct the parse tree for the sentence: ( a - b ) / c #

    3. Why won't this grammar work for a recursive descent parser?
     

  3. Suppose you are writing a recursive descent parser for Lisp S-expressions, as shown in a handout in class. Suppose you use the following grammar:
    
                sexpr --->  digit  |  '('    tail
                tail  --->  ')'    |  sexpr  tail
    
    Here you already have a scanner that will keep returning one of three tokens: a digit, a left parenthesis, or a right parenthesis. Without actually writing code, give a rough idea of what the parser would look like by answering the questions:

    1. What functions would you write? (Besides the scanner.)

    2. Give an outline of what happens inside each function. (Use English, or pseudo-code or by some other means, and just say what the code is doing.)

     

     

  4. Consider the following grammar:
    
            S  --->  E                      ("S" is the start symbol)
            E  --->  E '+' T  |  T
            T  --->  T '*' F  |  F
            F  --->  '(' E ')'  |  id
    
    Use the following shift-reduce table for this grammar:
    
         |  id |  *  |  +  |  (  |  )  |  $  |
    -----+-----+-----+-----+-----+-----+-----+
      S  |     |     |     |     |     | acc |  ( "s" means "shift")
      E  |     |     |  s  |     |  s  |  r  |
      T  |     |  s  |  r  |     |  r  |  r  |  ( "r" means "reduce")
      F  |     |  r  |  r  |     |  r  |  r  |
      id |     |  r  |  r  |     |  r  |  r  |  ( "acc" means "accept")
      *  |  s  |     |     |  s  |     |     |
      +  |  s  |     |     |  s  |     |     |    
      (  |  s  |     |     |  s  |     |     |
      )  |     |  r  |  r  |     |  r  |  r  |
      $  |  s  |     |     |  s  |     |     |
    -----+-----+-----+-----+-----+-----+-----+
    
    1. In the table above, what do the blank entries mean?

    2. Carry out the shift-reduce parse of the following sentence, showing the stack, current symbol, remaining symbols, and next action to take at each stage. (This sentence has the extra artifical symbol $ stuck in at the beginning and the end.)
      
           $ id * ( id + id ) $
      
      (Remember that you should initially shift the starting $.)

    3. What would happen if you started processing the following input string? $ id * + id $


    Points for each problem: 1-25, 2-25, 3-25, 4-25.