Lecture Note: Introduction to Compiler Construction
1. Definition of Compiler
A compiler is a special program that translates a source code written in a high-level
programming language (like C, Java, or Python) into machine code (binary form) that a
computer’s processor can understand and execute.
2. Difference Between Compiler and Interpreter
Feature | Compiler | Interpreter
--------------- | -------------------------------- | ----------------------------------
Execution | Translates whole code at once | Translates code line by line
Speed | Faster execution | Slower execution
Error Detection | Detects errors after compilation| Detects errors line by line
Example | C, C++ | Python, JavaScript
3. Phases of Compiler Construction
A typical compiler has several phases that work together in sequence:
1. Lexical Analysis (Scanner): Breaks input source code into tokens (e.g., 'int', 'x', '=', '5', ';').
2. Syntax Analysis (Parser): Checks if the token sequence follows grammar rules; builds a
parse tree.
3. Semantic Analysis: Checks for meaningfulness like type checking and variable
declarations.
4. Intermediate Code Generation: Produces an intermediate code.
5. Code Optimization: Improves code efficiency.
6. Code Generation: Converts intermediate code to machine code.
7. Code Linking and Assembly: Combines modules into a single executable.
4. Types of Compilers
- Single Pass Compiler: Scans code once.
- Multi Pass Compiler: Scans code multiple times for better optimization.
- Just-In-Time (JIT) Compiler: Compiles during execution (e.g., Java).
5. Importance of Compiler Construction
- Makes programs run efficiently.
- Deepens understanding of programming languages.
- Useful for building custom programming languages.
- Enhances skills in language design, parsing, and optimization.
6. Common Compiler Construction Tools
- Lex/Yacc (for Lexical and Syntax Analysis)
- ANTLR (Another Tool for Language Recognition)
- LLVM (Low-Level Virtual Machine for code generation)