CS 3723-001 Programming Languages
Spring 2001 -- Final Exam
Selected Answers

Just Problems 1, 2, 3, and 4 right now. I'll post answers to the remaining ones later.

  1. Consider the following Postscript program: Problem 1
    %!PS-Adobe-2.0
    /r 100 def
    
    /wedge {
      newpath
       0 0 moveto
       r -15 sin r mul lineto
       r 0 15 sin r mul  -90 90 arc
       closepath
    } def 
    
    /pie {
      1 1 12 {
        12 div setgray 
        gsave wedge 
          gsave fill grestore 
          0 setgray stroke 
        grestore 
        30 rotate 
      } for
    } def
    
    3 setlinewidth
    pie 
    showpage
    
     

     

    This program prints the picture shown on the right centered at the (0, 0) coordinate of the page, so that 3/4 of the picture is off the page.

    1. Explain what the function wedge does. (The Postscript function arc has the following definition:
          x y r ang1 ang2 arc
         % append counter-clockwise arc, center (x,y), radius r,
         % from angle ang1 to ang2; stack empty afterward.)
      
      The program was adapted from a program in the "Blue" book (page 135). The book's program used a much trickier and more sophisticated wedge function, so I made it more basic, less tricky. The book also drew the picture first at size 1 pt (1/72 inch) and then scaled it up a great deal.

      So wdege here draws a line from (0,0) to (r, r*sin(-15)) (the bottom line of the wedge). Then it draws a half-circle with center (r,0) and radius r*sin(15), from -90 degrees to 90 degrees. This finishes the curved part. Finally closepath finishes up the upper line.

    2. What does the function pie do? In particular, explain how pie creates 12 wedges. (Describe how the for loop works.)

      The for loop: 1 1 12 { }for just repeats what is in {} starting with a loop variable equal 1, incrementing by 1 each time, until it gets to 12. This loop provides the variable on the stack at the start of the {} part.

    3. Explain how pie manages to make the gray level go from near black to white around the circle. (Recall that 0 means black.)

      Using the loop variable on the stack (successively 1, 2, 3, ..., 11, 12), the code "12 div setgray" produces grey levels from 1/12 up to 12/12 = 1, that is, from nearly black to white.

    4. Without changing the definitions of pie or wedge, give code at the end that will print the same picture centered exactly at the center of an 8-1/2-by-11 inch sheet of paper, three times as big, and rotated counter-clockwise by 15 degrees.

      The following code works:

      
           8.5 72 mul 2 div 11 72 mul 2 div translate  % 306 391 translate
           3 3 scale
           15 rotate
       
      You can rearrange the three lines to some extent, but you would need a different translation if you tried to do it after the scale. (Actually, three times as big is too big to fit on the page.)


  2. Consider the following grammar:
    
        P  ---->  E #
        E  ---->  E + E
        E  ---->  E * E
        E  ---->  ( E )  |  a  |  b  |  c
    
    1. Which are the terminal symbols? Which are the non-terminal symbols?

      Terminals are # + * ( ) a b c
      Non-terminals are P E

    2. Give the parse tree for the sentence: a * ( b + c ) #.

    3. Show that the above grammar is ambiguous. (Use any method.)

      The sentence in part a has a unique parse tree, and thus does not demonstrate the ambiguity of this grammar. However the grammar is ambiguous. The easiest way to prove this is to display sentences that have more than one parse tree (or more than one leftmost derviation). Examples include:

      
         a + b * c (ambiguity of operator precedence)
         a + b + c (ambiguity of operator accociativity)
      


  3. Consider the diagram for an automaton that I will write on the board (here "d" stands for "digit"):

    1. Is this an NFA or a DFA? How do you know?

      This is an NFA because there are two transitions labeled with a ".".

    2. Use the algorithm in class to convert this to a DFA accepting the same language.

    3. By any method, even ordinary English, describe the language accepted by this automaton. (But be careful.)

      As a regular expressions, we can write: d+\.d+ | d*\.d+ | d+\.d*
      In English, any sequence of digits, followed by a decimal point, followed by any sequence of digits, except that there must be at least one digit either before or after the decimal point.


  4. This is a question about translating arithmetic expressions into quadruples.

    1. What special quantity is returned by many of the functions carrying out the translation?

      In each case, the function returns an integer which gives a memory address where the result of the subexpression recognized by the function will be kept at run time.

    2. How is a constant like 47 handled? (What quadruple(s) is(are) generated? Where is(are) it(they) generated? What is returned after it(they) is(are) generated?)

      Only a single LIT quad is generated, which puts the constant 47 into a temporary memory location (generated for this purpose). The memory location is returned as an int.

    3. How is a single variable name like rate handled. (Similar questions to the above.)

      Just look up rate in the symbol table, insert it if it's not there, and return its memory location. (No quads generated.) (Note: the above said "translating arithmetic expressions". If the variable is on the right side of an assignment, then you will use the memory location returned to write and ASG quad.)


Points for each problem: 1-30, 2-20, 3-20, 4-20, 5-20, 6-30, 7-20.