Compiler_Concepts
Compiler_Concepts
1. Compiler
A compiler is a system software that translates high-level programming language (HLL) code into machine
code (object code) before execution.
It processes the entire source code at once, generating an executable file.
Working of a Compiler:
1. Lexical Analysis - Converts the code into tokens.
2. Syntax Analysis - Checks for grammatical errors.
3. Semantic Analysis - Ensures logical correctness.
4. Intermediate Code Generation - Converts to an intermediate representation.
5. Optimization - Improves efficiency.
6. Code Generation - Produces machine code.
7. Code Linking & Loading - Produces the final executable.
Advantages:
- Faster execution since the entire code is compiled at once.
- Optimized performance due to code optimization.
Disadvantages:
- Debugging is difficult as all errors are shown after compilation.
2. Lexical Analysis
Lexical Analysis is the first phase of compilation, where the source code is converted into tokens. It removes
comments and whitespace while grouping
characters into meaningful sequences.
Example:
For the code:
int a = 10;
3. Preprocessor
A preprocessor processes the source code before actual compilation. It handles macros, file inclusion, and
conditional compilation.
Functions of Preprocessor:
1. File Inclusion (#include) - Adds library files.
2. Macro Expansion (#define) - Replaces macros with values.
3. Conditional Compilation (#ifdef) - Allows selective code compilation.
4. Removing Comments - Simplifies compilation.
Example:
#include <stdio.h>
#define PI 3.14
4. Assembler
An assembler converts assembly language into machine code (binary).
Machine Code:
B0 02
04 03
Types of Assemblers:
- One-Pass Assembler - Translates in a single pass.
- Two-Pass Assembler - Resolves addresses in two passes.
5. Interpreter
An interpreter translates and executes code line-by-line instead of compiling the whole program at once.
Advantages:
- Easy debugging as errors are detected immediately.
- No separate compilation step required.
Disadvantages:
- Slower execution than compiled programs.
Example:
Python Code:
print("Hello, World!")
The interpreter executes each line directly.
6. Linker
A linker combines multiple object files into a single executable file. It resolves references and addresses.
Functions of a Linker:
1. External Symbol Resolution - Links function calls.
2. Address Relocation - Adjusts memory addresses.
3. Library Linking - Adds standard functions like printf().
Types:
- Static Linker - Combines all files permanently.
- Dynamic Linker - Uses external libraries at runtime.
7. Loader
A loader loads the compiled program into memory for execution.
Types:
- Absolute Loader - Uses fixed memory addresses.
- Relocatable Loader - Adjusts addresses dynamically.
8. Compiler vs Interpreter
Feature | Compiler | Interpreter
------------------ | -------------------------------- | --------------------------------
Execution | Translates entire code at once | Translates line-by-line
Speed | Faster | Slower
Debugging | Difficult | Easier
Output | Generates an executable file | No separate executable file
Example | C Compiler (GCC) | Python Interpreter