Resolving Variables

Recall Semantic Analysis as Simplified Interpretation

Even if we associate unique symbols with identifiers,

  • same identifier can denote different variables at different points

This is why symbol table needs to change depending on position in tree

Once we look up identifier, we can directly link it to its definition

  • advantages:
    • even faster than table access
    • does not require having symbol table around
  • disadvantage
    • requires adding mutable reference to identifier nodes in trees
    • works only if trees were constructed without sharing subtrees
    • easier to make mistakes when working with mutable structures

Summary of semantic checks for variable uses:

  • undeclared variables
  • doubly declared variable
  • possible warnings:
    • declared but not used in any statements
    • read but not initialized (checked for local variables in Java)
    • only assigned, but never read
    • shadowed by another variable
    • assignments to parameters and notion of 'readonly' variables