Skip to content

Symbol Table

A Symbol Table is a data structure used by a compiler to store information about identifiers encountered during compilation.

Identifier → variable names, function names, class names, array names, etc.

Each entry in a symbol table contains:

  • Name of identifier
  • Type (int, float, function, array, etc.)
  • Scope (local, global, block)
  • Memory location / offset
  • Size
  • Additional attributes (parameters, return type, access specifier, etc.)

Symbol table helps the compiler to:

  • Check semantic correctness
  • Perform type checking
  • Detect undeclared variables
  • Detect multiple declarations
  • Allocate memory
  • Support scope resolution

  • Adds a new identifier entry
  • Error if identifier already exists in same scope
  • Finds an identifier
  • Searches current scope first, then outer scopes
  • Removes identifiers when scope ends
  • Modifies attributes like value, type, memory address

FieldDescription
NameIdentifier name
TypeData type
ScopeLevel of visibility
AddressMemory location
SizeMemory required
Line NoDeclaration line
ParametersFor functions

  • Simple implementation
  • Search time: O(n)

Advantages

  • Easy to implement

Disadvantages

  • Slow lookup

  • Identifiers stored in lexicographical order
  • Average search time: O(log n)
  • Worst case: O(n) (skewed tree)

Advantages

  • Sorted order maintained

Disadvantages

  • Unbalanced BST degrades performance

  • Self-balancing
  • Search/Insert/Delete: O(log n)

Used when ordering + performance both required


  • Identifier → hash function → index
  • Average search: O(1)
  • Worst case: O(n) (collision)

Collision Resolution

  • Chaining
  • Open Addressing (Linear / Quadratic / Double Hashing)

Why preferred?

  • Fast lookup
  • Efficient for large symbol tables

  • Inner scope hides outer scope identifiers
  • Local variables have higher priority
  • New table for each block
  • Tables destroyed when scope ends
  • Push on scope entry
  • Pop on scope exit
  • Each entry has scope number
  • Highest scope searched first

Static ScopingDynamic Scoping
Scope decided at compile timeScope decided at runtime
Used in C, C++, JavaRarely used
Symbol table sufficientRequires runtime stack

  • Undeclared variable
  • Multiple declarations
  • Type mismatch
  • Function argument mismatch
  • Scope violation

Symbol TableLiteral Table
Stores identifiersStores constants
Created by compilerCreated by assembler

  • Hash table is most preferred implementation
  • BST gives sorted order but may degrade
  • Balanced BST ensures O(log n) time
  • Symbol table is mainly used in semantic analysis
  • Scope handling is frequently asked

  1. Which data structure is best for symbol table? → Hash Table
  2. Can symbol table be implemented using BST? → Yes
  3. Which compiler phase uses symbol table? → Lexical + Semantic Analysis
  4. When are entries deleted? → At scope exit

Symbol Table is a compiler data structure that stores information about identifiers and supports efficient lookup, insertion, and scope management during compilation.


Want PYQs + memory tricks + diagrams next? 😄

Section titled “Want PYQs + memory tricks + diagrams next? 😄”