CS 3723-001 Programming Languages
Fall 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
    
             declist --->  dec  |  dec  declist
             dec     --->  type  idlist  ";"
             type    --->  "double"  |   "int"
             idlist  --->  id  |  id, idlist
    
    1. Construct the parse tree for the sentence: int id, id; double id;

    2. Use the {} notation (which means "zero or more occurrences of") to rewrite the above grammar (two uses of {} required).

  3. 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 $

  4. Consider the grammar from the review for this exam:
    
            S ---> b M b              ("S" is the start symbol)
            M ---> ( L
            M ---> a
            L ---> M a )
    
    Here is a recursive descent parser for this grammar, with two sections of code missing. It also has an initial "P" symbol, and a terminal "$". Fill in the two code sections, including the calls to error in case of an error. (You don't need to know anything about Java to do this problem. If you are not a Java person, just write code as if this were C. The code should be the same in this case.)
    
    /* GrammarTest.java: simple parser -- no output
     * grammar:
     *           P ---> S  "$"
     *           S ---> "b"  M  "b"               (S is the start symbol)
     *           M ---> "("  L
     *           M ---> "a"
     *           L ---> M  "a"  ")"
     */
    import java.io.*;
    class GrammarTest {
       private int level = 0;
       private char next;
    
       private void P() {				private void scan() {
          scan();					   do {
          S();					      try {
          if (next != '$') error(2);		         next = (char)System.in.read();
          else System.out.println("Success");	      } catch (IOException e) {
       }						         System.out.println("Exc reading char");
          						      }
       private void S() {				   } while (Character.isWhitespace(next));
          if (next == 'b') scan();			}
          else error(0);
          M();					private void error(int n) {
          if (next == 'b') scan();			   System.out.println("*** ERROR: " + n);
          else error(1);				   System.exit(1);
       }						}
    
       private void M() {				public static void main(String[] args) {
          // supply code here		           GrammarTest grammarTest = new GrammarTest();
       }						   grammarTest.P();
    
       private void L() {				}
          // supply code here             }
       }
       
    
    Points for each problem: 1-25, 2-25, 3-25, 4-25.