CS 3723-001 Programming Languages
Fall 2001 -- Final Exam
Tuesday, 11 December 2001, 8:00-10:15 pm

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 two Postscript programs, with the image each produces at the right: Problem 1
    
    %!PS-Adobe-2.0
    /r 200 def
    /iter 36 def
    /theta 130 def
    /star {
        r 0 moveto 
        0 1 iter { % for
          pop
          r theta cos mul r theta sin mul rlineto
          theta rotate
        } for
        stroke
    } def
    
    200 400 translate
    star
    showpage
    
     

     


    Problem 1
    
    %!PS-Adobe-2.0
    /r 200 def
    /iter 36 def
    /theta 130 def
    /star {
        r 0 moveto 
        0 1 iter { % for
          pop
          r theta cos mul r theta sin mul rlineto
          theta rotate
        } for
        stroke
    } def
    
    0 setlinejoin % joinlines at a point
    
    200 400 translate
    0 setgray
    6 setlinewidth
    gsave star grestore
    
    0.9 setgray
    4 setlinewidth
    star
    
    showpage
    
     

    1. Explain what the construct 0 1 iter { ... } for does.

    2. What is the pop inside the construct above for?

    3. The function star draws at the origin. Exactly where on the page would this figure appear.

    4. Explain what pair of numbers r theta cos mul r theta sin mul refers to, using ordinary math notation (not postscript).

    5. The second program calls star twice. Why is there a gsave ... grestore around the first call, but not the second?

    6. Explain how the second program gets the effect shown. (0 setgray sets the color to black.)


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

    2. Is this grammar ambiguous? (Yes or No)

    3. Give the parse tree for the sentence: ( a + b ) * c $.


  3. Consider the diagram for an automaton that I will write on the board.

    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. Using the reference sheet about the Quadruple Machine to help you, translate the following simple program by hand into quadruples. (All you need for an answer is the list of quadruples.)
    
         c = 1;
         a = 4*c;
         k = 3;
         t = a/(8*k+1);
         WRITE(t);
         WRITEC(10); /* a newline */
    


  5. This is a question about translating arithmetic expressions into quadruples. (It is about the portion of program that would carry out the translation that you gave in the previous problem.)

    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 a in the previous question handled. (Similar questions to the above.)


  6. This question is about how you might translate a goto statement into quadruples. Suppose (as we did in class) that our source language has a goto label statement, where label is any identifier, and it occurs just once followed by a colon in front of a statement. (One can have as many of the gotos as one likes.) So the source might look like the following, leaving out other statements, where there are statements between the gotos:
    
            goto L1;
             ...
            goto L1;
             ...
            goto L1;
             ...
            L1: /* some statement */
             ...
            goto L1;
             ...
            goto L1;
    
    1. Explain what form the quadruples will take for these statements.

    2. Explain how the translation will take place, using a single pass. (You must mention backpatching.)

  7. Consider the grammar rule for an IF-ELSE statment:
    
       ifstmt  ---->  "IF" "(" expr ")" stmt [ "ELSE" stmt ]
    
    1. Give the code for the function ifstmt in your recursive descent 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. Given a specific IF-ELSE statement, show what form the translating quadruples might take. This can include things like "quads to evaluate expr", and "quads to evaluate stmt", etc. You should focus on the jump quadruples that are needed.

    3. Show in outline form how to translate this general IF statement into quadruples, as you did (may have done) for the project for this course. This amounts to additions to the code for the ifstmt 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. You can also use the notation you used in your own code, as long as you explain it.)

  8. Describe briefly how the storage needed for the call of a function is handled in C/C++/Java at run time. You should include parameters, the return address, local variable, and storage that is created with malloc() or new().

Points for each problem: 1-30, 2-20, 3-20, 4-30, 5-20, 6-20, 7-30, 8-10; total-180.