The Wayback Machine - https://web.archive.org/web/20261122103234/https://www.geeksforgeeks.org/compiler-design/phases-of-a-compiler/

Phases of a Compiler

Last Updated : 14 Nov, 2026

The compilation process is divided into several phases. Each phase performs a specific task to transform the source code step by step into machine code.

There are 6 major phases in the compilation process.

compiler
Phases of Compiler

1. Lexical Analysis

Lexical Analysis is the first phase of a compiler that scans source code and converts it into tokens such as keywords, identifiers, operators, and constants. It groups characters based on language rules and passes the tokens to the next compilation phase.

Example: int x = 10;

The lexical analyzer would break this line into the following tokens:

  • int - Keyword token (data type)
  • x - Identifier token (variable name)
  • = - Operator token (assignment operator)
  • 10 - Numeric literal token (integer value)
  • ; - Punctuation token (semicolon, used to terminate statements)

2. Syntax Analysis or Parsing

Syntax Analysis checks whether the tokens follow the grammatical rules of the programming language. It verifies the code structure and identifies syntax errors. This phase uses parse trees or syntax trees to represent the structure of the source code.

  • Parse Tree: Represents the complete syntactic structure of the source code using grammar rules.
  • Syntax Tree: Represents the essential structure of the code in a simplified, hierarchical form.
phases
Parse Tree

3. Semantic Analysis

Semantic Analysis checks whether the source code is logically meaningful and follows the programming language's semantic rules. It identifies errors such as type mismatches, undeclared variables, and invalid operations.

  • Type Checking: Ensures that operations use compatible data types.
  • Variable Declaration: Checks whether variables are declared before they are used.

Example:

int a = 5;
float b = 3.5;
a = a + b;
  • a is int and b is float. Adding them (a + b) results in float, which cannot be assigned to int a.
  • Error: Type mismatch: cannot assign float to int.

4. Intermediate Code Generation

Intermediate Code Generation converts the source code into an intermediate representation (IR) that is independent of any specific machine. It acts as a bridge between the front-end and back-end, making the code easier to optimize and translate into target machine code.

Importance of Intermediate Code

1. Platform Independence: Since intermediate code is machine-independent, the same intermediate representation can be reused, but code generation must be performed again for each target platform.

2. Simplifying Optimization: Intermediate code provides a structured and simplified representation of the program, making it easier to apply optimization techniques such as:

  • Dead Code Elimination: Removing parts of the code that don’t affect the program’s output.
  • Loop Optimization: Improving loops to make them run faster or consume less memory.
  • Common Subexpression Elimination: Reusing previously calculated values to avoid redundant calculations.

3. Easier Translation: Intermediate code is closer to machine code but not tied to a specific machine. This makes the final code generation process smoother and more efficient. This step is handled in the back end of the compile

Example:

a = b + c * d;
t1 = c * d
t2 = b + t1
a = t2

5. Code Optimization

Code Optimization is the process of improving the intermediate or target code to make the program run faster, use less memory, or be more efficient, without altering its functionality.

  • Involves techniques like removing unnecessary computations, reducing redundancy, and reorganizing code to achieve better performance.
  • Optimization is classified broadly into two types Machine-Independent & Dependent

Common Techniques:

  • Constant Folding: Precomputing constant expressions.
  • Dead Code Elimination: Removing unreachable or unused code.
  • Loop Optimization: Improving loop performance through invariant code motion or unrolling.
  • Strength Reduction: Replacing expensive operations with simpler ones.

Example:

Code Before Optimization

for (int j = 0; j < n; j++)
{
    x = y + z;
    a[j] = 6 * j;
}

Code After Optimization

x = y + z;

for (int j = 0; j < n; j++)
{
a[j] = 6 * j;
}

6. Code Generation

Code Generation is the final phase of a compiler that converts the intermediate representation (IR) into target machine code or assembly code. It produces object code while considering the target machine's architecture and requirements.

  • It should carry the exact meaning of the source code.
  • It should be efficient in terms of CPU usage and memory management.

Example:

Three Address Code

Assembly Code

t1 = c * d

t2 = b + t1

a = t2

LOAD R1, c ; Load the value of 'c' into register R1

LOAD R2, d ; Load the value of 'd' into register R2

MUL R1, R2 ; R1 = c * d, store result in R1

LOAD R3, b ; Load the value of 'b' into register R3

ADD R3, R1 ; R3 = b + (c * d), store result in R3

STORE a, R3 ; Store the final result in variable 'a'

Symbol Table: It is a data structure being used and maintained by the compiler, consisting of all the identifier's names along with their types. It helps the compiler to function smoothly by finding the identifiers quickly.

Front End and Back End Architecture

The compiler is broadly divided into two main parts:

1
Parts of the Compiler

1. Front-End (Analysis Phase)

The front-end analyzes the source program and converts it into an intermediate representation (IR).

  • Input: Accepts source code written in a high-level programming language.
  • Lexical Analysis: Converts the source code into tokens.
  • Syntax Analysis: Checks the grammatical structure of the code.
  • Semantic Analysis: Verifies the meaning and consistency of the program.
  • Intermediate Code Generation: Converts the validated source code into an intermediate representation (IR).
  • Output: Produces intermediate code for the back-end.
  • Purpose: Analyzes and validates the source program before passing it to the back-end.

2. Back-End (Synthesis Phase)

The back-end takes the intermediate representation and generates the target machine code.

  • Input: Receives the intermediate representation from the front-end.
  • Code Optimization: Improves the intermediate code to make the generated program more efficient.
  • Code Generation: Converts the optimized intermediate code into target machine code.
  • Output: Produces machine code or other target code for execution.
  • Purpose: Generates efficient target code while considering the target machine's architecture and requirements.

Also Read:

Comment

Explore