What are the phases of a compiler

The compilation process is a sequence of various phases that transforms the source program from one representation to another. Each phase takes input from its previous stage,  and feeds its output to the next phase of the compiler. 

Phases of compiler are

i) Lexical analysis.
ii) Syntax analysis.
iii) Intermediate code generation.
iv) Code optimization.
v) Code generation.

Lexical analysis: This is the initial part of reading and analysing the program text: The text is read and divided into tokens, each of which corresponds to a symbol in the programming language, e.g., a variable name, keyword or number.
Syntax analysis: This phase takes the list of tokens produced by the lexical analysis and arranges these in a tree-structure (called the syntax tree) that reflects the structure of the program. This phase is often called parsing. 
Semantic Analysis:  This phase analyses the syntax tree to determine if the program violates certain consistency requirements, e.g., if a variable is used but not declared or if it is used in a context that does not make sense given the type of the variable, such as trying to use a boolean value as a function pointer. Type-checking is an important part of semantic analyzer.
Intermediate code generation: The program is translated to a simple machine- independent intermediate language. 
Code Optomizer: The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space.
Code Generation: is the last and final phase of a compiler. It gets inputs from code optimization phases and produces the page code or object code as a result. The objective of this phase is to allocate storage and generate relocatable machine code.

Symbol Table Management

  • This record variable name used in the source p
  • This stores attributes of each name
    • Eg: name, its type, its scope
    • Method of passing each argument(by value or by reference)
    • Return type

Implementation of symbol table can be done is either linear list or hash table.

Error Handling Routine:In the compiler design process error may occur in all the below-given phases:

  • Lexical analyzer: Wrongly spelled tokens
  • Syntax analyzer: Missing parenthesis
  • Intermediate code generator: Mismatched operands for an operator
  • Code Optimizer: When the statement is not reachable
  • Code Generator: Unreachable statements
  • Symbol tables: Error of multiple declared identifiers

Leave a Reply