1. Gyanastra
  2. Compiler Design
  3. Phases of a Compiler
  4. Overview of Compiler Phases and the Role of Preprocessor
Free lesson, no account needed 4 min read 2 practice questions after it

Compiler Design · Phases of a Compiler

Overview of Compiler Phases and the Role of Preprocessor

Overview

A compiler is software that converts a high-level language into a low-level language, working through a series of phases.
Before the compiler proper begins, a preprocessor turns a high-level language program (one containing directives such as #include and #define) into a pure high-level language program that the compiler phases then process.

Key concepts

  • Compiler: software whose main aim is to convert a high-level language into a low-level language.
  • Phases: the compiler works through a series of phases — lexical analysis, syntax analysis (parsing), and the later phases that produce the parse tree, three-address code, optimization, and target code generation.
    All phases are explained using a single example.
  • Lexical analysis: the only phase that actually reads the text of the source program. The other phases work on the tokens/structure produced from it; the syntax analyzer works on tokens, not raw text.
  • Front end and back end: the compiler has a front end (the early phases such as lexical analysis and syntax analysis) and a back end.
    When moving a compiler to a new machine, the front-end phases remain unchanged; only the back-end (target-dependent) phases such as code generation must be changed.
  • Supporting software modules: many other software modules help in the translation process. The first of these, sitting before the compiler proper, is the preprocessor.
  • High-level language program: a program that contains preprocessor directive lines such as #include or #define (for example, the starting lines of a C program).
  • Pure high-level language program: the program produced after the preprocessor removes those directive lines.
  • File inclusion: the operation of handling #include by including the file — the preprocessor substitutes the entire contents of the file to be included into the source program.
  • #define: used to define a constant (e.g., a bank interest value) so that its value is written in one place; the preprocessor handles the substitution of such defined names.

How it works

  1. The high-level language program is given as input to the preprocessor.
  2. The preprocessor converts that high-level language into pure high-level language by removing the directive lines (#include, #define).
  3. The pure high-level language is passed on to the compiler phases: lexical analysis, syntax analysis, and the remaining phases.

Why programs are not written directly in low-level language:

  • We are not comfortable writing programs in zeros and ones (the binary language).
  • We are comfortable writing programs in English or an English-like language, and then compiling with software that converts that high-level language into low-level language.
Figure. The phases of a compiler. The first three (gold) analyse the source: lexical, syntax and semantic analysis. The rest (navy) synthesise the target program. The symbol table and the error handler are shared by all phases.

Worked examples

A C program whose starting lines contain #include or #define is a high-level language program. The preprocessor removes these lines, producing the pure high-level language program:

  • #include is handled by file inclusion: the entire contents of the included file are substituted into the source program.
  • #define defines a constant (e.g., a bank interest value) so its value is written in one place; the preprocessor substitutes the defined name.

Revision summary

  • Lexical analysis is the only phase that reads the program text; the syntax analyzer works on tokens.
  • To retarget the compiler to a new machine, change only the back-end (target-dependent) phases; the front end stays the same.
  • The preprocessor is the first module, sitting before the compiler phases.
  • A program with #include / #define lines is a high-level language program; the preprocessor removes these lines to produce pure high-level language.
  • #include handling is called file inclusion — the whole included file is substituted into the source program.
  • #define exists so a constant's value is written in only one place.

Previous-year exam insights

  • The phases of a compiler in order are: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation; symbol-table management and error handling are supporting activities performed throughout all phases.
  • The front end consists of lexical, syntax and semantic analysis plus intermediate code generation and is largely machine-independent; the back end consists of optimization and target code generation and is machine-dependent.
  • Lexical analysis (the scanner) groups characters of the source into tokens; syntax analysis (the parser) checks the token stream against the grammar and builds a parse tree; semantic analysis checks type/meaning consistency.
  • The preprocessor performs file inclusion (#include), macro expansion (#define), conditional compilation (#if/#ifdef), and line control; it produces an expanded source program that is the true input to the compiler.
  • A compiler translates the whole program before execution, whereas an interpreter translates and executes statement by statement; a hybrid system (e.g., Java) first compiles to intermediate bytecode which is then interpreted/JIT-compiled.
  • The symbol table stores information about identifiers (name, type, scope, memory location) and is consulted/updated by nearly every phase, from lexical analysis to code generation.
Pages 1–2 of 2