Chapter 1: Introduction to Compiler Design
1.1 Introduction and Motivation
- Definition: A compiler is an executable computer program that reads a source program written in a high-level language and translates it into an equivalent program in a target language (usually machine language or intermediate/object code).
- Interdisciplinary Nature: Compiler design combines concepts from multiple Computer Science fields, including Programming Languages, Data Structures & Algorithms, Theory of Computation (Automata), Computer Architecture, and Operating Systems.
- Why Study Compilers?
- To develop advanced compilers for emerging computer architectures.
- To write more efficient high-level code.
- To understand parsing, which helps in building tools like text editors, debuggers, and preprocessors.
- It serves as an excellent "capstone" project that ties together various CS concepts.
1.2 Classification of Compilers
Historically, compilers were split into smaller programs that made "passes" over the source code because early computers lacked the memory to hold a massive, unified compiler program.
- 1.2.1 Single (One) Pass Compilers: Processes input exactly once, going directly from lexical analysis to code generation.
- Pros/Cons: Faster and requires less memory, but highly inefficient at optimizing code. (Also called narrow compilers, e.g., Pascal).
- 1.2.2 Multi-Pass Compilers: Processes the source code or syntax tree several times. Each pass takes the output of the previous phase as input.
- Pros: Requires less memory per pass and supports complex languages (e.g., Java). (Also called wide compilers).
- 1.2.3 Load and Go Compilers: Translates source code directly into absolute code and executes it immediately in memory (e.g., WATFOR).
- 1.2.4 Optimizing Compilers: Focuses on minimizing or maximizing specific attributes (e.g., execution time, memory usage, or power consumption). This is achieved through algorithm transformations.
1.3 Cousins of Compilers
- Assembler: Converts assembly language (mnemonic operation codes) into machine code.
- Interpreter: Translates and executes high-level instructions on the fly as they are encountered. Unlike a compiler (which produces a saved executable file), an interpreter outputs data immediately during execution.
- Linker: Takes one or more object files generated by a compiler and combines them into a single executable program.
- Loader: A part of the OS responsible for loading executables into memory, preparing them, and executing them.
1.4 Basic Compiler Architecture
Compilers are massive programs, so they are built using modular design (decomposition).
- Front End (Analysis): Handles language-specific tasks (Lexer, Parser, Semantic Analyzer).
- Back End (Synthesis): Handles platform/hardware-specific tasks (Optimizer, Code Generator). By separating these, you can use the same Front End for a C++ compiler on Windows and Mac, changing only the Back End.
The Analysis-Synthesis Model:
- Analysis: Breaks the source program into constituent pieces and creates an intermediate representation (a hierarchical structure called a tree).
- Synthesis: Takes the tree structure and translates the operations into the final target program.
1.5 Phases of a Compiler
Compilation is divided into six distinct sequential phases:
- Phase I: Lexical Analysis (Scanning)
- Reads the code as a stream of characters and chunks them into meaningful units called tokens.
- Ignores whitespace, tabs, and comments. Uses Regular Expressions.
- Phase II: Syntax Analysis (Parsing)
- Takes tokens and builds a Parse Tree (or Abstract Syntax Tree) based on Context-Free Grammar (CFG).
- Ensures the sequence of tokens forms a syntactically correct program and catches syntax errors.
- Phase III: Semantic Analysis
- Checks the "meaning" of the code (e.g., ensuring variables are declared before use, type-checking).
- Relies heavily on the Symbol Table (a data structure storing identifiers, variable types, scopes, etc.).
- Phase IV: Intermediate Code Generation
- Translates the semantic tree into an intermediate language.
- A popular format is Three-Address Code (e.g.,
x = y op z).
- Phase V: Code Optimization
- Replaces inefficient instruction sequences with better ones.
- Local Optimization: Looks at small blocks of code (constant evaluation, eliminating unnecessary operations).
- Global Optimization: Looks at the entire program (very difficult but highly efficient).
- Phase VI: Code Generation & Linking
- Converts the optimized intermediate code into actual assembly/machine code.
- Assigns memory, registers, and links appropriate language libraries via a linker.
1.6 Compiler Construction Tools
Modern software tools exist to automatically generate phases of a compiler, making the process much easier.
- Tool Types: Scanner generators, Parser generators, Syntax-directed translation engines, Automatic code generators.
- Popular Tools for C/C++: Flex & Lex (Scanners); Bison & YACC (Parsers).
- Popular Tools for Java: JLex & JFlex (Scanners); CUP, BYACC/J, & JavaCC (Parsers).