Six Phases of compiler – Lexical /Syntax /Semantic /Intermediate code generation/code optimization

Six Phases of compiler – Lexical /Syntax /Semantic /Intermediate code generation/code optimization



play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

 Six Phases of Compiler

A compiler translates high-level code into machine code through six main phases:

Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generation
Code Optimization
Code Generation

 Lexical Analysis (Scanning)

Input: Source Code (High-level language)
Output: Tokens
Purpose: Converts the source code into tokens (smallest units of the program).
Performs:
 Removes whitespaces, comments
 Identifies keywords, identifiers, operators, symbols
 Stores tokens in Symbol Table

Example:
Input: int x = 10;
Output:

TOKEN( int ), TOKEN( x ), TOKEN( = ), TOKEN( 10 ), TOKEN( ; )

 Syntax Analysis (Parsing)

Input: Tokens from Lexical Analysis
Output: Parse Tree (Syntax Tree)
Purpose: Checks if the code follows the grammar rules (using parsing techniques like LL(1), LR(1)).
Performs:
 Builds Parse Tree
 Detects Syntax Errors

Example:
Input: int x = 10;
Output: Parse Tree

Assignment
/ | \
int x 10

 If we write int = x 10;, the compiler will show a syntax error.

 Semantic Analysis

Input: Parse Tree
Output: Annotated Parse Tree
Purpose: Checks meaning and correctness of the program.
Performs:
 Type Checking (int x = “hello”; is invalid)
 Variable declaration checking
 Function parameter matching

Example:
Code:

int x = "hello"; // Error: String assigned to integer

Error: Type Mismatch

 Intermediate Code Generation

Input: Annotated Parse Tree
Output: Intermediate Code (3-address code, AST)
Purpose: Generates a machine-independent representation of the program.
Performs:
 Converts code to Intermediate Representation (IR)
 Uses Three-Address Code (TAC)

Example:
Code:

a = b + c * d;

TAC Output:

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

 Code Optimization

Input: Intermediate Code
Output: Optimized Intermediate Code
Purpose: Makes the code faster and memory-efficient.
Performs:
Constant Folding5 + 38
Dead Code Elimination → Removes unreachable code
Loop Optimization → Moves invariant expressions out of loops

Example:
Before Optimization:

int x = 2 * 10; // Multiplication every time

After Optimization:

int x = 20; // Precomputed at compile-time

 Code Generation

Input: Optimized Intermediate Code
Output: Target Machine Code (Assembly/Binary)
Purpose: Converts IR to actual machine code for execution.
Performs:
Register Allocation
Instruction Selection
Machine-specific code generation

Example:
IR:

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

Assembly Code (x86):

MOV R1, c
MUL R1, d
ADD R1, b
MOV a, R1

 Summary of Compiler Phases

Phase Input Output Purpose
Lexical Analysis Source Code Tokens Tokenization
Syntax Analysis Tokens Parse Tree Grammar Checking
Semantic Analysis Parse Tree Annotated Parse Tree Type Checking
Intermediate Code Generation Annotated Parse Tree Intermediate Code Machine Independence
Code Optimization Intermediate Code Optimized Code Faster Execution
Code Generation Optimized Code Machine Code Final Execution

 Final Notes

Lexical, Syntax, and Semantic AnalysisDetect errors.
Intermediate Code & OptimizationImprove efficiency.
Code GenerationFinal machine-executable code.

 Would you like a detailed example on any specific phase?

Six Phases of compiler – Lexical /Syntax /Semantic /Intermediate code generation/code optimization



Leave a Reply

Your email address will not be published. Required fields are marked *

error: