Compiler Construction Notes
Compiler Construction Notes
Lecture 03
Symbol Table
Content Taken From
• Compilers Principles, Techniques and Tools by Alfred V. Aho, Ravi Sethi and Jeffry D. Ulman
Web URL:
http://syllabus.cs.manchester.ac.uk/ugt/2019/COMP36512/2017/
Agenda
01 Symbol Table
• We need to be able to bind a name to a piece of information. In case the name is already defined in the s
ymbol table, the new binding takes precedence over the old.
• We need to be able to look up a name in a symbol table to find the information the name is bound to. If th
e name is not defined in the symbol table, we need to be told that.
• We need to be able to exit a scope, reestablishing the symbol table to what it was before the scope was
entered.
Who creates Symbol Table Entities?
Symbol-table entries are created and used during the analysis phase by the lexical
analyzer, the parser, and the semantic analyzer.
What kind of information might the compiler need about each item:
textual name, data type, declaring procedure, storage information. Depending on t
he type of the object, the compiler may want to know list of fields (for structures), n
umber of parameters and types (for functions), etc…
In practice, many different tables may exist.
Symbol table information is accessed frequently:
hence, efficiency of access is critical!
Organising the symbol table
Linear List:
Simple approach, has no fixed size; but inefficient: a lookup may need to traverse the e
ntire list: this takes O(n).
Binary tree:
An unbalanced tree would have similar behaviour as a linear list (this could arise if sym
bols are entered in sorted order).
A balanced tree (path length is roughly equal to all its leaves) would take O(log2n) probe
s per lookup (worst-case). Techniques exist for dynamically rebalancing trees.
Hash table:
Uses a hash function, h, to map names into integers; this is taken as a table index to st
ore information. Potentially O(1), but needs inexpensive function, with good mapping pr
operties, and a policy to handle cases when several names map to the same single ind
ex.
Bucket hashing (open hashing)
A hash table consisting of a fixed array of m p
ointers to table entries.
foo... qq...
Table entries are organised as separate linke
d lists called buckets.
Use the hash function to obtain an integer fro
m 0 to m-1.
As long as h distributes names fairly uniforml i...
y (and the number of names is within a small
constant factor of the number of buckets), bu
cket hashing behaves reasonably well.
Linear Rehashing (open addressing)
Use a single large table to hold records. Whe a
n a collision is encountered, use a simple tec
hnique (i.e., add a constant) to compute subs
equent indices into the table until an empty sl rehash
ot is found or the table is full. If the constant i b
s relatively prime to the table size, this, event
ually, will check every slot in the table.
The problems:
at point x, which declaration of variable y is current?
as parser goes in and out of scopes, how does it track y? allocate and initialise a
symbol table for each level!
Exercises
Explain why we use Symbol Table?