GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Resolve lexical bindings through nested scopes

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

How these are solved

Resolve each variable use through lexical nesting, then trace assignments or report bindings. Programs and requested output/table differ; dynamic scope questions are separate.

2 marks · MCQ
Report issue

Lexical scope across sibling procedures

Runtime Environments & Parameter Passing

Question 4.8, GATE 1997: A declares x, y. Nested B declares x, z and contains S1; sibling C declares i and contains S2. Which variables are visible?
Question 4.8, GATE 1997: A declares x, y. Nested B declares x, z and contains S1; sibling C declares i and contains S2. Which variables are visible?
Tap to enlarge
Your answer
Answer & solution

Answer: C: S1 sees B.x, z, A.y; S2 sees A.x, A.y, C.i (no official key was published)

Lexical lookup searches the enclosing declaration chain. At S1, the local x in B shadows A’s x; B’s z and A’s y are accessible. At S2, procedure C is a sibling of B, not nested inside B, so B’s x and z are inaccessible. C sees its own i and the x, y declared in A. This is option C. The dynamically most recent caller is irrelevant in this Pascal-like lexical-scope question.

Original paper, page 4

Descriptive
Report issue

Resolve Pascal record scopes

Runtime Environments & Parameter Passing

Question 10(a), GATE 1995: procedure T(U,V,X,Y:integer); var A:record A,B:integer end; B:record B,A:integer end; begin with A do begin A:=4; B:=V end; with B do begin A:=X; B:=Y end end. Identify the declarations bound to each A and B occurrence.
Tap to enlarge
Answer & solution

Answer: First with assigns A.A=4 and A.B=V; second assigns B.A=X and B.B=Y. (no official key was published)

There are six distinct declarations with these spellings: two outer record variables, outer A and outer B, and two integer fields inside each record. A field name belongs to its record’s field namespace; declaring field A does not replace the surrounding record variable A.

In “with A do”, the selector A is resolved before entering the with environment, so it denotes the outer record variable A. Within that body, unqualified field identifiers take precedence: the assignment A:=4 means outer-A.A:=4, while B:=V means outer-A.B:=V. V remains the procedure parameter.

That with scope ends at its end. Therefore the selector in the following “with B do” denotes the outer record B, not the field B from the preceding block. Within the second body, A:=X means outer-B.A:=X and B:=Y means outer-B.B:=Y. X and Y are parameters. The field declarations in the first record define A.A and A.B; those in the second define B.B and B.A. Parameter U is unused in the displayed the paper.

Original paper, page 6

Descriptive
Report issue

Lexical scope in sibling procedures

Runtime Environments & Parameter Passing

Question 22(i), 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 static scoping?
Question 22(i), 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 static scoping?
Question 22(i), 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 static scoping?
Question 22(i), 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 static scoping?
Tap to enlarge
Answer & solution

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

Static, or lexical, scoping resolves a name by the nesting of declarations in the program text, not by whichever procedure happened to call the current procedure. Procedure two has no local declaration of r. Its immediately enclosing lexical scope is main, so every reference to r in two denotes main's r. Procedure one's local r is in a sibling procedure's scope and is not visible inside two.

After main assigns r=2, its first call to two prints 2. Next main calls one, which creates a separate local r and sets that local variable to 5. The call from one to two still resolves two's r to main's unchanged variable and therefore prints 2. Returning from one removes its local activation without changing main's r. The final direct call to two again prints 2.

Thus the sequence of values is 2,2,2. The pseudocode uses write rather than writeln and requests no separators, so a literal output stream would commonly appear as 222. Formatting conventions do not change the three values.

Original paper, page 6

2 marks · MSQ
Report issue

Scope and shadowing of a global name

Runtime Environments & Parameter Passing

Question 7.3, GATE 1993: Global m is declared in PARAM. P declares another local m; Q does not. Where is the global name m directly visible?
Question 7.3, GATE 1993: Global m is declared in PARAM. P declares another local m; Q does not. Where is the global name m directly visible?
Tap to enlarge
Select all that apply
Answer & solution

Answer: C: PARAM and Q (no official key was published)

Lexical lookup in P finds its local m before reaching the enclosing declaration, so unqualified m there denotes the local variable. In Q, which has no local m, lookup reaches the global declaration in PARAM. The same global name is visible in the main block itself. Hence PARAM and Q are the regions of direct visibility, C. The global object can still be accessed indirectly through P’s reference parameters; object accessibility is distinct from whether its identifier is shadowed.

Original paper, page 6

Descriptive
Report issue

Lexical scope selects declaration context

Runtime Environments & Parameter Passing

Question 9(b.i), 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 static scope.
Question 9(b.i), 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 static scope.
Tap to enlarge
Answer & solution

Answer: a=5,b=10. (no official key was published)

Under static, or lexical, scope, a nonlocal name is resolved by the source program’s nesting structure. P is declared at global level, alongside Q, rather than inside Q. The closest enclosing declarations for the a and b used in P are therefore the global variables.

Main first assigns those global variables (1,2). Q then creates a fresh activation containing its own local a,b. Although Q calls P, Q’s locals are not lexically enclosing declarations of P. P’s assignments update the globals to (5,10), leaving Q’s locals untouched. Q returns and its activation disappears. Main then prints the global values a=5,b=10.

A useful implementation view is that P’s static link identifies the global environment, independent of which procedure called it. The existence of same-spelled variables in the caller does not change this link. Their uninitialized values are irrelevant because P never reads or writes them under static scoping.

Original paper, page 6