0
0
Compiler Designknowledge~6 mins

Phases of compilation in Compiler Design - Full Explanation

Choose your learning style9 modes available
Introduction
Turning human-written code into something a computer can run is tricky. This process happens in steps, each solving a part of the problem to make the code ready for the machine.
Explanation
Lexical Analysis
This phase reads the raw code and breaks it into small pieces called tokens. Tokens are like words in a sentence, such as keywords, numbers, or symbols. It removes spaces and comments that the computer doesn't need.
Lexical analysis turns raw code into meaningful tokens for the next phase.
Syntax Analysis
Here, the compiler checks if the tokens follow the language rules, like grammar in a sentence. It builds a tree structure called a parse tree that shows how the tokens fit together. If the code breaks the rules, errors are reported.
Syntax analysis ensures the code structure follows language grammar.
Semantic Analysis
This phase checks the meaning of the code. It verifies things like variable types and whether variables are declared before use. It also builds a symbol table to keep track of identifiers and their information.
Semantic analysis confirms the code makes sense logically and meaningfully.
Intermediate Code Generation
The compiler creates a simple, generic version of the code that is easier to work with. This intermediate code is not tied to any specific machine, making it easier to optimize and translate later.
Intermediate code acts as a bridge between source code and machine code.
Code Optimization
This phase improves the intermediate code to run faster or use less memory. It removes unnecessary steps and simplifies operations without changing what the code does.
Code optimization makes the program more efficient without altering its behavior.
Code Generation
Finally, the compiler translates the optimized intermediate code into machine code that the computer's processor can execute. This code is specific to the target hardware.
Code generation produces the final machine code for the computer to run.
Symbol Table Management
Throughout compilation, the compiler maintains a symbol table that stores information about variables, functions, and objects. This helps in checking correctness and generating code.
Symbol table management tracks all identifiers and their details during compilation.
Real World Analogy

Imagine writing a recipe in your language and wanting to share it with a friend who only understands a special cooking language. First, you break your recipe into words, then check if the words are in the right order. Next, you make sure the ingredients and steps make sense. Then, you rewrite the recipe in a simple, clear way. After that, you improve it to be quicker to cook. Finally, you translate it into your friend's cooking language.

Lexical Analysis → Breaking the recipe into individual words.
Syntax Analysis → Checking if the recipe's instructions follow proper order and grammar.
Semantic Analysis → Verifying the ingredients and steps make sense and are valid.
Intermediate Code Generation → Rewriting the recipe in a simple, clear format.
Code Optimization → Improving the recipe to cook faster or use fewer ingredients.
Code Generation → Translating the recipe into your friend's cooking language.
Symbol Table Management → Keeping a list of all ingredients and tools used in the recipe.
Diagram
Diagram
┌─────────────────────┐
│   Source Code       │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│ Lexical Analysis    │
│ (Tokens)           │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│ Syntax Analysis     │
│ (Parse Tree)        │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│ Semantic Analysis   │
│ (Symbol Table)      │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│ Intermediate Code   │
│ Generation          │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│ Code Optimization   │
└─────────┬───────────┘
          │
          ▼
┌─────────────────────┐
│ Code Generation     │
│ (Machine Code)      │
└─────────────────────┘
This diagram shows the step-by-step flow of source code through each compilation phase, ending in machine code.
Key Facts
TokenA small unit of code like a keyword, identifier, or symbol identified during lexical analysis.
Parse TreeA tree structure representing the grammatical structure of the source code.
Symbol TableA data structure that stores information about variables, functions, and identifiers.
Intermediate CodeA simplified code representation between source code and machine code.
Code OptimizationThe process of improving code efficiency without changing its output.
Machine CodeBinary instructions that a computer's processor can execute directly.
Common Confusions
Believing lexical analysis checks code meaning.
Believing lexical analysis checks code meaning. Lexical analysis only breaks code into tokens; meaning is checked later during semantic analysis.
Thinking code optimization changes what the program does.
Thinking code optimization changes what the program does. Optimization improves performance but does not alter the program's behavior or output.
Assuming intermediate code is the final machine code.
Assuming intermediate code is the final machine code. Intermediate code is a temporary, generic form; it must be translated into machine code in the final phase.
Summary
Compilation breaks down the process of turning code into machine instructions into clear, manageable steps.
Each phase focuses on a specific task, from reading code to checking rules, understanding meaning, and finally producing efficient machine code.
Understanding these phases helps in grasping how programming languages work behind the scenes.