CS4250: Programming Languages
HW#2: Eric Buckley - 18148800
5.6) Consider the following JavaScript skeletal program:
// The main program
var x; //main.x
function sub1() {
var x; //sub1.x
function sub2 {
...
}
...
}
function sub3{
...
}
Assume that execution runs main calls sub1 calls sub2 calls
sub3.
a) Assuming static scoping, which declaration is the correct
one for a reference to x?
i.
sub1: sub1.x since it is local to the routine.
ii.
sub2: sub1.x since sub2 is declared within sub1, sub1 is the next available
scope.
iii.
sub3: main.x since sub3 is declared directly within main and has no visibility
to other locals.
b) Assuming dynamic scoping, which declaration is the
correct one for a reference to x?
i.
sub1: sub1.x since it is local to the routine
ii.
sub2: sub1.x since the next available scope is the calling program
iii.
sub3: sub1.x since sub2 will pass this reference in its call to sub 3.
5.7) Assume the following JavaScript program was interpreted
using static-scoping rules.
var x; //main.x
function sub1() {
document.write(“x = “
+ x + “”);
}
Function sub2 {
var x; //sub2.x
x = 10;
sub1();
}
x = 5;
sub2();
What value of x is displayed in function sub1?
x = 5
since x refers to main.x
What value of x is displayed if dynamic scoping rules?
x = 10
since x refers to the calling program (sub2.x)
5.8) Consider the following JavaScript program:
var x, y, z;
function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
...
}
...
}
function sub3() {
var a, x, w;
...
}
List the variables, along with the program units where they
are declared, that are visible in the bodies of sub1, sub2, and sub3 assuming
static scoping is used
sub1:
sub1.a, main.x, sub1.y, sub1.z
sub2:
sub2.a, sub2.b, main.x, sub1.y, sub2.z
sub3:
sub3.a, sub3.w, sub3.x, main.y, main.z
5.11) Consider the following skeletal C program:
void fun1(void); /* prototype */
void fun2(void); /* prototype */
void fun3(void); /* prototype */
void main() {
int a, b, c;
...
}
void fun1(foid) {
int b, c, d;
...
}
void fun2(foid) {
int c, d, e;
...
}
void fun3(foid) {
int d, e, f;
...
}
Give the variables visible under dynamic scoping during the
last function call if the order of execution is:
a) main
calls fun1 calls fun2 calls fun3
The following table gives the variables defined at each call
level
code
|
a
|
b
|
c
|
d
|
e
|
f
|
main
|
main.a
|
main.b
|
main.c
|
|
|
|
fun1
|
|
fun1.b
|
fun1.c
|
fun1.d
|
|
|
fun2
|
|
|
fun2.c
|
fun2.d
|
fun2.e
|
|
fun3
|
|
|
|
fun3.d
|
fun3.e
|
fun3.f
|
Thus, at fun3, the variables available are main.a, fun1.b,
fun2.c, fun3.d, fun3.e, and fun3.f
5.12) Consider the following program, written in
JavaScript-like syntax:
// main program
var x, y, z;
function sub1() {
var a, y, z;
...
}
function sub2() {
var a, b, z;
...
}
function sub3() {
var a, x, w;
...
}
Given the following call sequence and assuming that dynamic
scoping is used, what variables are visible during execution of the last
subprogram activated?
c) main
calls sub2 calls sub3 calls sub1
The following table gives the variables defined at each call
level
code
|
a
|
b
|
w
|
x
|
y
|
z
|
main
|
|
|
|
main.x
|
main.y
|
main.z
|
sub2
|
sub2.a
|
sub2.b
|
|
|
|
sub2.z
|
sub3
|
sub3.a
|
|
sub3.w
|
sub3.x
|
|
|
sub1
|
sub1.a
|
|
|
|
sub1.y
|
sub1.z
|
Thus, at sub1, the variables available are sub1.a, sub2.b,
sub3.w, sub3.x, sub1.y, and sub1.z
No comments:
Post a Comment