GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 2.13

Dynamic scope through a caller

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 1999 paper

GATE 1999, Q2.13 2 marks · MSQ
Report issue

Runtime Environments & Parameter Passing

Question 2.13, GATE 1999: Global x is set to 0.25. show prints x. small has local x=0.125 and calls show. Main calls show,then small, using dynamic scope.
Question 2.13, GATE 1999: Global x is set to 0.25. show prints x. small has local x=0.125 and calls show. Main calls show,then small, using dynamic scope.
Tap to enlarge
Select all that apply
Answer & solution

Answer: C: 0.25,0.125 (no official key was published)

Dynamic lookup searches active calling environments. In the first show call, no nearer local x is active, so the global value 0.25 is printed. In the second call, small is the active caller and its local x=0.125 is the nearest binding, so show prints 0.125. Thus C. Under lexical scope, show’s definition outside small would instead resolve both calls to the global binding; the explicit dynamic-scoping premise is therefore essential.

Original paper, page 4