Sample Midterm Examination -- CS 434 -- Fall 1994

This is a self-scheduled, open book examination. You should return the examination within 2 hours of the time at which you pick it up. You may consult any texts or notes while completing the examination.

  1. (25 pts) Build the LR(0) machine for the grammar:
    S 0 S 1  |  A
    A 1 A  |  1

    Is the grammar an LR(0) grammar? Justify your answer.

  2. (25 pts) The first exercise at the end of the chapter on garbage collection in the text by Field and Harrison states "Using a copying collector the relative amount of time spent garbage collecting decreases (ultimately to zero) as the size of the heap increases". They attribute this quote to "Fred", but they were probably actually paraphrasing Andrew Appel, the author of the paper on garbage collection I distributed who says in that paper: "The cost of garbage-collecting a record can be less than one instruction, on an amortized basis"(p. 175).

    The exercise goes on to ask several questions about the opinion expressed by the quote(s). I'd like to ask the same questions (and one extra):

    1. What do these quotes mean? How can they claim that the cost of garbage collection decreases as the size of the heap increases? What happens as the size of the heap decreases (i.e. what happens in the worst case).
    2. Why are these remarks true for copying garbage collectors, but not for mark-scan collectors.
  3. One limitation of LR(0) parsing is that only unambiguous grammars can be LR(0). For this problem, I would like you to either show that this is true in general or at least show it in a particular case (described below). The gimmick here is that if you find the general case too daunting, I want to provide a simpler problem you can work on (with the understanding that a correct answer to the simple problem is worth less than a correct answer to the general problem as indicated by the point values shown below). So, please either:
    1. (25 pts) Prove that if a grammar is ambiguous, it can not be an LR(0) grammar. (Hint: Recall that there are several, equivalent conditions that can be used to determine whether a grammar is ambiguous.)
    2. (15 pts) Show that any grammar that contains both left and right recursive productions with the same left-hand side symbol can not be LR(0). (I had you show that such grammars must be ambiguous as a homework problem.)
  4. (25 pts.) Consider the changes that would be required in your JACCL compiler if the definition of JACCL were extended to include support for enumeration types similar to those in PASCAL. In particular, suppose that we extend the syntax of the language by adding the productions:
    < type specification > < enumeration type specification >
    < enumeration type specification > ( < identifier list > )
    < identifier list > < identifier >
     |  < identifier list > , < identifier >
    < factor > succ ( < expression > )
     |  pred ( < expression > )
    The names included in the identifier list in an enumeration type specification will be usable as constants denoting the values of the type within the program. As in PASCAL, the elements of an enumeration type will be ordered according to the order in which they appeared in the type's specification. Applying the succ operation to an element of an enumeration type will return the next element (i.e. the successor). Applying pred will return the previous elements. The results of applying succ to the last element of an enumeration type or pred to the first are not defined (that is, as in the rest of the compiler, I am not expecting you to check for run-time errors).

    As defined originally, JACCL only allowed assignment to variables of type integer. To make enumerations useful, we must allow assignments to variables of enumeration types. Each enumeration type, however, is a distinct type. Accordingly, a compiler's type checking should ensure that the only values that can be legally assigned to a variable or passed to a parameter declared to be of a given enumeration type are the values of the type.

    1. How would you handle enumeration types in the symbol table of your compiler? In particular:
      • Describe the information you would keep in the declaration descriptor for the name of an enumeration type.
      • Describe the information you would keep in the declaration descriptor for the name of a member of an enumeration type.
      • How would you handle the declaration descriptors for elements of an enumeration type? Would they be kept in "declstack"s like variables, included in the hash table used for record component names or would they require some third form of lookup structure?
    2. In the unextended JACCL language, only integers could be passed as value parameters or returned by functions. How should enumeration types be treated in this regard? Justify your answer. If you think they should be allowed as parameters or function return values, explain what further extensions to the language (and to your compiler) would be necessary (if any).
    3. In the grammar for these extensions, I have shown the names of the functions succ and pred in boldface to suggest that they are keywords. Do they really need to be treated this specially or could I simply treat them as built in functions like getnum and getch? Explain your answer.

Computer Science 434
Department of Computer Science
Williams College