A symbol table is a data structure used by a compiler to store information about identifiers such as variables, functions, constants, and parameters in a program.
- Stores information such as the name, type, scope, and memory location of each identifier.
- Is created and updated during different phases of compilation.
- Supports error detection, scope resolution, code optimization, and code generation.
Role of Symbol Table in Compiler Phases
The symbol table is accessed and updated during different compilation phases.
1. Lexical Analysis
- Identifies keywords, identifiers, and other tokens.
- Recognizes identifier names and may record them in the symbol table.
2. Syntax Analysis
- Uses identifier information while analyzing program structure.
- Supports the association of declarations with their corresponding identifiers.
- Helps maintain scope-related information.
3. Semantic Analysis
- Checks whether identifiers are declared before use.
- Performs type checking and validates type compatibility.
- Checks function parameters and return types.
- Detects duplicate declarations and scope-related errors.
4. Intermediate Code Generation
- Retrieves identifier types and other attributes.
- Uses symbol information to generate intermediate code.
- Maintains information about temporary variables when required.
5. Code Optimization
- Uses identifier and type information during optimization.
- Supports constant propagation and dead code elimination.
- Helps analyze variable usage and data flow.
6. Target Code Generation
- Uses type, size, and storage information to generate target instructions.
- Determines memory locations and offsets.
- Supports register and memory allocation.
Example of Using Symbol Table
Let us consider the following program
#include <stdio.h>
const float pi = 3.14159f;
float calculateArea(float radius) {
return pi * radius * radius;
}
int main() {
float distance = 5;
float area = calculateArea(distance);
printf("Area of circle with radius %.2f = %.5f\n", distance, area);
return 0;
}
Key identifiers involved:
- distance (variable in main)
- pi (constant)
- radius (parameter in calculateArea)
- calculateArea (function)
A symbol table of this program would look like the following.
| Name | Type | Scope | Category | Additional Info |
|---|---|---|---|---|
| pi | float | Global | Constant | Read-only |
| calculateArea | float | Global | Function | Returns float |
| radius | float | Local (Function) | Parameter | Scope: calculateArea |
| distance | float | Local (main) | Variable | Stack allocated |
| area | float | Local (main) | Variable | Stack allocated |
Implementation
Symbol tables can be implemented using various data structures, each with its trade-offs:
- Hash Tables: Offer fast lookup times, making them suitable for large programs.
- Binary Search Trees: Maintain sorted order, which can be beneficial for certain compiler operations.
- Linear Lists: Simpler to implement but less efficient for large numbers of identifiers.
Applications
- Name Resolution: Identifies variables and functions with their types and memory locations.
- Scope Management: Resolves naming conflicts and manages local/global scopes.
- Code Optimization: Provides information useful for improving execution efficiency.
- Code Generation: Supplies data types and storage details for machine code generation.
- Error Detection: Helps detect undeclared variables, type mismatches, and duplicate declarations.

