CS 3723/3731, Final Exam, Fall 2004


  1. Grammars and Syntax-directed Translation:

    Consider the separate sheet with Java code, starting with the definition of the Tnode class. I have removed most of the comments from this code. The listing consists of two executable classes: SearchTree and TraverseTree. Each of these classes make use of two other classes, also listed: Tnode and GetChar. (GetChar just fetches the next input character.) In class we considered an example similar to this, written in C.

    Here is an interactive session of the execution of these two programs. (Boldface is user input. % is the Unix/Linux prompt. The first input is just the first letters of the days of the week, and I then added the first letters of the months.)

    Answer the following questions:

    1. Using the program SearchTree, the input mtwtfss produces output

        ( m ( f () () ) ( t ( s () () ) ( w () () ) ) )#

      This output represents a tree. What kind of tree? Draw a diagram of the tree.

    2. Both programs piped together produce the following output for the above input:

        fmstw

      What does this output mean? Where does it come from?

    3. Given the input abcdefg, what would the two programs piped together produce as output?

    4. The boldface portion of the class TraverseTree in a parser. What kind of parser is it? Give a grammar that this parser might have been written from.

    5. Suppose we want to use 0 to represent the null tree, rather than (). In this case, the input mtwtfss would produce the output:

        ( m ( f 0 0 ) ( t ( s 0 0 ) ( w 0 0 ) ) )#

      What changes would be needed to both the programs SearchTree and TraverseTree to implement this change? (Give changes by line number or by marking on the listing.)


  1. The Tiny Translation Recitation:

    This question concerns the series of recitations used to translate programs from the "Tiny" language to MIPS assembler code. The portion of the grammar for Tiny that handles the if-else statements is:

    Suppose you have the following actual Tiny code that is an if-else statement:

    Here is a table showing the Tiny code at the left, the MIPS assembler code in the middle (where the commented code starting with "code to" is assumed to be generated by other parts of the compiler), and on the right a simple parser, with "// Do something" comments where additional MIPS needs to be generated. (In this parser, the function L() parses a list of statements.)

    Tiny CodeMIPS CodeParser for if-else in Java
    
    
      
      [
      
    
      10 - n
      
    
    
      
      ?
      
    
      n = n - 1;
      
    
      :
      
    
      
      
      
      a = b;
    
      
    
      ]
    
    
    
    
    
      # start of if-else
    
    
      # code to evaluate 10 - n
      # return location
      
      (Insert A)
      (Insert B)
      # start of then part
      
    
      # code to do n = n - 1
    
    
      # end of then part; start of else
      (Insert C)
    
      (Insert D)
    
      
      # code to do a = b;
      
      (Insert E)
    
      # end of if-then
      
    
    
    
    
       private void I() {
          
          int res;
          if (next == '[') {
             scan();
             res = E();
          }
          // Do something A
          // Do something B
          if (next == '?') {
             scan();
             
             L();
          }
          
          if (next == ':') {
             // Do something C
          }
          // do something D
          if (next == ':') {
             scan();
             L();
             if (next == ']') scan();
             // Do something E
          }
          else if (next == ']') scan();
       }
    
    

    1. Give the additional MIPS code (a label or instruction) that needs to be present at positions A, B, C, D, and E. (I'm not tricking you here -- each of the positions needs something. However, you may have structured things somewhat differently.)

    2. The parser code also works if the else part is missing, and if you do the 'Do somethings"correctly, the MIPS code will be correct. Show how this works out with the example above in case it is missing the else part, that is, if ": a = b;" is missing above.


  1. The Lisp Language:

    1. In each case say what the Lisp interpreter would produce when it evaluates the given input (there are no errors):
      1. (car '((a b) c d (e)))
      2. (cdr '(a (b c d)))
      3. (cdr (car '((a b) c d)))
      4. (cons '(a b) '(c))
      5. (list '(a) '(b c))
      6. (append '(a) '(b c))
      7. (cond ((= 6 (* 2 3)) 47)
               (t 54))
      8. ()

    2. Write a recursive Lisp function harmonic, where (harmonic n) returns the value

        
        1 + (1/2) + (1/3) + . . . + (1/(n-1)) + (1/n).
        

      Your function can calculate the answer either as a double or as a fraction.

    3. Draw a diagram showing the internal representation in Lisp of the S-expression:

        
        ((a b) (c d) e)
        


  1. The Postscript Language:

    This part will work with the first and third pictures shown below. (The middle one is just for fun.)

    Cross 1: cross1.ps Cross 2: cross2.psCross 3: cross3.ps

    The following Postscript code produces Cross 1 with its center at the origin. The variable r below is half the narrow width of a "blade", s is half the wide width of a blade, and t is the length of a blade.

    1. Add to or modify the code for cross1.ps so that the cross is printed at the exact center of the page. [Hints: The page is 8.5 inches wide and 11 inches tall. There are 72 points to an inch.]

    2. Modify the code from part 11 above to produce the picture at the right above, labeled "Cross 3", also at the center of the page. The values for r, s, and t that you need for the three crosses on top of one another are in the table below:

      rstgray
      12 40 800 (black)
      8 35 900.4 (dark gray)
      4 30 1000.8 (light gray)

    3. Add to the code in 12 above so that your last name is printed on top of the crosses, in black, and using a 30-point font. (use /Times-Bold findfont 30 scalefont setfont.) Your name must be centered horizontally and have its lower edge 5 inches from the bottom. You must use the stringwidth function to do the centering. (This feature is not shown in the diagram above.)


  1. The Ruby Language:

    1. This part asks about some random features of Ruby that you should have looked at:

      1. What token is used to terminate many Ruby control structures (such as "if-elsif-else", "while")?
      2. What token does a function definition start with?
      3. Give several types of tokens that Ruby programmers often leave off at the end of lines, although they can be present.
      4. What is the name of the method in a Ruby class that does the initialization (in place of the constructors in Java and C++)?
      5. The class Date in Recitation 13 had the following line as the second line attr_reader :year, :month, :day. What is the purpose of this line (what do you get from it)?
      6. What do local variables of a function start with?
      7. What do class instance variable of a class start with?
      8. What do global variables start with?
      9. What do constants start with?

    2. This question is about Ruby regular expressions, used in an object-oriented fashion, as you were supposed to work with in Recitation 13. Suppose you have input data about authors, titles of books, and date released (from a library webpage) in the following form:

        
        Jane Austen, "Emma" (Aug 1994)
        Jane Austen, "Mansfield Park" (Jun 1994)
        Charles Dickens, "Doctor Marigold" (Aug 1998)
        Charles Dickens, "George Silverman's Explanation" (Feb 1997)
        William Faulkner, "Light in August" (Aug 2005)
        Ernest Hemingway, "A Farewell to Arms" (Aug 2005)
        
      We want to rewrite these in the following form. This assumes that the book title is always enclosed in quotes, that the author's first and last names are separated by a blank and followed by an comma (with no middle name or initial), and that the date is enclosed in parentheses

        
        Emma by Austen, Jane. Released: Aug 1994.
        Mansfield Park by Austen, Jane. Released: Jun 1994.
        Doctor Marigold by Dickens, Charles. Released: Aug 1998.
        George Silverman's Explanation by Dickens, Charles. Released: Feb 1997.
        Sir Nigel by Faulkner, William. Released: Aug 2005.
        A Farewell to Arms by Hemingway, Ernest. Released: Aug 2005.
        

      Write a Ruby program using Ruby code and regular expressions to do this translation. If you have forgotten various items, just fake it as best you can, but you are not supposed to be using Perl for this problem. If you didn't do Recitation 13 the way I suggested but used some method of your own, then you should mention this.