The Wayback Machine - https://web.archive.org/web/20261121160757/https://www.geeksforgeeks.org/compiler-design/intermediate-code-generation-in-compiler-design/

Intermediate Code Generation in Compiler Design

Last Updated : 18 Nov, 2026

Intermediate Code Generation is a compiler phase that converts the source program into an intermediate representation (IR) between the front end and back end. The IR simplifies program analysis and optimization and helps generate target code for different machine architectures.

  • Machine Independence: A common IR can be used to support multiple target architectures.
  • Optimization: The compiler can optimize the IR before generating target code.
  • Retargeting: Supporting a new target machine may require implementing or modifying the back end instead of building an entirely new compiler.
frame_1000002479

Process of Intermediate Code Generation

Intermediate Code Generation converts the source program into an intermediate representation that can be analyzed and optimized before target code generation.

  • Translation: The compiler converts the source program into an intermediate representation.
  • Optimization: The intermediate code is optimized to improve performance, reduce code size, or minimize resource usage.
  • Target Code Generation: The back end converts the optimized intermediate representation into target-specific code.

Types of Intermediate Representation

1. Postfix Notation

Postfix notation, also known as Reverse Polish Notation (RPN), places operators after their operands.

  • In infix notation, operators appear between operands, such as a + b.
  • In postfix notation, the operator appears after the operands, such as ab+.
  • Postfix notation eliminates the need for parentheses to represent operator precedence.

Example:

Infix Expression

Postfix Expression

a + b

ab+

(a + b) * c

ab+c*

(a - b) * (c + d) + (a - b)

ab-cd+*ab-+

2. Three-Address Code

Three-Address Code (TAC) is an intermediate representation in which each instruction contains at most three address-like operands: typically, two operands and one result.

  • A typical instruction has the form x = y op z.
  • Temporary variables are used to store intermediate results.
  • TAC simplifies code optimization and target code generation.

Example: Three-address code for a + b * c + d:

T1 = b * c
T2 = a + T1
T3 = T2 + d

Three-address code can be represented using:

  • Quadruples
  • Triples
  • Indirect Triples

3. Syntax Tree

A syntax tree is a compact representation of the syntactic structure of an expression or program.

  • Internal nodes represent operators or language constructs.
  • Leaf nodes represent operands, identifiers, or constants.
  • It omits unnecessary grammar details found in a parse tree, making it useful for analysis and optimization.

Example: Expression x = (a + b * c) / (a - b * c)
 

frame_1000002484

Benefits

  • Simplifies compiler implementation by separating the front end from the back end.
  • Supports code optimization before target code generation.
  • Enables compiler retargeting for different machine architectures.
  • Allows reuse of compiler components across different platforms.

Limitations

  • Requires additional memory to store the intermediate representation.
  • Adds an extra compilation stage, which may increase compilation time.
  • Increases compiler implementation complexity.
  • Some intermediate representations may be less readable than the original source code.
Comment

Explore