CS 3723-001 Programming Languages
Spring 2001 -- Final Exam
Wednesday, 8 May 2001, 8:00 am - 10:15 am

Directions: Use your own paper for answers. When you are done you may keep this exam sheet and should pick up an answer sheet.

  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.)
      
    2. What does the function pie do? In particular, explain how pie creates 12 wedges. (Describe how the for loop works.)

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

    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.


  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?

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

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


  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?

    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.)


  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?

    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?)

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


  5. Using the reference sheet about the Quadruple Machine to help you, translate the following simple program by hand into quadruples:
    
       n = 1;
       WHILE (10 - n) { 
          WRITE(n);
          WRITEC(32); /* a blank */
          n = n + 1;
       }
       WRITEC(10); /* a newline */
    


  6. Consider the grammar rule for a WHILE statment:
    
       whilestmt  ---->  "WHILE" "(" expr ")" stmt 
    
    1. Give the code for the function whilestmt in your parser. (Just give this code in rough form, using whatever kind of helpful pseudocode you need. You can employ the notation that you actually used in your own parser if you wish.)

    2. Show in outline form how to translate a general WHILE statement into quadruples, as you did (may have done) for the project for this course. This amounts to additions to the code for the whilestmts function in your parser. You should focus particularly on how you keep track of the numbers of key quadruples, and how you carry out the backpatching. You can assume a function nextQuad() that gives the quadruple number of the next quadruple to be generated. (Again, you can use any convenient pseudocode, and helpful additional functions, as needed.)


  7. This is a question about Prolog. Here is a short Prolog program, in the file rulers.pl:
    
    reigns(henry7, 1485, 1509).
    reigns(henry8, 1509, 1547).
    reigns(edward4, 1547, 1553).
    reigns(jane_grey, 1553, 1553).
    reigns(mary1, 1553, 1558).
    reigns(elizabeth1, 1558, 1603).
    reigns(james1, 1603, 1625).
    reigns(charles1, 1625, 1642).
    reigns(oliver_cromwell, 1642, 1658).
    reigns(richard_cromwell, 1658, 1659).
    reigns(charles2, 1660, 1685).
    reigns(james2, 1685, 1688).
    reigns(mary2, 1688, 1694).
    reigns(william3, 1688, 1702).
    reigns(anne, 1702, 1714).
    reigns(george1, 1714, 1724).
    reigns(george2, 1727, 1760).
    reigns(george3, 1760, 1811).
    reigns(george4, 1811, 1830).
    reigns(william4, 1830, 1837).
    reigns(victoria, 1837, 1901).
    reigns(edward7, 1901, 1910).
    reigns(george5, 1910, 1936).
    reigns(edward8, 1936, 1936).
    reigns(george6, 1936, 1952).
    reigns(elizabeth2, 1952, 2001).
    house_of(henry7, tudor).
    house_of(henry8, tudor).
    house_of(edward4, tudor).
    house_of(jane_grey, tudor).
    house_of(mary1, tudor).
    house_of(elizabeth1, tudor).
    house_of(james1, stuart).
    house_of(charles1, stuart).
    house_of(oliver_cromwell, commonwealth).
    house_of(richard_cromwell, commonwealth).
    house_of(charles2, stuart).
    house_of(james2, stuart).
    house_of(mary2, orange).
    house_of(william3, orange).
    house_of(anne, stuart).
    house_of(george1, brunswick).
    house_of(george2, brunswick).
    house_of(george3, brunswick).
    house_of(george4, brunswick).
    house_of(william4, brunswick).
    house_of(victoria, brunswick).
    house_of(edward7, saxe_coburg_gotha).
    house_of(george5, windsor).
    house_of(edward8, windsor).
    house_of(george6, windsor).
    house_of(elizabeth2, windsor).
    ruler(N, Y) :- reigns(N, A, B), Y >= A, Y =< B.     /* =< instead of <= */
    
    1. Which are the facts above and which are the rules?

    2. Look at the following session (boldface and all semicolons are user input -- a semicolon just means to try again):
      ten42% pl
      ?- consult(rulers).
      rulers compiled, 0.00 sec, 5,164 bytes.
      Yes
      ?- ruler(X, 1560).
      X = elizabeth1 ;
      No
      ?- ruler(X, 1688).
      X = james2 ;
      X = mary2 ;
      X = william3 ;
      No
      ?- house_of(X, stuart).
      X = james1 ;
      X = charles1 ;
      X = charles2 ;
      X = james2 ;
      X = anne ;
      No
      
      Explain why there are the three answers to ruler(X, 1688).

    3. Write a rule years(H, A, B) where H is a House and A and B are the years of someone's reign who belongs to the house H.

    4. With the new rule, what would be the result of typing years(orange, A, B).?

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