• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Translation from high-level source to low-level target code is too complex for a single monolithic step. Without a phased approach, error handling, optimization, and code generation would be entangled, making compilers impossible to build, maintain, or reason about.

Core Idea

The compilation process is divided into a sequence of phases, each performing a specific transformation. The six standard phases are: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. These phases are grouped into the front-end (analysis) and back-end (synthesis).

How It Works

The source program passes through each phase sequentially. The output of one phase becomes the input to the next. Lexical analysis produces tokens. Syntax analysis produces a parse tree. Semantic analysis checks type consistency and augments the syntax tree. Intermediate code generation produces a machine-independent IR. Code optimization improves the IR. Code generation produces the target machine code.

Visual Explanation

compiler_phases Source Source Code Phase1 1. Lexical Analysis (Scanner) Source->Phase1 Phase2 2. Syntax Analysis (Parser) Phase1->Phase2 Phase3 3. Semantic Analysis Phase2->Phase3 Phase4 4. Intermediate Code Generation Phase3->Phase4 Phase5 5. Code Optimization Phase4->Phase5 Phase6 6. Code Generation Phase5->Phase6 Target Target Code Phase6->Target

Key Properties

  • Phases are independent: Each phase has a well-defined input and output
  • Front-end: Phases 1-3 (source-language dependent)
  • Back-end: Phases 4-6 (target-machine dependent)
  • Symbol table: All phases interact with a shared symbol table
  • Error handling: Each phase can detect and report errors

Connections

Edge Cases & Gotchas

  • Phases vs Passes: A single pass can combine multiple phases (e.g., lexical and syntax analysis often interleave)
  • Phase ordering: Code optimization can span multiple passes or even be optional for simple compilers
  • Symbol table access: All phases read/write the symbol table — it is not a phase but a supporting data structure used throughout