CD Notes
Comprehensive guide to symbol tables: implementation strategies (hash tables, trees), entry structure, scope management, attribute storage, update mechanisms, practical compiler implementation with code.
Purpose
A symbol table is a core data structure that maps names (identifiers) to their attributes (type, scope, address, etc.). It enables:
| 1. Name Resolution | Find what identifier refers to |
| 2. Semantic Checking | Verify valid usage |
| 3. Code Generation | Get address/register for identifier |
| 4. Error Reporting | Provide context for errors |
Symbol Table Structure
Symbol Entry
| name | string // Identifier name |
| kind | enum // variable, function, class, constant |
| type | Type // int, float, array, struct, etc. |
| scope_level | integer // 0=global, 1=first nested, ... |
| attributes | { |
| address | memory_location // Where stored in memory |
| size | integer // Bytes allocated |
| initialized | boolean // Has value been set? |
| used | boolean // Has been referenced? |
| declared_line | integer // For error reporting |
| is_constant | boolean // Read-only? |
| visibility | enum // public, private, protected |
| initial_value | value // For constants |
Multiple Symbol Tables (Scope Chain)
| Global Scope Table | {x: int, foo: function} |
| ├─ Function A Scope | {x: local int, y: local int} |
| │ └─ Block 1 Scope | {z: local int} |
| └─ Function B Scope | {a: parameter, b: local int} |
| 1. Check Block 1 | not found |
| 2. Check Function A | found! (local x) |
Implementation Strategies
Strategy 1: Hash Table + Scope Chain
Strategy 2: Tree Structure (BST)
| - Fast lookup | O(log n) |
| - Range queries | Find all symbols in range |
| - Ordered traversal | Process symbols in order |
Symbol Table Operations
Insert Symbol
Lookup Symbol
Update Symbol
Practical Implementation
Semantic Analysis with Symbol Table
Advanced Features
Shadowing Warnings
def add_symbol_with_shadowing_check(name, symbol):
if self.parent and self.parent.lookup(name) is not None:
warning(f"'{name}' shadows outer scope declaration")
self.add_symbol(name, symbol)Unused Variable Detection
Quick Revision Notes
- Symbol table: Maps identifiers to attributes
- Entry: name, type, kind, scope, attributes
- Scope chain: Current scope + parent scopes
- Lookup: Search chain from innermost to global
- Insert: Add to current scope only
- Hash table: Fast O(1) lookup
- Scope level: Tracks nesting depth
Interview Q&A
Q1: Why use scope chains instead of flat symbol table?
A: Scope chains enable shadowing (inner scope can hide outer names) and automatic cleanup when scope exits. Flat tables require manual namespace management and cannot express nested scopes naturally.
Q2: How do hash table collisions affect symbol table performance?
A: Collisions slow lookup from O(1) to O(chain_length). Good hash function and load factor control minimize collisions. Linear probing or chaining handles collisions. Typically O(1) amortized in practice.
Q3: How do you handle function overloading in symbol tables?
A: Store multiple symbols with same name but different signatures (parameter types). Lookup includes signature matching: find symbol matching name + parameter types. Many languages use separate lookup_function(name, types).
Q4: What's the difference between symbol table and name mangling?
A: Symbol table: runtime data structure for compiler. Name mangling: encoding names to avoid conflicts (C++ mangles function names based on signature). Related: mangling helps generate unique names for symbol table.
Q5: How do you handle forward declarations in symbol tables?
A: Two-pass approach: first pass collects all declarations (create symbol table entries), second pass checks usage. Or: create placeholder symbols on first mention, verify on definition.
Q6: How do symbol tables support code generation?
A: Symbol table provides: memory addresses for variables, function entry points, type information for type-specific instructions. Code generator queries symbol table to get this info during translation.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Symbol Table: Core Data Structure for Semantic Analysis and Code Generation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, semantic, analysis, symbol, table
Related Compiler Design Topics