Compiler Design
Compilers translate high-level source code into machine code. Understanding compilation phases, parsing, and code generation helps you write better code and build developer tools.
Contents 2 Sections
1Compilation Phases
1. Lexical Analysis (Scanning)
Converts source text into tokens (the smallest meaningful units).
``
Source: "let x = 5 + y;"
Tokens: [LET, IDENT("x"), ASSIGN, INT(5), PLUS, IDENT("y"), SEMICOLON]
``
2. Syntax Analysis (Parsing)
Builds an Abstract Syntax Tree (AST) from tokens according to grammar rules.
``
AssignmentExpr
├── Identifier: x
└── BinaryExpr (+)
├── IntLiteral: 5
└── Identifier: y
``
- 3. Semantic Analysis
Type checking, scope resolution, ensure program is meaningful.
- Variable declarations before use
- Type compatibility (can't add int + string without coercion)
- Function signatures match call sites
4. Intermediate Code Generation Convert AST to platform-independent IR (e.g., LLVM IR, three-address code).
- 5. Optimization
- Constant folding:
3 + 4 → 7at compile time - Dead code elimination: remove unreachable branches
- Loop unrolling and inlining
6. Code Generation Map IR to target machine instructions (x86, ARM, WebAssembly).
2Grammars & Parsing
Context-Free Grammar (CFG)
``
expr → expr + term | term
term → term * factor | factor
factor → ( expr ) | NUMBER | IDENT
``
Recursive Descent Parser (simple)
``javascript
function parseExpr(tokens) {
let left = parseTerm(tokens);
while (peek(tokens) === '+' || peek(tokens) === '-') {
const op = consume(tokens);
const right = parseTerm(tokens);
left = { type: 'BinaryExpr', op, left, right };
}
return left;
}
``
- Common Parsing Algorithms
- LL(k): top-down, reads k tokens ahead. Simple to implement.
- LR/LALR: bottom-up, handles wider grammars. Used by yacc/bison.
- Real-World Compiler Tools
- LLVM: production-grade backend used by clang, Rust, Swift
- Babel: JavaScript transpiler (AST transformation)
- Tree-sitter: fast incremental parser for editors
Explore the Full Portfolio
This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.
The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.
For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.