Intermediate Representations: Abstract Syntax Trees and Three-Address Code

In the field of compiler design, intermediate representations (IRs) play a crucial role in the conversion of high-level programming languages into low-level machine code. IRs act as an intermediate step between the source code and the final executable code, allowing for easier analysis and transformations. Two commonly used intermediate representations are Abstract Syntax Trees (ASTs) and Three-Address Code.

Abstract Syntax Trees (ASTs)

An Abstract Syntax Tree is a hierarchical data structure that represents the syntactic structure of a program. It captures the relationships between different elements of the source code, such as statements, expressions, and declarations. ASTs eliminate irrelevant details and focus on the essential components of a program's structure.

Construction of ASTs

During the parsing phase of a compiler, the source code undergoes lexical and syntactic analysis to recognize the grammar rules. This process generates a parse tree, which represents the derivation of the source code. The parse tree is subsequently transformed into an AST by applying certain rules and optimizations.

Nodes and Edges

In an AST, the nodes represent different language constructs, such as variable declarations, expressions, or control flow statements. Each node has children that correspond to its sub-components. For instance, an if-else statement node may have a conditional expression node as its child, and two blocks of code nodes as its children representing the if and else clauses.

The edges in an AST represent the relationships between the nodes. These relationships can be parent-child relationships or sibling relationships, depending on the structure of the language construct being represented.

Benefits of ASTs

ASTs offer several advantages in the context of compiler design:

  1. Simplified representation: ASTs discard unnecessary details present in the source code, providing a cleaner and more manageable representation for analysis and optimization.

  2. Language-independent: ASTs can be used as an intermediate representation for multiple programming languages. The structure of an AST defines the underlying semantics, making it suitable for various target languages.

  3. Efficient analysis: ASTs allow for efficient traversal and analysis of the program structure. Compilers can perform semantic checks, type inference, and optimization techniques based on the AST representation.

Three-Address Code (TAC)

Three-Address Code is another widely used intermediate representation technique. It represents high-level language constructs as a series of basic operations involving at most three operands. Each operation in TAC computes a value and stores the result in a temporary variable. TAC provides a simpler and more compact representation compared to ASTs.

Basic Operations

TAC operations typically include arithmetic operations (addition, subtraction, multiplication, division), logical operations (AND, OR, NOT), assignments, conditional branches, and function calls. Each operation is assigned a unique label to facilitate control flow analysis and optimization.

Temporary Variables

Three-Address Code extensively utilizes temporary variables to store intermediate results. These variables are typically denoted by symbols like T1, T2, T3, etc. The assignment of values to temporary variables helps in expressing complex computations as a series of simpler operations.

Benefits of TAC

The use of Three-Address Code in compiler design offers several advantages:

  1. Simplicity: TAC simplifies the complexities present in high-level programming languages by breaking down complex expressions into a sequence of basic operations. This allows for easier translation and optimization.

  2. Optimization possibilities: The structured and explicit nature of TAC facilitates efficient code optimization techniques. The code can be transformed, modified, and optimized using a variety of compiler optimization algorithms.

  3. Code generation: TAC serves as an efficient representation for code generation. It is relatively easy to convert TAC into low-level machine code, either directly or through additional transformations.


Intermediate representations like Abstract Syntax Trees (ASTs) and Three-Address Code (TAC) contribute significantly to the compilation process. ASTs provide a structured representation of the program's syntax and offer convenient analysis and transformation possibilities. On the other hand, TAC simplifies high-level language constructs into a series of basic operations, allowing for efficient optimization and code generation. Both representations play crucial roles in modern compiler design, enabling the translation of source code into optimized machine code.

© NoobToMaster - A 10xcoder company