Passes and Phases of Compiler Design

Last modified on November 5th, 2020 Download This Tutorial in PDF

What is a Compiler?

A compiler is special software that converts the high-level language code into machine code.

Passes and Phases of Compiler Design

If we divide the compiler on the basis of the way in which the compiler compiles the program, then we can divide it into two phases.

  1. Analysis Phase
  2. Synthesis Phase

Analysis Phase in Compiler Design

The Analysis Phase is also known as the front-end of the compiler. When the program is in the Analysis phase, the compiler reads the source program, divides it into some core parts, and then checks the program for some errors just like lexical errors, grammar errors, and syntax errors.

During the analysis phase, the compiler generates an intermediate representation of the source program and symbol table and should be fed to the Synthesis phase as an input.

Passes and Phases of Compiler Design

Synthesis Phase in Compiler Design

The synthesis phase is also known as the back-end of the compiler. The compiler generates the target program with the help of intermediate source code representation and symbol table during the synthesis phase.

Phases of Compiler design

Phases of Compiler
Phases of Compiler

A phase of a compiler takes input from the previous stage, processes and produces the output that can be used as input for the next stage of the compiler. A pass can have more than one phase.

The compiler works in phases and each phase of the compiler transforms the source code from one representation to another representation. Every compiler phase takes inputs from its previous stage and gives its output to the next phase of the compiler for further processing.

There is a total of 6 major phases in a compiler design. Each of the phase help in converting the source code in high-level langue to the source in machine language. The phases of a compiler are mentioned below;

  1. Lexical analysis
  2. Syntax analysis
  3. Semantic analysis
  4. Intermediate code generator
  5. Code optimizer
  6. Code generator

These 6 phases of the compiler convert the source code by performing the following actions;

  • dividing into tokens
  • creating parse trees
  • optimizing the source code by different phases.

Phase 1 (Compiler design): Lexical Analysis

What is the output of the lexical analyzer?

The output of the lexical analyzer phase is the stream of tokens. Stream of tokens consists of identifiers, keywords, separators, operators, and literals.

Lexical Analysis is the first phase of the compiler design. During Lexical Analysis, the compiler scans the source code. This scanning process of the source code can be character by character and from left to right. During the Lexical Analysis phase, we group these characters into

In other words, we can say that characters from the source program are grouped into the meaningful sequences of the characters by identifying the possible tokens in the source code.

It makes the entry of the corresponding tickets into the symbol table and passes that token to the next phase.

The primary functions of the Lexical Analysis are mentioned below;

  • Identification of the lexical units present in the source code.
  • Classify the lexical units into classes. For example, we can classify them into reserved words, constants, and enter them in different tables. Lexical Analysis Ignore the comments in the source code.
  • Identify token which is not a part of the language

Example of Lexical Analysis in Compiler design

Perform the lexical analysis of the following statement x = y + 10.


X Identifier/Variable
= Assignment operator
Y identifier/Variable
+ Addition operator
10 Number/value

Phase 2: Syntax Analysis

What is the output of the Syntax Analyzer phase?

The parser analyzes the tokens streams against the production rules to detect any errors in the token streams of the given code. The output of the Syntax Analyzer phase is a parse tree.

What is the output of the Semantic Analyzer?

The output of the Semantic Analyzer is an annotated syntax tree.

What is the output of intermediate code generation?

The output of intermediate code generation is a linear representation of the syntax tree.

Code optimization phase

Example of Code optimization phase :

Question: How to optimize the given code?

a = intofloat(5)

b = c * a

d = e + b

finally = d

Answer: We can optimize the given code as mentioned below;

b =c * 5.0

finally = e+b

Why code optimization is called an optional phase?

The code optimization phase is an optional phase in the compiler construction. For example, If the intermediate representation is a good enough representation to develop the object code, then the compiler can skip the process of optimizing the intermediate representation.

What is the output of code generation?

The output of code generation is the target program.

What is Pass in Compiler?

A pass refers to the traversal of a compiler take place through the entire program, then it is called a pass.

Subscribe for Friendship

Latest posts by Prof. Fazal Rehman Shamil (see all)