Mid-Term Exam 2 - SUNY

advertisement
CSE-307 Principles of Programming Languages
2nd Mid-Term Exam
November 3, 2011
Duration: 1 hour 15 minutes
Maximum Score: 25
Instructions
•
•
•
•
WRITE YOUR NAME and USB ID NUMBER legibly and sign in the space below.
This exam has five questions over eight numbered pages (including this one).
Write your answer for each question in the space provided.
Please Note: For Python programming questions, you may define and use helper functions, or use library functions, unless explicitly prohibited. If you use library functions,
the library functions must exist, and use of the functions must exactly match their definitions. Vague references to library functions may not get credit.
• CLOSED NOTES, CLOSED BOOK; ALL PORTABLE ELECTRONIC DEVICES OFF.
USB ID:
Name:
Signature:
Question Max.
1.
6
2.
5
3.
5
4.
5
5.
4
Total:
25
1
Score
1. [6 points] A hailstone sequence is constructed from positive integers as follows. If a
number n in the sequence is even, the next number in the sequence is n/2; if the number
is odd, the next number is 3n + 1. The Collatz conjecture says that every hailstone
sequence eventually reaches 1.
For instance, the hailstone sequence starting at 10 and ending at 1 is: 10, 5, 16, 8, 4, 2, 1.
In this question, you are asked to write a Python function hailstone that, given a
positive integer n, returns the number of elements in the hailstone sequence starting at
n and ending at 1. For instance, hailstone(10) should return 7.
(a) [3 points] Write the definition of hailstone in Python without loops, using only recursion.
(b) [3 points] Write the definition of hailstone in Python without using recursion.
2
2. [5 points] Consider the problem of evaluating arithmetic expressions given by the following
grammar:
E
E
E
E
→
→
→
→
E+E
E*E
(E)
intconst
(1)
(2)
(3)
(4)
(5)
(a) [3 points] How will you represent such arithmetic expressions in Python?
(b) [2 points] How will you define a function eval expr in Python that takes an arithmetic
expression (in your chosen representation from part(a)) and returns its value?
[You may continue your answer in the following page, if necessary]
3
[Use this page to answer Q 2 if necessary]
4
3. [5 points] The SML representation of C [ ’s abstract syntax tree, and the specification of
its semantic functions (i.e. the interpreter) is given, for reference, as an appendix to this
exam.
This question considers the introduction of do-while loops in C [ .
(a) [2 points] How will you change the abstract syntax of C [ to add do-while loops?
(b) [3 points] How will you modify the semantic functions to add do-while loops? Which
function(s) will you modify? Precisely describe the modification.
[You may continue your answer in the following page, if necessary]
5
[Use this page to answer Q 3 if necessary]
6
4. [5 points] C/C++/Java family of languages have a unary post-increment operator “++”.
Consider introducing the operator to C [ with the following meaning: Let e be an expression; then e++ adds 1 to the value of e and stores the result in the location associated with
e. The value of e++ is same as the (unmodified) value of e. Post-increment is undefined
if e does not have a location associated with it, or if e’s value is not an integer.
For instance, if x is a variable at location 1 in store, whose current value is 10, then x++
updates location 1 in store to 11; the value of x++ is 10.
Assume that PostIncr of Expr is added as one of the alternatives in the SML datatype
representing C [ ’s abstract syntax. Fill the following with the definition of the semantic
function that handles post-increment expressions.
| eval_expr(PostIncr(e), env, store) =
7
5. [4 points] Consider the following initial environment and store:
env = [("u", Loc(1)), ("v", Loc(2)), ("w", Loc(3)),
("x", Loc(4)), ("y", Loc(5)), ("z", Loc(6))]
store=[(1,Int(10)), (2, Ref(1)), (3, Ref(2)),
(4, Int(20)), (5, Ref(4)), (6, Ref(3))]
For each of the following questions, give an assignment statement, or a sequence of
assignment statements, that, when evaluated in the above environment and store, will
lead to the given environment (env’) and store (store’), without using names “u” and
“x”, arithmetic operations, or integer constants. If no such statement (or sequence of
statements) is possible, state it and give a brief, clear reason.
(a) env’ = [("u", Loc(1)), ("v", Loc(2)), ("w", Loc(3)),
("x", Loc(4)), ("y", Loc(5)), ("z", Loc(6))]
store’ = [(1,Int(20)), (2, Ref(1)), (3, Ref(2)),
(4, Int(20)), (5, Ref(4)), (6, Ref(3))]
(b) env’ = [("u", Loc(1)), ("v", Loc(2)), ("w", Loc(3)),
("x", Loc(4)), ("y", Loc(5)), ("z", Loc(6))]
store’ = [(1,Int(10)), (2, Ref(3)), (3, Ref(1)),
(4, Int(20)), (5, Ref(4)), (6, Ref(3))]
(c) env’ = [("u", Loc(1)), ("v", Loc(2)), ("w", Loc(3)),
("x", Loc(4)), ("y", Loc(5)), ("z", Loc(6))]
store’ = [(1,Int(10)), (2, Ref(1)), (3, Ref(2)),
(4, Int(20)), (5, Ref(6)), (6, Ref(5))]
(d) env’ = [("u", Loc(2)), ("v", Loc(3)), ("w", Loc(1)),
("x", Loc(4)), ("y", Loc(5)), ("z", Loc(6))]
store’ = [(1,Int(10)), (2, Ref(1)), (3, Ref(2)),
(4, Int(20)), (5, Ref(4)), (6, Ref(3))]
8
Download