Chapter 7: Intermediate representation and symbol table

Symbol Table

. Compiler uses symbol table to keep track of scope and binding information about names

. symbol table is changed every time a name is encountered in the source; changes to table occur

- if a new name is discovered

- if new information about an existing name is discovered

. Symbol table must have mechanism to:

- add new entries

- find existing information efficiently

. Two common mechanism:

- linear lists, simple to implement, poor performance

- hash tables, greater programming/space overhead, good performance

. Compiler should be able to grow symbol table dynamically

. if size is fixed, it must be large enough for the largest program

A compiler uses a symbol table to keep track of scope and binding information about names. It is filled after the AST is made by walking through the tree, discovering and assimilating information about the names. There should be two basic operations - to insert a new name or information into the symbol table as and when discovered and to efficiently lookup a name in the symbol table to retrieve its information.

Two common data structures used for the symbol table are -

1. Linear lists:- simple to implement, poor performance.

2. Hash tables:- greater programming/space overhead, good performance.

Ideally a compiler should be able to grow the symbol table dynamically, i.e., insert new entries or information as and when needed. But if the size of the table is fixed in advance then ( an array implementation for example), then the size must be big enough in advance to accommodate the largest possible program.