CSC376 · 2081 · 5 marks
How do you represent recursion in an activation tree? Generate the three-address code for the following instruction:
How do you represent recursion in an activation tree? Generate the three-address code for the following instruction: [5]
Given
Part 1: Conceptual question about representing recursion in an activation tree. No numeric data.
Part 2: "Generate the three-address code for the following instruction."
Critical issue: The actual instruction/expression to be translated into three-address code is NOT provided in the question text. The statement ends with "the following instruction:" but no instruction, expression, or code fragment follows.
Therefore, the specific expression required for Part 2 is missing or unreadable, and three-address code cannot be generated for an expression that was not given. An expression such as a = b * c + b * d can only be treated as an illustration, not as the question's data.
I will fully answer Part 1 (conceptual, no data needed) and, for Part 2, explain the method and demonstrate it on a clearly-labelled illustrative example (marked as assumed, since the actual instruction is missing).
Solution
Part 1: Representing Recursion in an Activation Tree
An activation tree shows the flow of control among procedure activations during a program's execution.
Properties:
- Each node = one activation (one call) of a procedure.
- The root = the activation of
main. - Node for
Pis the parent of node forQif control flows fromPintoQ(i.e.,PcallsQ). - Left-to-right ordering reflects the temporal order in which activations begin.
Recursion representation: When a procedure calls itself, the activation tree contains several distinct nodes bearing the same procedure name, arranged as a chain (or subtree) descending from the calling node. Each recursive call is a separate activation with its own activation record (own parameters, locals, return address) pushed onto the control stack.
Example: fact(n) computing $n!$
$$ \text{fact}(4) \rightarrow \text{fact}(3) \rightarrow \text{fact}(2) \rightarrow \text{fact}(1) $$
fact(4)
|
fact(3)
|
fact(2)
|
fact(1)
- Each node is a distinct activation even though the procedure name repeats.
- At any moment, the runtime stack holds exactly the activation records lying on the path from the root to the currently active node.
- On return,
fact(1)'s record is popped, control resumes infact(2), and so on.
Thus recursion = repeated nodes of the same procedure along a root-to-leaf path, each with its own activation record on the stack.
Part 2: Three-Address Code Generation
Missing data: The question says "Generate the three-address code for the following instruction:" but no instruction/expression was supplied in the given text. I cannot produce the intended answer without it. Below I state the method and demonstrate it on an assumed illustrative expression (clearly marked as not from the question).
Method (syntax-directed translation):
- $E \rightarrow id$:
E.place = id - $E \rightarrow E_1 * E_2$: emit
t = E1.place * E2.place - $E \rightarrow E_1 + E_2$: emit
t = E1.place + E2.place - $S \rightarrow id = E$: emit
id = E.place
Assumed example (illustrative only): $a = b * c + b * d$
$$ \begin{aligned} t_1 &= b * c \ t_2 &= b * d \ t_3 &= t_1 + t_2 \ a &= t_3 \end{aligned} $$
Quadruple form $(op, arg_1, arg_2, result)$:
| Op | Arg1 | Arg2 | Result |
|---|---|---|---|
* | b | c | t1 |
* | b | d | t2 |
+ | t1 | t2 | t3 |
= | t3 | - | a |
Caveat: If the actual exam instruction differs (e.g., x = (a + b) * (c + d) or a control statement), the code must be regenerated from that expression. The above b*c+b*d result is not verifiable because the source instruction was absent from the given question.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.