Homework 7


  1. Scott 6.1
  2. Scott 6.8
  3. Scott 6.11: Full credit--handle only the case where the result of a condition is used to jump to the then or else branch. You need only use an L-attributed grammar in this case.

    Extra credit (20 points)--handle both the case where the result of a condition is used as a value in a comparison or is used to jump to the then or else branch. The book has done something tricky and non-intuitive with the grammar. Specifically the grammar allows the result of a boolean condition to be used in a numerical comparison (e.g., (a or b) < c). If you use short-circuiting, then you have to differentiate between the case where the result of a condition is being used as a value in a comparison and when it is being used to jump to the then or else branch of a conditional. If you only handle the case where the result is used to jump to the then or else branch of a conditional, you can get away with using an L-attributed grammar. If you want to handle both cases, then you will need to add an inherited attribute to pass along the context (value or jump) and you will not be able to write a set of attribute rules that can be evaluated with a single left-to-right traversal of the syntax tree, which is what is required by an L-attributed grammar.

  4. Scott 6.30: The book's answer makes a dubious assumption that quicksort must concatenate the arrays that are returned from the two recursive calls, when in fact quicksort typically performs an in-place modification of the arrays. According to the normal definition of quicksort, quicksort is tail recursive, at least according to the definition used in the book. Therefore, please interpret the question as asking whether it is possible to combine the two recursive calls that quicksort normally makes at the end of a quicksort routine into a single recursive call.
  5. Scott 7.2 (only answer for structural equivalence and name equivalence; do not answer for loose name equivalence)
  6. Scott 7.15
  7. Scott 7.16
  8. Scott 8.2
  9. Scott 8.4 (on my Mac, I got output that looked like -8748356, -8748355, -8748354, -8748353, -8748352, -8748351, etc., so it may not always be 0, 1, 2, 3, ..., 9 but instead some other random increasing sequence)