4 Intermediate Code Generation Code Generation And Optimizatio

Compiler Design and Construction · Unit 4 · 16 hrs

Intermediate Code Generation, Code Generation and Optimization

Exam-focused notes for Intermediate Code Generation, Code Generation and Optimization (Compiler Design and Construction, CSC376): what the TU syllabus asks and how it has actually been tested, with 19 solved past questions from this unit.

Intermediate Code Generation, Code Generation and Optimization in short

This chapter follows a syntactically and semantically checked program the rest of the way through the compiler: into an intermediate representation, through code generation onto a target machine, and finally through the transformations that make the generated code faster and smaller.

Intermediate Code Generation, Code Generation and Optimization: notes

Intermediate Code Generator: High-level and Low-level Intermediate representation

Once a program has passed syntax and semantic analysis, the compiler does not translate straight to target code; instead it first produces an intermediate representation (IR), a form that sits between the source language and the target machine. Structurally, this places the intermediate code generator between the compiler's front end and back end: $$\text{front end} ;\to; \text{intermediate code} ;\to; \text{back end} ;\to; \text{target machine code}$$ An intermediate representation is deliberately machine independent, so the same front end can feed different back ends, but it is also deliberately close to actual machine instructions, so translating it into real target code is straightforward. Using an intermediate representation has several concrete payoffs. Without one, retargeting a compiler to a new machine would need an entirely new, full native compiler for every machine; with one, only the back end (synthesis) needs to change for a new target, while the whole front end (analysis) is reused unchanged. It also makes it far easier to apply source level code improvements, since code optimization techniques can be written once, to operate on the intermediate representation, rather than being duplicated for every possible target machine.

Intermediate representations are commonly grouped by how close they sit to the source or to the machine. A high-level intermediate representation stays close to the source program's own structure, for instance a syntax tree that still names source-level constructs directly, and is well suited to source-level analyses such as certain optimizations that are easiest to reason about in terms of the original program structure. A low-level intermediate representation instead resembles actual machine instructions closely, for instance three-address code, and is well suited to target-dependent tasks such as register allocation and instruction selection that must eventually reason in terms of a real instruction set. Many compilers use both, translating a high-level IR such as a syntax tree down into a low-level IR such as three-address code before code generation proper begins.

Syntax tree & DAG representations

A syntax tree (a compacted variant of the parse tree) is a graphical, high-level intermediate representation of an expression or statement in which every leaf represents an operand and every interior node represents an operator applied to its children. For the expression $a \times (b + c) / d$, the syntax tree has the division at the root, with its left child the multiplication of $a$ and the subtree for $b + c$, and its right child the leaf $d$:

NodeRepresentsLeft childRight child
$n_1$$b + c$$b$$c$
$n_2$$a \times n_1$$a$$n_1$
$n_3$ (root)$n_2 / d$$n_2$$d$

A directed acyclic graph (DAG) carries the same idea further: it identifies common subexpressions, so that a node computing a subexpression that occurs more than once in the source expression is given more than one parent, rather than being duplicated as a separate subtree the way a syntax tree would duplicate it. For the expression $a + a \times (b - c) + (b - c) \times d$, where $b - c$ occurs twice, the DAG is

NodeRepresentsLeft operandRight operand
$n_1$$b - c$$b$$c$
$n_2$$a \times n_1$$a$$n_1$
$n_3$$a + n_2$$a$$n_2$
$n_4$$n_1 \times d$$n_1$$d$
$n_5$ (root)$n_3 + n_4$$n_3$$n_4$

with $n_1$ referenced by both $n_2$ and $n_4$, so the subtraction $b - c$ is represented, and would be computed, only once, exactly the saving a DAG is built to expose.

Three-address code

Three-address code is a low-level, linear intermediate representation in which every instruction has at most three addresses, typically two for operands and one for the result, and uses at most one operator per instruction; a general three-address statement has the form $x = y \operatorname{op} z$, where $x$, $y$, and $z$ are names, constants, or compiler-generated temporaries, and $\mathrm{op}$ is any single operator. Because a more complex source expression, such as $((b+a)*(y-(b+a)))$, contains several operators nested inside one another, it must be broken into a sequence of instructions, introducing a fresh temporary at each step to hold an intermediate result:

t1 = b + a
t2 = y - t1
t3 = t1 * t2

This is exactly why the representation is called three-address code: outside of unary operators and copies, each instruction names at most three values, two sources and one destination, mirroring the layout of many real machine instructions and making it a natural stepping stone to target code.

Quadruples

A quadruple is a record with four fields, (op, arg1, arg2, result), used to implement three-address statements as an explicit table rather than as text: the three-address statement $x = y \operatorname{op} z$ is stored by placing $\operatorname{op}$ in the op field, $y$ in arg1, $z$ in arg2, and $x$ in result. For the expression $$x = (a+b) \times (c+d) - (a+b+c)$$ the three-address code is

t1 = a + b
t2 = c + d
t3 = t1 * t2
t4 = t1 + c
t5 = t3 - t4
x = t5

(noting that $a+b+c$ is computed as $t_1 + c$, reusing the already-computed $t_1 = a+b$), and its quadruple table is

#oparg1arg2result
0+abt1
1+cdt2
2*t1t2t3
3+t1ct4
4-t3t4t5
5=t5x

Triples

A triple is a leaner record with only three fields, (op, arg1, arg2), dropping the explicit result field a quadruple carries. Instead of naming a temporary explicitly, a triple is referred to, wherever it is used as an operand of a later triple, by its own position (index) in the triple table, so arg1 and arg2 hold either an operand name or a pointer to an earlier triple's result. Translating the same expression $x = (a+b) \times (c+d) - (a+b+c)$ into triples gives

#oparg1arg2
0+ab
1+cd
2*(0)(1)
3+(0)c
4-(2)(3)
5assignx(4)

Because triples identify a result only by its position, moving one triple to a different position (as an optimizer might, reordering or eliminating instructions) forces every other triple that referred to it by position to be updated too; a quadruple's explicit, named temporaries do not have this problem, since a temporary's name stays valid no matter where the instruction that computes it ends up, which is the main practical trade-off between the two representations.

SDT for intermediate code

Generating three-address code directly during parsing is a standard application of syntax directed translation. Two synthesized attributes are carried on every expression nonterminal $E$: $E.\mathrm{place}$, the name (a variable or a freshly generated temporary) that will hold $E$'s value, and $E.\mathrm{code}$, the sequence of three-address instructions that computes that value. A helper function $\mathrm{newtemp}()$ manufactures a fresh temporary name each time it is called, and $\mathrm{gen}(\cdot)$ emits one three-address instruction. For assignment statements and arithmetic expressions, the translation scheme is: $$ \begin{aligned} S &\to \mathrm{id} = E &\quad& S.\mathrm{code} = E.\mathrm{code} ;|; \mathrm{gen}(\mathrm{id}.\mathrm{place} = E.\mathrm{place}) \ E &\to E_1 + E_2 &\quad& E.\mathrm{place} = \mathrm{newtemp}();\ E.\mathrm{code} = E_1.\mathrm{code} ;|; E_2.\mathrm{code} ;|; \mathrm{gen}(E.\mathrm{place} = E_1.\mathrm{place} + E_2.\mathrm{place}) \ E &\to (E_1) &\quad& E.\mathrm{place} = E_1.\mathrm{place};\ E.\mathrm{code} = E_1.\mathrm{code} \ E &\to \mathrm{id} &\quad& E.\mathrm{place} = \mathrm{id}.\mathrm{name};\ E.\mathrm{code} = \text{empty} \end{aligned} $$

(where $|$ denotes concatenating instruction sequences). Every rule follows the same pattern seen throughout syntax directed translation: a synthesized attribute at a node is built purely from the corresponding attributes already computed at its children, so the whole translation can be produced in one bottom-up sweep as the parser reduces each production, with no separate tree-walking pass required afterward.

What this unit covers

  • Intermediate Code Generator: High-level and Low-level Intermediate representation
  • Syntax tree & DAG representations
  • Three-address code
  • Quadruples
  • Triples
  • SDT for intermediate code
  • Intermediate code generation for Declarations, Assignments, Control Flow, Boolean Expressions and Procedure Calls
  • Back patching
  • Code Generator: Factors affecting a code generator
  • Target Language
  • Basic blocks and flow graphs
  • Dynamic programming code-generation algorithm
  • Code Optimization: Need and criteria of Code Optimization
  • Basic optimization techniques
  • Case Studies of some compilers like C compiler, C++ complier

Intermediate Code Generator

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.[10]

Show solved answer

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)
Open this answer on its own page →

2076 · 6 marks

Explain with example about different methods of intermediate code representation.[6]

Intermediate code is a machine-independent representation of the source program, generated by the front end of the compiler and used as input to the back end (code optimization and code generation phases). It acts as a bridge between the source language and the target machine language. --- Three-...

Open this answer on its own page →

Back patching

2081 · 10 marks

Illustrate the concept of backpatching with an example. Convert the regular expression a(a + b)a# to DFA.[10]

Show solved answer

Backpatching and RE to DFA Conversion

Part 1: Backpatching (5 marks)

Concept

Backpatching is a technique used during single-pass code generation for handling forward jumps whose target addresses are not yet known when the jump instruction is generated.

When we emit a conditional or unconditional jump, the destination label may still be undefined. Instead of making a second pass, we:

  1. Generate the jump with the target field left blank.
  2. Keep the instruction number in a list.
  3. When the target address becomes known, we go back and fill in all instructions in that list. This "going back to fill in" is backpatching.

Data Structures and Helper Functions

  • truelist: jumps taken when a boolean is true.
  • falselist: jumps taken when a boolean is false.
  • nextlist: unconditional jumps to the statement following the current one.
FunctionMeaning
makelist(i)creates a new list with only instruction i
merge(p1,p2)concatenates two lists
backpatch(p,i)inserts i as the target of every jump in list p

Example: if (a < b) then x = 1 else x = 2

Code is emitted with blank targets:

AddrEmitted codeList
100if a < b goto ___truelist = {100}
101goto ___falselist = {101}
102x = 1(then-part)
103goto ___nextlist = {103}
104x = 2(else-part)
105...next statement

Backpatching actions:

  • then-part begins at 102 → backpatch(truelist,102) gives 100: if a<b goto 102
  • else-part begins at 104 → backpatch(falselist,104) gives 101: goto 104
  • exit is 105 → backpatch(nextlist,105) gives 103: goto 105

Final patched code:

100: if a < b goto 102
101: goto 104
102: x = 1
103: goto 105
104: x = 2
105: ...

Backpatching therefore lets the compiler resolve forward addresses in a single pass.


Part 2: Convert a(a + b)a# to DFA (Direct / Syntax-Tree Method)

Step 1: Number the leaves

The augmented expression is $a \cdot (a+b) \cdot a \cdot #$. Numbering symbol positions:

$$a_1 \cdot (a_2 + b_3) \cdot a_4 \cdot #_5$$

Step 2: Syntax tree

              . (root)
             / \
            .    #(5)
           / \
          .    a(4)
         / \
      a(1)   +
            / \
         a(2) b(3)

Step 3: nullable / firstpos / lastpos

No leaf is nullable, so every node here is non-nullable.

Nodenullablefirstposlastpos
1 (a)F{1}{1}
2 (a)F{2}{2}
3 (b)F{3}{3}
2+3F{2,3}{2,3}
1·(2+3)F{1}{2,3}
4 (a)F{4}{4}
(…)·4F{1}{4}
5 (#)F{5}{5}
rootF{1}{5}

Step 4: followpos

Rule for concatenation $c_1 \cdot c_2$: for each $i \in lastpos(c_1)$, $followpos(i) \mathrel{+}= firstpos(c_2)$.

Concat nodelastpos(left)firstpos(right)effect
1·(2+3){1}{2,3}followpos(1)={2,3}
(…)·4{2,3}{4}followpos(2)={4}, followpos(3)={4}
(…)·5{4}{5}followpos(4)={5}

followpos table:

PositionSymbolfollowpos
1a{2,3}
2a{4}
3b{4}
4a{5}
5#∅

Step 5: Construct the DFA

  • Start state $A = firstpos(\text{root}) = {1}$.
  • The accepting states are those containing position 5 (the #).

From $A={1}$ (position 1 is a):

  • on a: union of followpos of positions in $A$ with symbol a = followpos(1) = {2,3} → state $B={2,3}$.
  • on b: none.

From $B={2,3}$ (2 is a, 3 is b):

  • on a: followpos(2) = {4} → state $C={4}$.
  • on b: followpos(3) = {4} → state $C={4}$.

From $C={4}$ (position 4 is a):

  • on a: followpos(4) = {5} → state $D={5}$.
  • on b: none.

From $D={5}$ (position 5 is #, contains #, so $D$ is the accepting state):

  • followpos(5) = ∅ → no outgoing transitions.

DFA Transition Table

StatePositionson aon b
→A{1}B-
B{2,3}CC
C{4}D-
D*{5}--

DFA Diagram

        a          a / b          a
 →(A) ------> (B) --------> (C) -------> ((D))

Accepting state: $D={5}$. The DFA accepts strings matching $a(a,|,b)a$, i.e. exactly aaa and aba, before the endmarker #.

Open this answer on its own page →

Dynamic programming code-generation algorithm

2081 · 5 marks

Write the code generation algorithm for the instruction a = b op c. [5]

Code generation is the final phase of compilation that maps intermediate code (three-address instructions) to target machine instructions. For a three-address instruction of the form a = b op c, the code generator must decide: - Which registers to use for operands - Where the current values of va...

Open this answer on its own page →

Basic optimization techniques

2081 · 5 marks

What are the techniques for compiler optimization? Explain. [5]

Code optimization is the process of improving intermediate code so that the output program runs faster and takes less memory space. It removes unnecessary lines of code and arranges statements to speed up execution without wasting resources. --- As per the notes, optimization techniques are class...

Open this answer on its own page →

Basic blocks and flow graphs

2080 · 5 marks

How do you recognize basic block? Discuss about the factors that affect code generator. [5]

--- A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without any halt or possibility of branching except at the end. First, we identify leaders (the first statement of a basic block) using the following rules: 1. Rule 1: ...

Open this answer on its own page →

2081.1 · 10 marks

Explain the optimization techniques for code optimization. Convert the following program to basic block and control flow.

$M = A + B$

$N = C + D$

$\text{IF } (M > N) \rightarrow X = M - N;$

$\text{ELSE } E = M + N + X$

[10]

Program statements: (The repeated LaTeX text M=A+B, N=C+D etc. is just rendering duplication; the actual program has one statement each for M and N.) --- Code optimization transforms a program so it runs faster and/or uses less memory without changing its meaning (output). Techniques are broadly ...

Open this answer on its own page →

Code Optimization

2080 · 5 marks

Why code optimization is needed? Describe any two techniques for loop optimization. [5]

Code optimization is the process of transforming a program to make it more efficient without changing its output or meaning. It is needed because: - Reduces space and increases speed: Optimization reduces the memory consumed by the program and increases the execution speed of the compiled code. -...

Open this answer on its own page →

2078 · 5 marks

What is the advantages of code optimization? Explain about dead-code elimination. [5]

--- Code optimization is the process of improving intermediate code so that the output program runs faster and takes less memory space. Its main advantages are: 1. Improved Execution Speed: It arranges the sequence of statements in order to speed up program execution without wasting resources. 2....

Open this answer on its own page →

2076 · 6 marks

What is the purpose of code optimization? Explain different types of loop optimization techniques with example.[6]

Code optimization is the process of improving the intermediate code so that the output program runs faster and takes less memory space. It removes unnecessary lines of code and arranges the sequence of statements to speed up program execution without wasting resources. Key goals: - Reduce executi...

Open this answer on its own page →

2075 · 6 marks

Define code optimization. Discuss about any three code optimization techniques with example.[6]

Code optimization is the process of improving the intermediate code so that the output program runs faster and takes less memory space. It removes unnecessary lines of code and rearranges the sequence of statements to speed up program execution without wasting resources, and without changing the ...

Open this answer on its own page →

Three-address code

2080 · 5 marks

What is three-address code? How high level code is converted to three address code? Illustrate with an example. [5]

Three-address code is an intermediate code representation where each instruction uses at most three addresses: two for operands and one for the result. Each instruction in three-address code is described as a 4-tuple: (operator, operand1, operand2, result) The general form of a three-address stat...

Open this answer on its own page →

2075 · 6 marks

Define three address codes. write three address codes for S = do m = n + p while a <= b [6]

Show solved answer

Three Address Code

Definition (Step 1 - Extract)

Given data (from the question):

  • Statement to translate: S = do m = n + p while a <= b
  • Structure: a do ... while loop with body m = n + p and condition a <= b.

No numeric matrices or values are involved; this is a compiler-design (intermediate code generation) problem.


Solution

Definition of Three Address Code

Three Address Code (TAC) is an intermediate code representation in which each instruction contains at most three addresses -- normally two operands and one result. The general form is:

$$x = y ; op ; z$$

where:

  • $x, y, z$ are names, constants, or compiler-generated temporaries,
  • $op$ is an operator (arithmetic, logical, relational, etc.).

It is called three address code because each statement references at most three "addresses." Complex expressions are broken into a sequence of such simple instructions using temporaries. TAC can be implemented using quadruples, triples, or indirect triples.

Common statement forms:

  • Assignment: x = y op z, x = op y, x = y
  • Unconditional jump: goto L
  • Conditional jump: if x relop y goto L
  • Labels: L:

Translating: S = do m = n + p while a <= b

This is a do-while loop:

  • Body: m = n + p (executed at least once)
  • Condition: a <= b (checked after each iteration; if true, repeat)

The body executes first, then the condition is tested. If the condition holds, control loops back to the beginning of the body.


Generated Three Address Code

LineInstructionMeaning
1L1:Start of loop body (control always enters here first)
2t1 = n + pEvaluate n + p into temporary t1
3m = t1Assign result to m
4if a <= b goto L1Test condition; if true, repeat the body
5L2:Exit point of the loop

In pure sequential form:

L1:  t1 = n + p
     m  = t1
     if a <= b goto L1
L2:  ...

Explanation

  1. L1: marks the beginning of the body. Since it is a do-while, the body must run at least once, so control enters L1 unconditionally.
  2. t1 = n + p uses a new temporary t1 = newtemp() to store the arithmetic result (three-address form: one operator, two operands, one result).
  3. m = t1 completes the assignment m = n + p.
  4. if a <= b goto L1 evaluates the relational condition. If true, jump back to L1 and repeat; if false, fall through.
  5. L2: is the loop-exit label, where execution continues after the loop.

Quadruple Representation (optional supporting form)

#oparg1arg2result
(1)+npt1
(2)=t1-m
(3)if<=abL1

This confirms each instruction uses at most three addresses.

Open this answer on its own page →

Quadruples

2078 · 5 marks

Define three address codes. Write down Quadruples for: $a = -b*(c+d)/e$. [5]

Show solved answer

Three Address Code and Quadruples

Given

  • Expression to translate: $a = -b * (c + d) / e$
  • Required output format: Quadruples
  • Also required: Definition of three address code

No numeric data is involved; this is a code-generation problem. All required information is present.


Solution

Definition of Three Address Code

Three address code (TAC) is an intermediate representation of a program in which each statement contains at most three addresses (or operands): two for the operands and one for the result. The general form is:

$$x = y ; \text{op} ; z$$

where $x$, $y$, $z$ are names, constants, or compiler-generated temporary variables, and $\text{op}$ is an operator (arithmetic, logical, etc.). Each statement performs at most one operation, which makes it easy to translate into machine code and to optimize.

Three address code can be implemented in three ways: Quadruples, Triples, and Indirect Triples.


Breaking the Expression into Three-Address Statements

Expression: $a = -b * (c + d) / e$

Applying operator precedence (parentheses first, then unary minus, then $*$ and $/$ left to right):

StepStatementExplanation
1$t_1 = -b$unary minus on $b$
2$t_2 = c + d$evaluate parenthesis
3$t_3 = t_1 * t_2$multiply
4$t_4 = t_3 / e$divide by $e$
5$a = t_4$assign to $a$

Quadruple Representation

A quadruple has four fields: (op, arg1, arg2, result).

Indexoparg1arg2result
(0)uminusb_$t_1$
(1)+cd$t_2$
(2)*$t_1$$t_2$$t_3$
(3)/$t_3$e$t_4$
(4)=$t_4$_a

Notes:

  • uminus denotes the unary minus operator (distinguished from binary subtraction).
  • _ indicates an unused field.

Final Result

The expression $a = -b*(c+d)/e$ produces 5 quadruples using 4 temporary variables ($t_1, t_2, t_3, t_4$), matching the standard textbook decomposition.

Open this answer on its own page →

Code Generator

2078 · 5 marks

Explain about the factors affecting target code generation. [5]

Target code generation is the final phase of compilation that translates optimized intermediate code into machine-level instructions for a specific target machine. Several factors influence how efficient and correct the generated target code will be. --- The quality and form of the optimized inte...

Open this answer on its own page →

2076 · 6 marks

Discuss about the different factors affecting target code generation.[6]

Target code generation is the final phase of compilation that translates optimized intermediate code into target machine language. The quality and correctness of the generated code depends on several important factors. --- - The code generator takes optimized intermediate code as its input. - The...

Open this answer on its own page →

2075 · 6 marks

Discuss about the different factors affecting target code generation.[6]

Target code generation is the final phase of compilation that translates optimized intermediate code into target machine code. Several important factors influence this process. --- The input to the code generator is the optimized intermediate code produced by earlier phases. The quality and form ...

Open this answer on its own page →

Syntax tree & DAG representations

2081.1 · 10 marks

Discuss about Directed Acyclic Graph with an example. Represent the expression A = (B + C) - (D - E) using 3AC, Quadruple and Triple.[10]

--- A Directed Acyclic Graph (DAG) is a graph representation of an expression that, like a syntax tree, has: - Leaf nodes representing operands (identifiers/constants) - Interior nodes representing operators The key difference is that a DAG shares nodes for common sub-expressions. A node represen...

Open this answer on its own page →

Intermediate code generation for Declarations, Assignments, Control Flow, Boolean Expressions and Procedure Calls

2081.1 · 5 marks

What are the advantages of intermediate code? How do you convert procedure call to 3AC? [5]

--- The key advantages of using intermediate code representation are: 1. Portability / Machine Independence - If a compiler translates source language directly to target machine language without intermediate code, then for each new machine a full native compiler is required. - Intermediate code e...

Open this answer on its own page →