CSC376 · 2081 · 10 marks
What are the significances of intermediate code? Differentiate between DAG and Syntax tree. Represent the instruction A = B + C - D * E + G using quadruple and triple.
What are the significances of intermediate code? Differentiate between DAG and Syntax tree. Represent the instruction A = B + C - D * E + G using quadruple and triple.[10]
Intermediate Code, DAG vs Syntax Tree, and Quadruple/Triple Representation
Given Data (Extracted)
- Task 1: Significances of intermediate code (conceptual)
- Task 2: Differentiate DAG vs Syntax tree (conceptual)
- Task 3: Expression to represent using quadruple and triple: $$A = B + C - D * E + G$$
Operator precedence assumption: * binds tighter than +/-; + and - are left-associative and equal precedence, evaluated left to right. So the expression parses as:
$$A = ((B + C) - (D * E)) + G$$
1. Significances of Intermediate Code (3 marks)
Intermediate code (IC) is a machine-independent representation produced between the analysis (front end) and synthesis (back end) phases of a compiler.
| Significance | Explanation |
|---|---|
| Machine Independence | IC is not tied to any target CPU, keeping the front end portable. |
| Retargetability | One front end can serve many target machines by swapping only the back-end code generator. |
| Enables Optimization | Machine-independent optimizations (common subexpression elimination, constant folding, dead-code removal) are applied conveniently on IC. |
| Front-end Reuse for Many Languages | Several source-language front ends can share a single back end if they emit the same IC. |
| Modularity / Simpler Compiler Design | Clean separation of phases eases development, debugging, and maintenance. |
| Aids Error/Type Checking | Type mismatches and semantic issues can be handled while generating IC. |
2. Difference Between DAG and Syntax Tree (3 marks)
| Feature | Syntax Tree | DAG (Directed Acyclic Graph) |
|---|---|---|
| Definition | Tree with operators at internal nodes and operands at leaves. | Graph like a syntax tree but with common subexpressions shared as one node. |
| Common subexpressions | Repeated as separate subtrees. | Shared using a single node. |
| Node parents | Each node has exactly one parent. | A node may have multiple parents. |
| Space usage | More (duplication). | Less (sharing). |
| Redundancy detection | Does not expose redundant computation. | Explicitly reveals repeated computations. |
| Typical use | Semantic analysis, syntax-directed translation. | Code optimization (common subexpression elimination). |
Example: A + A * B
Syntax Tree (A duplicated):
+
/ \
A *
/ \
A B
DAG (A shared):
+
/ \
| *
\ / \
A B
3. Quadruples and Triples for A = B + C - D * E + G (4 marks)
Three-Address Code
$$ \begin{aligned} t_1 &= B + C \ t_2 &= D * E \ t_3 &= t_1 - t_2 \ t_4 &= t_3 + G \ A &= t_4 \end{aligned} $$
Quadruple Representation: (op, arg1, arg2, result)
| Index | op | arg1 | arg2 | result |
|---|---|---|---|---|
| (0) | + | B | C | $t_1$ |
| (1) | * | D | E | $t_2$ |
| (2) | - | $t_1$ | $t_2$ | $t_3$ |
| (3) | + | $t_3$ | G | $t_4$ |
| (4) | = | $t_4$ | - | A |
Triple Representation: (op, arg1, arg2) (result implied by position)
| Index | op | arg1 | arg2 |
|---|---|---|---|
| (0) | + | B | C |
| (1) | * | D | E |
| (2) | - | (0) | (1) |
| (3) | + | (2) | G |
| (4) | = | A | (3) |
Quadruple vs Triple
| Quadruple | Triple | |
|---|---|---|
| Result | Explicit named temporary | Implicit (position index) |
| Fields | 4 | 3 |
| Instruction reordering | Easy | Hard (position references break) |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.