1 Lexical/Dynamic Scoping
int var = 10;
procedure set_var(int val):
var = val;
int var = 2;
(a) Suppose the procedure main() uses static (lexical) scoping. What will
it print, and why?
(b) Suppose the procedure main() uses dynamic scoping. What will it
print, and why?
2 Lexical Scoping
This problem uses the following procedure. This procedure uses static (lexical) scoping.
procedure proc1(int i):
b = a + 1;
procedure recursion(int k):
b = b – 1;
if (b 1):
recursion(k * b);
a = k;
b = b + i;
a = 1;
(a) What does this procedure print? (Give the output of procedure main().)
(b) Rewrite the procedure main() where each variable (argument variables, declared variables, procedure names) is renamed by their (level,
(c) How does procedure proc1 find variable a in instruction b = a + 1;?
Show the RISC instructions corresponding to the high-level instruction b = a + 1; in this procedure. You should use the same ILOC
instruction format as used in class.
(d) Show the stack frames at the beginning of procedure proc1. Label
each frame with its procedures name, and make sure you include the
local variables and their values. Show all access links and control links
between the stack frames, and the frame pointer (FP), by drawing
arrows. You should use the frame layout in the figure below.
Figure 1: Figure for the frame layout