Documente Academic
Documente Profesional
Documente Cultură
The job of the symbol table is to store all the names of the program and information about each name In block structured languages, roughly speaking, the symbol table collects information from declarations and uses that information whenever a name is used later in the program
this information could be part of the syntax tree, but is put into a table for efficient access to names If there are different occurrences of the same name, the symbol table assists in name resolution
Either the parser or the lexical analyzer can do the job of inserting names into the symbol table (as long as scope information is given to the lexer)
Information can be added throughout compilation
1
Arrays
Also needs number of dimensions Upper and lower bounds of each dimension
Lookups occur a lot more often than insert Hash tables most often chosen to represent symbol tables as they have good time complexity for lookup
0 1
name
Hash function
Scope Analysis
The scope of a name is tied to the idea of a block in the programming language
Standard blocks (statement sequences, sometimes if statement) Procedures and functions Program (global program level) Universe (predefined functions, etc.)
Names must be unique within the block in which they are declared (no two objects with the same name in one block)
There are some languages with exceptions for different types
Name resolution: a use of a name should refer to the most local enclosing block that has a declaration for that name.
Parts of the table not currently active may be kept for future semantic analysis
The symbol tables shown so far are all for the program being compiled, also needed is a way to look up names in the universe
System-defined names
Alternate Representation
The lists of hash tables can be inefficient for lookup since the system has to search up the list of lexical levels
More names tend to be declared at level 0, thus making the most common occurrence be the most expensive
An optimization of the symbol table as lists of hash tables is to keep one giant hash table
Within that table each name will have a list of occurrences identified by lexical level
Static Scope
The scoping system described so far assumes that the scope rules are for static scoping
The static problem layout of enclosing blocks determines the scoping of a name
Object-Oriented Scoping
Languages like Java must keep symbol tables for
The code being compiled Any external classes that are known and referenced inside the code The inheritance hierarchy above the class containing the code
One method of implementation is to attach a symbol table to each class with two nesting hierarchies
One for lexical scoping inside individual methods One to follow the inheritance hierarchy of the class
Resolving a name
First consult the lexically scoped symbol table If not found, search the classes in the inheritance hierarchy If not found, search the global name space Consists of the current package and any imported packages
10
Testing cases
Include all types of correct declarations Incorrect cases may include Definition of an ambiguous name Definition without a name Meaningless recursive definitions (in some types of structures) References to an undefined name
11
References
Keith Cooper and Linda Torczon, Engineering a Compiler, Elsevier, 2004. Kenneth C. Louden, Compiler Construction: Principles and Practices, PWS Publishing, 1997. Per Brinch Hansen, On Pascal Compilers, Prentice-Hall, 1985. Out of print. Aho, Lam, Sethi, and Ullman, Compilers: Principles, Techniques, and Tools. Addison-Wesley, 2006. (The purple dragon book)
12