Understanding Compiler Phases and Structure

Aug 27, 2024

Phases of Compiler

Introduction

  • Overview of the compiler design and its structure.
  • Previous discussion on language processing systems, converting high-level language to low-level language (assembly/machine code).

Compiler Phases

  • Definition of a Phase:
    • A phase is a logically interrelated operation that takes a source program in one representation and produces output in another representation.
    • Each phase takes input and gives output to the next phase.

Major Phases of Compilation

  • Total compilation is divided into two major phases:
    1. Analysis Phase
    2. Synthesis Phase

Analysis Phase

  • Characteristics:
    • Machine independent.
    • Language dependent.
  • Focuses on the source language.

Synthesis Phase

  • Characteristics:
    • Machine dependent.
    • Language independent.
  • Focuses on the machine language (low-level language).

Structure of the Compiler

  1. Source Program: High-level language code.
  2. Target Program: Low-level language code (after compilation).
  3. Phases of Compilation:
    • Lexical Analyzer
    • Syntax Analyzer
    • Semantic Analyzer
    • Intermediate Code Generator
    • Code Optimizer
    • Code Generator
  4. Symbol Table Manager:
    • Stores symbol-related information.
    • Handles errors that may occur during any phase.

Detailed Explanation of the First Phase: Lexical Analysis

  • Function:
    • Reads a stream of characters from the source program.
    • Groups characters into meaningful sequences called lexemes.
  • Representation:
    • Lexical Analyzer represents lexemes as tokens.

Example of Lexical Analysis

  • Example Statement: new_value = old_value + 12
    • Tokens generated:
      • new_value -> Identifier
      • = -> Assignment Operator
      • old_value -> Identifier
      • + -> Add Operator
      • 12 -> Number
  • Additional Operations:
    • Removes white spaces and identifies errors.

Conclusion

  • The first phase of the compiler, Lexical Analysis, has been discussed.
  • Further phases will be explained in the next session.