Symbol Table Management
1. Job
Manage creation, lookup, scope push/pop, and cleanup so every use finds the right declaration and memory is freed on scope exit.
2. Scope Protocol
on `{` / function entry: enter_scope() → push new table
on declaration: insert(name, attrs); if exists in current → duplicate error
on use: lookup(name) innermost-out; if miss → undeclared error
on `}` / exit: run exit actions (assign offsets), discard table (pop)
3. Forward References and Overloading
- Forward decls (functions, labels): insert incomplete entry, backpatch later.
- Overloading: same name, multiple entries distinguished by signature; lookup matches arg types.
- Redefinition vs shadowing: same scope twice = error; inner scope same name = legal shadow.
4. Worked — nested blocks
global: {g}
enter f: +{a} | lookup g→global, a→f
enter block: +{a(inner)} | lookup a→inner (shadows)
exit block: drop inner a; a→f again
exit f: drop f table
5. Exam Points
- Explain enter/exit/insert/lookup protocol — 5 marks, very frequent.
- Shadowing vs redefinition, forward refs — 3-5 marks, frequent.
- Trace lookup in nested scopes — 3 marks, frequent.