(12 points) 14. Consider the following program, written in JAVASCRIPT-LIKE synta
ID: 3710369 • Letter: #
Question
(12 points) 14. Consider the following program, written in JAVASCRIPT-LIKE synta:: /main program var x,Y, 2 function subl O var a, Y. 2 function sub2 0 var a, b, z; function sub3 0 var a, x, W Part1) 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. main calls subl; subl calls sub2; sub2 calls sub3. b. main calls subl; subl calls sub3 c. main calls sub2; sub2 calls sub3; sub3 calls subl d. main calls sub3; sub3 calls subl e. main calls subl; subl calls sub3; sub3 calls sub2. f. main calls sub3; sub3 calls sub2; sub2 calls subl Part2) Repeat 'Part1' with a static scoping.Explanation / Answer
//********Please find below answer and explanation below****
************Part 1) Dynamic scoping**********
In dynamic scoping variables has a global stack of bindings and the occurrence of a variable is searched in the most recent binding.
a)
Answer: a(sub2), x(Global/Main),w(Global/Main)
Explanation: Stack of excution follows
x,y,z (Global/Main)-> a,y,z (sub1)->a,b,z(sub2)->a,x,w(sub3)
In dynamic scoping it traverses scopes of execution according to function call sequence
b)Answer: a (sub3), x(Global/Main),w(Sub3)
Explanation as a)
x,y,z(Global)->a,y,z(sub1)->a,x,w(sub3)
c)Answer: a(sub1), y(Global/Main), z(sub2)
Explanation:
Explanation: Here z is from unit sub2 as z occured in most recent binding in sub2() as per execution of function stack below
Stack of execution follows:
x,y,z(Global/Main)->a,b,z(sub2)->a,x,w(sub3)->a,y,z(sub1)
d) Answer: a(sub1), y(Global/Main), z(Global)
x,y,z(Global/Main)->a,x,w(sub3)->a,y,z(sub1)
Exaplanation as part c)
e)Answer: a(sub2), b(sub2), z(sub1)
Explanation: Here z is from unit sub1 as z occured in most recent binding in sub1() as per execution of function stack below
x,y,z(Global/Main) -> a,y,z(sub1)->a,x,w(sub3)->a,b,z(sub2)
f) a(sub1), y(Global/Main), z(sub2)
x,y,z(Global/Main)->a,x,w(sub3)->a,b,z(sub2)->a,y,z(sub1)
Exaplanation as part c)
**********Part 2) Global scoping*********
In static scoping the compiler first searches in the current block, then in the surrounding blocks successively and finally in the global variables.
a) a(sub1), x(Global/Main), w(sub3)
a comes from unit sub1 as it traverse from recent call stack to oldest call stack to find variable
similary x and z are declared globaly so x and z will come from line var x,y,z which is global
Execution stack
x,y,z (Global/Main)-> a,y,z (sub1)->a,b,z(sub2)->a,x,w(sub3)
b)Answer: a (sub3), x(Global/Main),w(Sub3)
Explanation as a)
x,y,z(Global)->a,y,z(sub1)->a,x,w(sub3)
c)Answer: a(sub1), y(Global/Main), z(sub2)
Explanation:
Explanation: Here z is from global as z occured in most most oldest binding globally in line var x,y,z as per execution of function stack below
Stack of execution follows:
x,y,z(Global/Main)->a,b,z(sub2)->a,x,w(sub3)->a,y,z(sub1)
d) Answer: a(sub1), y(Global/Main), z(Global)
x,y,z(Global/Main)->a,x,w(sub3)->a,y,z(sub1)
Exaplanation as part c)
e)Answer: a(sub2), b(sub2), z(Global)
Explanation: Here z is from unit sub1 as z occured in most recent binding in sub1() as per execution of function stack below
x,y,z(Global/Main) -> a,y,z(sub1)->a,x,w(sub3)->a,b,z(sub2)
f) a(sub1), y(Global/Main), z(Global)
Explanation: Here z is from global as z occured in most most oldest binding globally in line var x,y,z as per execution of function stack below
x,y,z(Global/Main)->a,x,w(sub3)->a,b,z(sub2)->a,y,z(sub1)
#*******Please do let me know if you have any doubts*****
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.