Symbol Table
1. Purpose
The symbol table maps each identifier to its meaning: type, scope, address, size, lifetime. Consulted in lexical (insert), syntax/semantic (type check), code gen (address) phases. Without it, x is just letters.
2. Core Operations
| Op | Job |
|---|---|
| insert(name, info) | add on declaration; error on duplicate in same scope |
| lookup(name) | find innermost visible binding; error if undeclared |
| enter/exit scope | push/pop scope on { / } |
| update | fill address/type after analysis |
3. Scope Rule (most-tested)
Innermost binding wins. Search current scope → enclosing scopes → global. Shadowing allowed; use-before-declare rejected.
{ int x=1; { float x=2.5; print(x); } print(x); }
// inner prints 2.5, outer prints 1
4. When Used Across Phases
lex: insert lexeme string
parse/semantic: insert decl, lookup uses, check types
IR/codegen: read offset/width, emit loads/stores
5. Exam Points
- Define symbol table + operations — 5 marks, very frequent.
- Scope resolution / shadowing example — 3-5 marks, frequent.
- Role in each compiler phase — 3 marks, frequent.