Compiler Overview — Phases, Front-end vs Back-end, Tools
509 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Compiler Overview — Phases, Front-end vs Back-end, Tools ## 🎯 Learning Objectives - List and describe the phases of a compiler - Explain the front-end vs back-end distinction - Describe the role of Flex and Bison - Trace a program through all compilation phases * * * ## 1. Compiler Phases ### 1.1 Intuition A comp...

Compiler Overview — Phases, Front-end vs Back-end, Tools
🎯 Learning Objectives
- List and describe the phases of a compiler
- Explain the front-end vs back-end distinction
- Describe the role of Flex and Bison
- Trace a program through all compilation phases
1. Compiler Phases
1.1 Intuition
A compiler translates a high-level language (like C) into low-level machine code. This complex task is broken into phases — each phase transforms the program representation step by step.
1.2 Phase Overview
(Diagram)
1.3 Phase Detail
| Phase | Input | Output | Key Concepts |
|---|---|---|---|
| Lexical Analysis | Source characters | Tokens | Regex, NFA, DFA, Flex |
| Syntax Analysis | Tokens | Parse Tree / AST | CFG, LL/LR parsing, Bison |
| Semantic Analysis | AST | Annotated AST | Type checking, symbol table |
| Intermediate Code Gen | AST | TAC / IR | 3-address code, temp variables |
| Optimization | IR | Optimized IR | CSE, constant folding, LICM |
| Code Generation | IR | Assembly/Machine | Register allocation, instruction selection |
2. Front-end vs Back-end
(Diagram)
Front-end: Depends on source language, independent of target machine Back-end: Depends on target machine, independent of source language
Benefits: Reuse back-end for multiple languages (GCC), reuse front-end for multiple machines (LLVM).
3. Compiler Construction Tools
3.1 Flex (Lexical Analyzer Generator)
Takes regular expression patterns → generates C code for a DFA-based scanner
3.2 Bison (Parser Generator)
Takes a CFG grammar → generates C code for an LALR(1) parser
(Diagram)
4. 📝 Practice Questions
Q1: List the six major phases of a compiler in order.Answer: Lexical Analysis → Syntax Analysis → Semantic Analysis → Intermediate Code Generation → Optimization → Code Generation Q2: What is the advantage of using an intermediate representation (IR)?Answer: IR decouples the front-end from the back-end. The same IR can be used for multiple source languages (C, C++, Fortran → same IR) and multiple target architectures (x86, ARM, RISC-V). This dramatically reduces the number of components needed (n languages × m architectures → n+m components instead of n×m). Q3: What does Flex generate? What does Bison generate?Answer: Flex generates a C function calledyylex()that performs lexical analysis (a DFA-based tokenizer). Bison generates a C function calledyyparse()that performs syntax analysis (an LALR(1) parser). The two are linked:yylex()is called byyyparse()to get the next token. Q4: What is triple-address code (TAC)?Answer: TAC is an IR where each instruction has at most three operands (two sources, one destination). Example:t1 = a + b; t2 = t1 * c;. It's called "triple" or "three-address" code because instructions reference at most three variables. TAC is easy to generate and simplifies optimization. Q5: Why are compilation phases separated rather than combined?Answer: Separation provides: (1) modularity (each phase can be developed/tested independently), (2) reusability (front-end shared across targets), (3) manageability (each phase has a focused task), and (4) additional optimization (IR simplifications between phases).
5. 🔗 Cross-References
- Week 2 - Lexical Analysis: First phase in detail
- Week 3 - Syntax Analysis: Second phase
- BSCS3021 (ToC): Regular languages, CFG theory Join Discord NextLexical Analysis