A compiler is a software program that translates source code written in a high-level programming language into a lower-level form that can be executed by a computer.
- It checks the source code for errors and reports them during compilation.
- It converts the source program into an executable or intermediate form that can be processed by the target system.

Working
A compiler processes the source program through several phases. Each phase performs a specific task, such as identifying tokens, checking syntax and meaning, optimizing the code, and generating target code.
Lexical Analysis
Lexical analysis is the first phase of compilation. The lexical analyzer (lexer) reads the source code and divides it into meaningful units called tokens.
Example
int sum = a + b;
int, sum, =, a, +, b, and ; are identified as tokens.
Syntax Analysis
- Syntax analysis checks whether the tokens follow the grammar rules of the programming language.
- The parser performs this analysis and builds a structure such as a Parse Tree or Abstract Syntax Tree (AST).
- If the source code violates the language syntax, the compiler reports a syntax error.
Semantic Analysis
Semantic analysis checks whether the program is logically meaningful according to the rules of the language.
It detects errors such as:
- Using an undeclared variable
- Incompatible data types
- Invalid operations
- Incorrect use of functions or operators
Intermediate Code Generation
- After semantic analysis, the compiler converts the source program into an intermediate representation (IR).
- The intermediate representation provides a form that can be used for optimization and later converted into the target code.
Code Optimization
Code optimization improves the intermediate code to make the generated program more efficient.
It may:
- Remove unnecessary code
- Reduce the number of instructions
- Improve execution speed
- Reduce memory usage
Optimization is generally performed without changing the program's intended behavior.
Code Generation
In the final major phase, the compiler converts the intermediate representation into target code.
Depending on the language and compiler, the target can be:
- Machine code
- Assembly code
- Bytecode
- Another intermediate form
Types of Compilers
- Cross Compiler: A cross compiler runs on one platform but generates code for a different platform.
- Bootstrap Compiler: Used to develop a compiler for the same language or compiler system, often leading to a self-hosting compiler.
- Transcompiler: Converts source code from one high-level programming language into another high-level language.
- JIT Compiler: Compiles code during program execution instead of compiling the entire program beforehand.
Advantages
- Faster Execution: Compiled programs can execute efficiently because the source code is translated before execution.
- Error Detection: Compilers detect syntax, semantic, and type-related errors during compilation.
- Code Optimization: Compilers optimize the generated code to improve performance and reduce resource usage.
- Improved Security: Compile-time checks can detect certain invalid operations and potential programming errors before execution.