GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Resolve variables through the active call chain

3 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Follow active callers to resolve nonlocal variables and trace output. Different program calls and values; no static-scope or mixed reference-convention cases.

2 marks · MSQ
Report issue

Dynamic scope through a caller

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

Descriptive
Report issue

Dynamic scope follows the call chain

Runtime Environments & Parameter Passing

Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Question 22(ii), GATE 1994: main declares global integer r. Procedure two, declared directly inside main, executes write(r). Sibling procedure one declares its own local r, assigns it 5, then calls two. main assigns its r=2 and executes two; one; two. What values are printed under dynamic scoping?
Tap to enlarge
Answer & solution

Answer: 2, 5, 2 (without separators, 252). (no official key was published)

Under dynamic scoping, resolve an unbound local name by searching active callers, nearest first. Procedure two itself has no r. In the first call main→two, the closest active declaration is main's r, already set to 2, so two prints 2.

The second relevant call chain is main→one→two. Procedure one has an active local r initialized to 5. The search from two finds that declaration before reaching main, so this invocation prints 5. Procedure two merely reads the variable; it does not copy the value into main's r or assign to either declaration.

After one returns, its local r is no longer in the active call chain. The last main→two call therefore finds main's original r=2 and prints 2. The resulting sequence is 2,5,2, or 252 if ordinary write operations introduce no spaces. The key distinction is lifetime plus dynamic caller order: the same body of two can access different storage on different calls under dynamic scoping, whereas static scoping would always bind its r to main's lexical declaration. This result concerns the hypothetical dynamic-scoping interpretation requested, not the default rules of Pascal.

Original paper, page 6

Report issue

Dynamic scope selects the active caller

Runtime Environments & Parameter Passing

Question 9(b.ii), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under dynamic scope.
Question 9(b.ii), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under dynamic scope.
Tap to enlarge
Answer & solution

Answer: a=1,b=2. (no official key was published)

Dynamic scope resolves a free name by searching the active call chain from the current activation outward. During P, the chain is P→Q→main/global. P has no local a or b; the nearest active bindings are Q’s local a,b. Hence P’s assignments set Q’s variables to (5,10), rather than modifying the globals.

The globals were initialized to (1,2) by main and are still (1,2) when Q returns. Q’s locals disappear with its activation, and the final print in main uses the globals. The output is a=1,b=2.

No undefined read is caused by Q’s initially uninitialized locals: P overwrites them before they could be read, and the final print occurs after they are out of scope. Dynamic scoping must not be confused with dynamic memory allocation or call-by-reference parameter passing. It is a rule for selecting which binding an identifier denotes; the assignment operation itself is unchanged.

Original paper, page 6