2081 2

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]

Compiler Design and ConstructionChapter 4: Intermediate Code Generation, Code Generation and OptimizationIntermediate Code GeneratorNumerical: quadruples and triples

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.

SignificanceExplanation
Machine IndependenceIC is not tied to any target CPU, keeping the front end portable.
RetargetabilityOne front end can serve many target machines by swapping only the back-end code generator.
Enables OptimizationMachine-independent optimizations (common subexpression elimination, constant folding, dead-code removal) are applied conveniently on IC.
Front-end Reuse for Many LanguagesSeveral source-language front ends can share a single back end if they emit the same IC.
Modularity / Simpler Compiler DesignClean separation of phases eases development, debugging, and maintenance.
Aids Error/Type CheckingType mismatches and semantic issues can be handled while generating IC.

2. Difference Between DAG and Syntax Tree (3 marks)

FeatureSyntax TreeDAG (Directed Acyclic Graph)
DefinitionTree with operators at internal nodes and operands at leaves.Graph like a syntax tree but with common subexpressions shared as one node.
Common subexpressionsRepeated as separate subtrees.Shared using a single node.
Node parentsEach node has exactly one parent.A node may have multiple parents.
Space usageMore (duplication).Less (sharing).
Redundancy detectionDoes not expose redundant computation.Explicitly reveals repeated computations.
Typical useSemantic 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)

Indexoparg1arg2result
(0)+BC$t_1$
(1)*DE$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)

Indexoparg1arg2
(0)+BC
(1)*DE
(2)-(0)(1)
(3)+(2)G
(4)=A(3)

Quadruple vs Triple

QuadrupleTriple
ResultExplicit named temporaryImplicit (position index)
Fields43
Instruction reorderingEasyHard (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.