Describe how a stack is used to implement procedures and functions

Languages like Lisp, Prolog and Python
March 21, 2023
Explain how it is possible to “leak memory” using a reference counting garbage collector
March 21, 2023

Describe how a stack is used to implement procedures and functions

2007 Paper 5 Question 6
Compiler Construction
(a) Describe how a stack is used to implement procedures and functions.
[6 marks]
(b) Suppose a language allows the creation of pointers. How does this complicate
the use of stacks as described in part (a)? [2 marks]
(c) How does the Java language deal with the problem described in part (b)?
[2 marks]
(d) Consider the following ML-like program containing the function g that returns
a function as a result.
let a = 17 in
let g b = (let h c = a + b + c in h) in
let f1 = g 21 in
let f2 = g 33 in
let v = f1(3) + f2(57) in


Explain carefully how such a program can be compiled. In particular, pay
special attention to how the code for the body of the function h can access the
values of a, b, and c. [10 marks