Compiler Construction - BSCS Notes

Compiler Construction — Complete BSCS Notes

Compiler Techniques & Methodology

Definition: A compiler is a program that converts high-level language into machine code.
Methodology: Step-by-step translation process (analysis → synthesis).
Example: C program → machine code

Organization of Compiler

Phases:
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
4. Code Generation
Front End: Analysis
Back End: Code generation

Lexical Analysis

Definition: Converts source code into tokens.
Example:
int a = 5;
Tokens → int, a, =, 5

Syntax Analysis

Definition: Checks grammatical structure of program.
Example:
a = 5 + ; ❌ (invalid)

Parsing Techniques

Types:
Top-Down Parsing
Bottom-Up Parsing
Example: Parse tree generation

Object Code Generation

Definition: Convert intermediate code into machine code.
Example:
a = b + c → ADD instruction

Code Optimization

Definition: Improve code efficiency.
Example:
x = 2 * 3 → x = 6

Error Detection & Recovery

Types:
Lexical Error
Syntax Error
Semantic Error
Recovery: Skip, insert, or replace tokens

Compiler vs Interpreter

Compiler: Translates whole program at once
Interpreter: Executes line by line
Example:
C → Compiler
Python → Interpreter