CS 3723-001 Programming Languages
Spring 2002 -- 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

    Construct the parse tree for the sentence: (a + b^c)*a $

     

     

  3. Consider the following grammar rule from the course parser assignment:

    (Recall that the brackets [] mean an optional item.)

    Write code or pseudo-code for a function ifstmt that could work as part of a recursive descent parser for a grammar including this grammar rule. You should assume that a scanner is given to return tokens, and that separate functions expr() and stmt() are available. Use any convenient notation or language. Do not try to supply I/O statements, header files or other details. Just give the bare code for the function ifstmt.

     

     

  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  |     |     |
    -----+-----+-----+-----+-----+-----+-----+
    
    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 $.)

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