CS 3723-001 Programming Languages, Fall 2002 -- Mid-term Exam
Answers (in boldface)

  1. Consider a shift-reduce parser as we had in a recitation.
    1. In such a parser, say what is being shifted and where it is being shifted to. The input tokens from the scanner are being shifted one-by-one onto a special stack used by the parser.
    2. Suppose the grammar rule
           E ---> E '+' T 
        
      is being used for a reduction. Describe briefly what happens during this reduction. A sequence of terminals and non-terminals on the top of the stack is found to match the right side of some grammar rule. (In this case, E, '+', and T.) If a reduction is called for and if it is clear which rule to use, then the matching symbols of the stack are popped off and the single left non-terminal is pushed (in this case E).
    3. How does the parser know whether to shift or to reduce? The top of the stack and the next token are looked up in a special shift-reduce table, which says whether to shift, reduce, accept, or issue an error message. (This is the simple case we considered in class.)

  2. Consider the following slightly modified grammar rule from the course parser assignment:
    1. What does the portion { S } mean on the right side? This means that a sequence of zero or more statements (S) can occur at this point.
    2. What construct is this rule describing? This is describing a while construct.
    3. Write code or pseudo-code for a function W 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 E() and S() 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 parser function W. Here is an answer in Java:
        
           private void W() {
              if (next == '{') { scan(); E(); }
              if (next == '?') {
                 scan();
                 while (next != '}') S();
              }
              else error(7);
              if (next == '}') scan();
              else error(8);
           }
           // the while loop could instead be:
           // while (next == '{' || next == '[' || next == '<' ||  next == '>' ||
               Character.isLetter(next) ) S();
        
  3. Suppose you are generating MIPS code for an if-then-else construct, using the grammar rule
    1. Show what additional MIPS instructions must be generated inside the function I in order to implement the if-then-else. More specifically, show exactly what MIPS instructions and/or labels would need to be inserted by the I function at the three places below labeled
      # INSERT ...
        # Start of code for an if-then-else
        # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
        # Start of if-then-else (no MIPS code needed here)
        # Start of code to evaluate an expression E
           ...
        # Result left in a temporary memory location 144 bytes past
        #   the start of the memory array M.
        # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
                lw      $t1,    144($s1)
                beq     $t1,    $zero,  ThenEnd4
        # Start of code to evaluate the first portion { S }
           ...
        # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
                j       ElseEnd4
        ThenEnd4:
        # Start of code to evaluate the second portion {S}
           ...
        # INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
        ElseEnd4:
        
        
      (Note that there must be a sequence number, like the 4 above, that will distinguish this if-then-else from any other.)
    2. How does the function I find out that the result of evaluating E is going to be in a memory location 144 bytes past the start of M? This value, either in bytes or words, is returned by the E function.

  4. Very briefly say how one can manage to pass a function in Java as a parameter, even though Java does not directly support functions as objects that can be passed. One can embed a function as a member function in a class that is "implementing an interface". Such a class can then be passed as a parameter, when the function to which it is passed is expecting the interface class.

  5. This is a question about 2-dimensional arrays in C/C++/Java:

    1. Suppose you declare a variable s in C or C++ using int s[5][4];. Is this stored as an array of 1-dimensional arrays (along with the 1-dimensional arrays), or is it stored just as a single 1-dimensional array? In C/C++ is it possible to create an array stored the other way? This array is just stored as a 1-dimensional array. The handout about 2-dimensional arrays showed how to declare an array of 1-dimensional arrays.
    2. Suppose you declare a variable s in Java using int[][] s = new int[5][4]; Same two questions as in part a. In the case of Java, the only option is to have an array of references to 1-dimensional arrays. (Of course in Java a 2-dimensional array could be simulated inside a 1-dimensional array.)
Points for each problem: 1-25, 2-25, 3-25, 4-10, 5-15.