Phases of Compiler


Video Tutorial Thumbnail


description

Simple C Program

description

Phases of Compiler

description

2. Syntax Analysis

Purpose: Ensures the code follows the rules of grammar.

Example: Checks if statements like sum = a + b; are syntactically correct.

Output: A syntax tree or parse tree that represents the structure of the program.


3. Semantic Analysis

Purpose: Checks the meaning of the program.

Verifies that:


4. Intermediate Code Generation

Purpose: Transforms the code into an intermediate representation (IR), which is easier for the compiler to optimize.

Example IR for the program

description

5. Code Optimization

Purpose: Improves the efficiency of the intermediate code.

Example optimization:

Instead of separately assigning t1 = 5 and t2 = 3, directly calculate t3 = 8.


6. Code Generation

Purpose: Converts the optimized intermediate code into machine code (binary instructions).

Example machine code

description

This machine code is what the computer executes.


7. Symbol Table and Error Handling

A symbol table keeps track of variables (a, b, sum), their types, and memory locations throughout all phases.

Error handling happens in phases like:


A compiler goes through 7 phases: