Académique Documents
Professionnel Documents
Culture Documents
20
Answer :
The referencing environment of a statement is the collection of all variables that are visible in the
statement. The referencing environment of a statement in a static-scoped language is the variables
declared in its local scope plus the collection of all variables of its ancestor scopes that are visible.
Answer :
The static ancestors of a subprogram sub() are all the procedures in the program within which the
procedure sub() is defined, i.e., the definition of the procedure sub() is nested. The definition of a
procedure may be directly nested within only one procedure, called its static parent procedure.
However, this static parent procedure may itself be nested within another procedure, and so on up to
the main() program. All these procedures are considered to be static ancestors of the procedure sub().
Simply put, the static ancestors are those that strictly contain the subprogram in question.
The dynamic ancestors of a subprogram sub() are all the procedures called before sub() during the
execution of a program, that have not yet finished executing. These are the procedures that are waiting
for procedure sub() to finish executing before they can terminate. Simply put, dynamic ancestors are
those that are called to reach the subprogram in question.
Answer :
Such vari-ables are typically stack dynamic, so their storage is allocated when the section is entered and
deallocated when the section is exited
Answer :
“let” introduces a new variable scope, and allows you to bind variables to values for that scope. It is
often read as “let x be [value] in …”
20. What is the difference between the names defined in an ML let construct from the variables declared
in a C block?
Problem Set no 7 – 12
7. Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is
displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1 ?
Answer :
var x;
function sub1() {
function sub2() {
var x;
x = 10;
sub1();
x = 5;
sub2();
Answer:
Answer :
var x, y, z;
function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
...
...
}
function sub3() {
var a, x, w;
...
List all 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.
Answer:
Answer :
x = 1;
y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
...
def sub2():
global x;
a = 13;
x = 15;
w = 17;
...
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
...
...
List all the variables, along with the program units where they are
declared, that are visible in the bodies of sub1, sub2, and sub3, assumingstatic scoping is used.
Answer:
Answer :
void fun(void) {
int a, b, c; /* definition 1 */
...
while (. . .) {
int b, c, d; /*definition 2 */
...1
while (. . .) {
int c, d, e; /* definition 3 */
...2
...3
...4
For each of the four marked points in this function, list each visible variable,
along with the number of the definition statement that defines it.
Answer:
Answer :
void main() {
int a, b, c;
...
void fun1(void) {
int b, c, d;
...
}
void fun2(void) {
int c, d, e;
...
void fun3(void) {
int d, e, f;
...
Given the following calling sequences and assuming that dynamic scoping
is used, what variables are visible during execution of the last function
called? Include with each visible variable the name of the function in
Answer:
Answer:
Answer:
var a=main;var c,d,e=fun2; var b =fun1; var f= fun3
Answer:
// main program
var x, y, z;
Answer :
function sub1() {
var a, y, z;
...
function sub2() {
var a, b, z;
...
function sub3() {
var a, x, w;
...
Given the following calling sequences and assuming that dynamic scoping
is used, what variables are visible during execution of the last subprogram
activated? Include with each visible variable the name of the unit
where it is declared.
a x w in sub3, y z in sub1.
a y z in sub1; x w in sub3;