Uploaded by FRS

Chapter10Assignment1 (2)

advertisement
Programming Languages (CS 320)
Chapter 10: Implementing Subprograms
Assignment 1
Submission Date:
Exercise 1
Consider the following JavaScript skeletal program:
// The main program
var x;
function sub1() {
var x;
function sub2() {
. . .
x = 2;
sub3();
. . .
}
sub2();
x = 7;
. . .
}
function sub3() {
var y, z;
x = y + z;  1
. . .
}
sub1();
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
(i)
(ii)
Draw the runtime stack with all the activation record instances when the
execution reaches position 1 in the above skeletal code. Assume static depth
of main is 0.
What is the chain offset and local offset for variable x at point 1.
Exercise 2
Show the stack with all activation record instances, including static and
dynamic chains, when execution reaches position 1 in the following skeletal
program. Assume bigsub is at level 1.
var x;
function bigsub() {
var mysum;
function a() {
var x;
function b(sum) {
var y, z;
...
c(z);
...
} // end of b
...
b(x);
...
} // end of a
function c(plums) {
... <-----------------------------1
} // end of c
var l;
...
a();
...
} // end of bigsub
bigsub();
Download