Uploaded by TECHNOSIS

csci316 Final Exam Home Assignment

advertisement
Final Exam Home Assignment
NAME: ____________________________
1. Given the following code with nested subs.:
function bigsub() {
function a(flag) {
function b() {
...
a(false);
...
} // end of b
...
if (flag)
b();
else c();
...
} // end of a
function c() {
function d() {
... <------------------------1
} // end of d
...
d();
...
} // end of c
...
a(true);
...
} // end of bigsub
A. Trace through the code to show the order of the sub calls. You may assume that bigsub is
at “level 1” of the nesting. Suggestion: It may be helpful to highlight the code blocks or to
draw a memory diagram (as done in class).
B. Show the stack with all activation record instances, including variables, parameters, static
and dynamic chains, and returns, when execution reaches position 1 in the skeletal
program. Your stack should clearly indicate all links; you may use “colors” to indicate
the links or use “solid/dotted” lines.
2. Given the following problem:
The values of three integer variables— a, b, and c—must be placed in the three variables
max, mid, and min, with the obvious meanings, without using arrays or user-defined
or predefined subprograms. (Just write as a single code fragment.)
A. Write two solutions to this problem using Java or C++ :
one that uses nested selections, and
one that does not.
B. Compare the time complexity of the two solutions in A.
3. The following question refers to this example from the Sebesta text:
An Attribute Grammar for Simple Assignment Statements
Syntax rule: <assign> → <var> = <expr>
Semantic rule: <expr>.expected_type ← <var>.actual_type
Syntax rule: <expr> → <var>[2] + <var>[3]
Semantic rule: <expr>.actual_type ←
if (<var>[2].actual_type = int) and
(<var>[3].actual_type = int)
then int
else real
end if
Predicate: <expr>.actual_type == <expr>.expected_type
Syntax rule: <expr> → <var>
Semantic rule: <expr>.actual_type ← <var>.actual_type
Predicate: <expr>.actual_type == <expr>.expected_type
Syntax rule: <var> → A | B | C
Semantic rule: <var>.actual_type ← look-up(<var>.string)
The look-up function looks up a given variable name in the symbol table and returns the
variable’s type.
A. Rewrite the attribute grammar rules (Semantic and Predicate Rules) so that:
data types cannot be mixed in expressions, but
assignment statements need not have the same types on both sides of the assignment
operator.
B. Show the “decorations” (semantics) of the tree generated for A = B + C for
int A, int B, double C, using your answer from part A.
C Show the “decorations” (semantics) of the tree generated for A = B + C for
int A, double B, double C, using your answer from part A.
Download