Question
Consider the program given below, in a block-structured pseudo-language with lexical scoping and nesting of procedures permitted.
Program main;
Var ...
Procedure A1;
Var ...
Call A2;
End A1
Procedure A2;
Var ...
Procedure A21;
Var ...
Call A1;
End A21
Call A21;
End A2
Call A1;
End main.
Consider the calling chain: Main -> A1 -> A2 -> A21 -> A1
The correct set of activation records along with their access links is given by
Official answer and solution
- Official answer
- D
- From
- Official GATE 2012 answer key
Solution
Trace activation records and static access links under lexical scoping for nested procedures.
-
In block-structured languages with lexical scoping, an access link (static link) always points to the activation record of the procedure in which the called procedure is lexically declared.
-
Lexical nesting hierarchy:
mainis the global root.A1is declared insidemain. Its static enclosing scope ismain. Every activation record forA1must have its access link pointing tomain.A2is declared insidemain. Its access link must point tomain.A21is declared insideA2. Its access link must point toA2.
- Tracing call chain
Main -> A1 -> A2 -> A21 -> A1:
- Bottom of stack:
Main. - Next:
A1(access linkMain). - Next:
A2(access linkMain). - Next:
A21(access linkA2). - Top of stack: second activation of
A1. BecauseA1is lexically enclosed bymain, its access link points directly toMain(NOT toA21orA2).
-
The frame pointer points to the most recent activation record (top
A1). -
Diagram (D) correctly depicts this exact set of 5 activation records and their access links.
D
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2012 Computer Science and Information Technology (CS)
- Question
- 36 of 65, printed as CS Q.36
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.