Compiler Design and Construction - Old Questions

8.  What do you mean by intermediate code? Explain the role of intermediate code in compiler design.

6 marks | Asked in 2068

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