Back to Blogs
How to write a compiler ?
This series of blogs acts as an analysis and deep dive into compiler design.
Currently I am building EEL. EEL is eBPF Language, this series of blogs consists of all the things I learned throughout the process of building it.
What is a compiler?
A compiler is a program that translates source code written in one language into another representation, usually machine code or an intermediate form that can be executed by a computer.
The Puzzle Pieces
Modern compilers are made up of several different pieces which come together as a puzzle to make a compiler.
These pieces are:
- Lexer - Converts character streams to token streams.
- Parser - Builds Abstract Syntax Trees (AST) based on grammar rules.
- Semantic Analyzer - Performs type check, scope validation, and AST checks.
- IR Generator - Produces Intermediate Representation.
- Optimizer - Refactors code pathways for high performance.
- Code Generator - Translates IR to target architecture assembly/bytecode.
- Assembler - Turns assembly text into binary machine instructions.
- Linker - Combines machine instructions and externs into an Executable.
We will analyze each of these one by one in great detail.
Source Code
↓
Lexer
↓
Tokens
↓
Parser
↓
AST
↓
Semantic Analysis
↓
IR
↓
Optimizer
↓
Code Generator
↓
Assembler
↓
Linker
↓
ExecutablePseudocode Reference
Throughout the blogs we will take following pseudocode as reference:
fn add(a,b){
price = 10;
total = price + 42;
print(total);
}
Aadarsh Chandra (Pi)
I am an 18-year-old self-employed developer building full-stack web applications, Python/Rust backends, and low-level systems. I learn by building things from first principles.