GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Construct activation records and scope links

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

How these are solved

Trace nested calls, allocate active frames, set dynamic link to caller and static link to lexical parent. Requested diagram or link counts differ; display-only task remains separate.

2 marks · MCQ
Report issue

Static links in nested procedure activations

Runtime Environments & Parameter Passing

Question 36, GATE 2012: In a lexically scoped block language, main declares A1 and A2. A1 calls A2. A2 declares A21 and calls A21; A21 calls A1. Main calls A1, producing the live call stack main→A1(first)→A2→A21→A1(second). Which activation-record/access-link diagram is correct? Let M, X, Y, Z, W denote these five activations respectively. Arrows below are access (static) links, and FP denotes the frame pointer.
(A) X→M, Y→X, Z→Y, W→Z; FP=W.
(B) Y→M, Z→Y, W→Z; no access link is drawn for X; FP=W.
(C) Only M,X,Y,Z are drawn: X→M, Y→M, Z→Y; W is absent and FP=X.
(D) X→M, Y→M, Z→Y, W→M; FP=W.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

An access link points to the activation of the lexically enclosing procedure, not generally to the caller. Both A1 activations and A2 are declared directly in Main, so all three point to Main. A21 is declared in A2, so its access link points to that A2 activation. The frame pointer is at the latest (bottom) A1 activation. Option D depicts precisely these static links; caller links instead would form the dynamic call chain.

Check: The second A1 was called by A21, but A1’s declaration remains inside main. Dynamic recursion changes which activation is current, not the lexical parent. Thus its static link skips both A21 and A2, while its dynamic caller link would point to A21.

Original paper, page 9 · Answer key

5 marks · Descriptive
Report issue

Draw activation stacks and static links

Runtime Environments & Parameter Passing

Question 16, GATE 1999: In a Pascal skeleton, program sort declares a,x and the sibling procedures readarray, exchange, qsort. qsort declares k,v and a nested function partition, which declares i,j. The active call sequence is sort → qsort(1,9) → qsort(1,3) → partition(1,3) → exchange(1,3). Draw the runtime stack with access links after each activation.
Tap to enlarge
Answer & solution

Answer: Both qsort frames and exchange link statically to sort; partition links to qsort(1,3). (no official key was published)

Access (static) links follow lexical nesting, whereas control (dynamic) links follow the caller. Label frames S=sort, Q1=qsort(1,9), Q2=qsort(1,3), P=partition(1,3), E=exchange(1,3). Each line below is a complete snapshot written bottom → top; the parenthesized target is the access link:

1. [S(outer/null)]
2. [S(outer/null)] [Q1(S)]
3. [S(outer/null)] [Q1(S)] [Q2(S)]
4. [S(outer/null)] [Q1(S)] [Q2(S)] [P(Q2)]
5. [S(outer/null)] [Q1(S)] [Q2(S)] [P(Q2)] [E(S)]

The final stack, top first, is:
E : static→S; dynamic→P
P : static→Q2; dynamic→Q2
Q2: static→S; dynamic→Q1
Q1: static→S; dynamic→S
S : static→outer/null; dynamic→outer/null

Recursive Q2 is not lexically declared inside Q1, so its static link must not point to Q1. Partition accesses its enclosing qsort variables such as v through Q2, then global a through Q2→S. Exchange was declared directly inside sort, so its a and x references use E→S, even though partition called it. readarray is not active and has no frame in these snapshots. Return addresses and saved registers may also occupy frames but do not change the links.

Original paper, page 8

5 marks · Descriptive
Report issue

Five activation records with nested recursion

Runtime Environments & Parameter Passing

Question 24, GATE 1997: Consider this Pascal nesting and call structure, with no calls other than those shown. Main declares X; A, nested in Main, declares Y; B, nested in A, declares Z; C, nested in B, also declares Z. Main calls A; A calls B; B calls C and, after C returns, calls A. C contains no procedure calls. At an instant there are five activation records on the runtime stack. Show its top, static and dynamic links, and local variables. Source skeleton:
program Main; var X: integer;
  procedure A; var Y: integer;
    procedure B; var Z: integer;
      procedure C; var Z: integer;
      begin {no procedure calls} end;
    begin C; A end;
  begin B end;
begin A end.
Ellipses in the source allow other non-call work; the skeleton preserves every shown declaration and call.
Question 24, GATE 1997: Consider this Pascal nesting and call structure, with no calls other than those shown. Main declares X; A, nested in Main, declares Y; B, nested in A, declares Z; C, nested in B, also declares Z. Main calls A; A calls B; B calls C and, after C returns, calls A. C contains no procedure calls. At an instant there are five activation records on the runtime stack. Show its top, static and dynamic links, and local variables. Source skeleton:
program Main; var X: integer;
  procedure A; var Y: integer;
    procedure B; var Z: integer;
      procedure C; var Z: integer;
      begin {no procedure calls} end;
    begin C; A end;
  begin B end;
begin A end.
Ellipses in the source allow other non-call work; the skeleton preserves every shown declaration and call.
Tap to enlarge
Answer & solution

Answer: Bottom to top: Main, A1, B1, A2, B2. Dynamic links follow the call chain; static links B2→A2, A2→Main, B1→A1, A1→Main. (no official key was published)

Distinguish a procedure's lexical parent from the activation that calls it. The dynamic link records the caller; the static link records the active instance of the enclosing lexical scope. Calls to A from inside B are recursive calls to a procedure declared directly inside Main, not declarations of a new A inside B.

Trace the shown calls. Main calls A1, which calls B1. B1's call to C1 creates only the fourth record, and C1 makes no further calls. C1 returns before the following call to A2, so its record has been removed. A2 is then the fourth record, and its call to B2 creates the fifth. At any matching five-record stage of this call pattern, the active chain is therefore:

Stack position   Activation   Local slot   Dynamic link   Static link
TOP              B2           B2.Z         A2             A2
                 A2           A2.Y         B1             Main
                 B1           B1.Z         A1             A1
                 A1           A1.Y         Main           Main
BOTTOM           Main         Main.X       null           null

Other ordinary record fields, such as a return address and saved registers, may exist but are not assigned values by the problem. A1.Y and A2.Y are distinct storage cells, as are B1.Z and B2.Z. No numeric values for X,Y,Z can be recovered from the ellipses. The completed C1 record, including its separate C1.Z slot, is absent.

The dynamic chain is B2→A2→B1→A1→Main. The different static chain from B2 is B2→A2→Main. In particular, A2's static link must not point to B1 merely because B1 called it. To create A2, B1 can follow its own static chain B1→A1→Main to find A's lexical parent. This is the central distinction the example tests.

Original paper, page 10