Compiler Design Cheatsheet – Phases, Parsing, Code Generation

Introduction
Compiler Design is a core subject in Computer Science Engineering (CSE) that often feels overwhelming due to its theoretical concepts and technical depth. Whether you’re preparing for semester exams, GATE, or placement interviews, having a concise cheatsheet of compiler design can save you hours of last-minute revision.
In this guide, we’ll cover:
- The phases of a compiler (front-end, middle-end, and back-end).
- Parsing techniques (top-down and bottom-up).
- Syntax-directed translation and intermediate code generation.
- Code optimization and code generation.
This cheatsheet is designed to be crisp, exam-friendly, and easy to revise — perfect for BTech students.

What is a Compiler?
A compiler is a software program that translates high-level programming language code (like C, Java, or Python) into machine code that the computer can execute.
Key points:
- Converts source code → target code.
- Ensures correctness and efficiency.
- Performs optimization for faster execution.
Difference between Compiler and Interpreter
Feature | Compiler | Interpreter |
---|---|---|
Translation | Translates the whole program at once | Translates line by line |
Execution Speed | Faster (after compilation) | Slower |
Memory Usage | Requires more memory | Requires less memory |
Examples | GCC, Java Compiler | Python Interpreter |

Phases of a Compiler
A compiler works in multiple phases, each handling a specific task. These phases are usually grouped into Front-end, Middle-end, and Back-end.
1. Lexical Analysis (Scanner)
- Converts source code into tokens.
- Removes whitespace and comments.
- Uses Finite Automata for token recognition.
- Output: A sequence of tokens.
Example:
Input: int a = 5;
Tokens: int
, id
, =
, num
, ;

2. Syntax Analysis (Parser)
- Checks grammar rules of the programming language.
- Builds a parse tree from tokens.
- Errors detected: Missing semicolons, misplaced brackets, etc.
Two main parsing techniques:
- Top-Down Parsing (LL)
- Bottom-Up Parsing (LR)
We’ll cover parsing in detail later in this cheatsheet.
3. Semantic Analysis
- Ensures meaningful statements.
- Checks type compatibility (e.g., you cannot add a string and integer).
- Uses symbol table to verify variable declarations.
4. Intermediate Code Generation (ICG)
- Converts parse tree into an intermediate representation (IR).
- Example IR forms: Three-address code (TAC), postfix notation.
Example TAC:
For expression: a = b + c * d
TAC:
t1 = c * d
t2 = b + t1
a = t2

5. Code Optimization
- Improves performance without changing meaning.
- Removes redundancies, reuses common sub-expressions, and reduces memory usage.
Examples:
- Common subexpression elimination.
- Dead code elimination.
- Loop optimization.
6. Code Generation
- Converts optimized intermediate code into target machine code.
- Handles register allocation, instruction selection, and addressing modes.
- Output: Assembly or binary code.

Parsing in Compiler Design
Parsing is a critical part of compiler design where the syntax of the program is checked. Let’s look at its techniques.
Types of Parsing
- Top-Down Parsing
- Starts from the root and works towards the leaves.
- Methods: Recursive Descent, LL(1) Parsing.
- Grammar:
E → T + E | T
- Input:
id + id
- Parsed using function calls recursively.
- Bottom-Up Parsing
- Starts from input symbols and reduces them to the start symbol.
- Methods: Shift-Reduce Parsing, LR(0), SLR(1), LALR(1), CLR(1).
id + id
:- Shift
id
- Reduce
id → E
- Shift
+
- Shift
id
- Reduce
id → E
- Reduce
E + E → E

Syntax-Directed Translation (SDT)
- Combines parsing with semantic rules.
- Produces annotated parse trees with attributes.
- Attributes:
- Synthesized (calculated from children).
- Inherited (passed from parent).
Example: For arithmetic expression evaluation.

Code Generation in Detail
The final stage of a compiler is code generation, where machine-level instructions are produced.
Steps in Code Generation
- Instruction Selection
- Register Allocation
- Memory Management
- Instruction Scheduling
Example
Source Expression: a = (b + c) * d
Intermediate Code (TAC):
t1 = b + c
t2 = t1 * d
a = t2
Generated Assembly (x86 example):
MOV R1, b
ADD R1, c
MUL R1, d
MOV a, R1
Summary Table of Compiler Phases
Phase | Input | Output | Tool/Concept Used |
---|---|---|---|
Lexical Analysis | Source code | Tokens | Finite Automata |
Syntax Analysis | Tokens | Parse Tree | CFG, Parsing Algorithms |
Semantic Analysis | Parse Tree | Annotated Tree | Symbol Table |
Intermediate Code | Annotated Tree | IR (TAC) | SDT |
Optimization | IR | Optimized IR | Peephole, Loop Opt. |
Code Generation | Optimized IR | Machine Code | Register Allocation |
[YOUTUBE VIDEO PLACEHOLDER — Search YouTube for: “compiler design phases explained”]
FAQ Section
1. What are the 6 phases of a compiler?
The phases are: Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code Optimization, and Code Generation.
2. What is the difference between parsing and lexical analysis?
Lexical analysis converts characters into tokens, while parsing checks the grammatical structure of those tokens.
3. Which parsing method is most commonly used in compilers?
Bottom-up parsing (LR Parsing) is widely used in real compilers because it can handle a larger class of grammars.
4. What is intermediate code in compiler design?
It is a machine-independent representation of the program, usually in the form of three-address code, which makes optimization easier.
5. Why is code optimization important?
Optimization improves execution speed, reduces memory usage, and ensures the compiled code runs efficiently.
Conclusion
Compiler Design may look complex, but breaking it down into phases, parsing, translation, and code generation makes it much easier to grasp. This cheatsheet is meant to give you a quick, exam-ready overview without losing the core details. Use it for:
- Last-minute exam preparation.
- Quick GATE revision.
- Placement interview readiness.
Keep revisiting this sheet whenever you need a fast refresh of compiler concepts.
Author Profile
- At Learners View, we're passionate about helping learners make informed decisions. Our team dives deep into online course platforms and individual courses to bring you honest, detailed reviews. Whether you're a beginner or a lifelong learner, our insights aim to guide you toward the best educational resources available online.
Latest entries
UncategorizedOctober 3, 2025AKTU BTech Important Questions & Notes
Exam Revision NotesSeptember 24, 2025C++ Programming Cheatsheet – STL, OOP Concepts, Syntax
Exam Revision NotesSeptember 22, 2025Java Programming Cheatsheet – Collections, OOP, Exceptions
UncategorizedAugust 28, 2025BTech 1st Year Notes & Cheatsheets (Subject-Wise)