Compiler Design and Construction - Old Questions

3. What is the role of intermediate code generation in the entire compilation process? Convert the following into three address code.

        a+(b-c)*d

10 marks | Asked in Model Question

Intermediate code is used to translate the source code into the machine code. Intermediate code lies between the high-level language and the machine language. The given program in a source language is converted into an equivalent program in an intermediate language by the intermediate code generator. Intermediate codes are machine independent codes. 

Compiler - Intermediate Code Generation - Tutorialspoint

Roles of Intermediate code are :

  • It acts as a glue between front-end and backend (or source and machine codes).
  • If the compiler directly translates source code into the machine code without generating intermediate code then a full native compiler is required for each new machine.
  • The intermediate code keeps the analysis portion same for all the compilers that's why it doesn't need a full compiler for every unique machine.
  • Intermediate code generator receives input from its predecessor phase and semantic analyzer phase. It takes input in the form of an annotated syntax tree.
  • Using the intermediate code, the second phase of the compiler synthesis phase is changed according to the target machine.
  • Intermediate code generator lowers abstraction from source level.
  • Complete some syntactic checks, perform more semantic checks. For e.g. break should be inside loop or switch only.

The intermediate code representation are

  • Graphical representation e.g. Abstract Syntax Tree(AST) , DAGS
  • Postfix notations
  • Three Address codes

Given expression:

a+(b-c)*d

Converting it into three address code:

    t1 = b-c

    t2 = t1*d

    t3 = a+t2

For given expression the quadruples is:

 

op

arg1

arg2

result

(0)

-

b

c

t1

(1)

*

t1

d

t2

(2)

+

a

t2

t3


For given expression, the triples is:

 

op

arg1

arg2

(0)

-

b

c

(1)

*

(0)

d

(2)

+

a

(1)