CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?
-
date post
20-Dec-2015 -
Category
Documents
-
view
226 -
download
0
Transcript of CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?
CPSC 325 - Compiler
Tutorial 6 & 7
Symbol Table
What is Symbol Table?
Why Do We Need a Symbol Table?
Bind names to program entities (e.g., variables, procedures).
– set of possible values, i.e. their type. – storage associated with names, i.e., where they are stored. – visibility, i.e., where they can be used.
Usage of Symbol Table:– verification (e.g., number and type of procedure arguments).– code generation (e.g., types and instructions).– debugging (e.g., memory - symbol association).
What Symbol table does?
1. Given a declaration of a name, is there already a declaration of the same name in the current scope. (i.e., is it multiply declared?)
2. Given a use of a name, to which declaration does it correspond (using the "most closely nested" rule), or is it undeclared?
Attention!!!
Generally, symbol table is only needed to the two purposes of last slide; in other words,
once all declarations have been processed to build the symbol table, and all uses have been processed to link each ID node in the abstract-syntax tree with the corresponding symbol-table entry, then the symbol table itself is no longer needed
because no more lookups based on name will be performed.
Symbol Table Structure
Associate Attributes with Symbols:– Type– Name– Storage Class– Scope, Visibility and Lifetimes
Structure and Attributes Depend on Language Features:
– PASCAL allows Nested Lexical Scoping.– Lisp allows Dynamic Scoping.
Scope, Visibility and Lifetime
Scope: Unit of Static program structure that may have one or more variables declared in it.
Visibility: Refers to what scopes a given variable’s name refers to a particular instance of that variable.
Lifetime: Execution period from the point when a variable first becomes visible until it is last visible.
Scope, Visibility and Lifetime (cont.)
Prescribe: Scope, Visibility and Lifetime of Symbols Global: Visible throughout the entire program executi
on; lifetime encompasses whole execution. Functions, files: Visible in all of a given file; lifetime e
ncompasses the entire execution. Local: Visible and live with activation of a scope. Modifiers: How Values Can be Changed and Retaine
d. Static: Retains Values across Lifetime Boundaries.
Assumptions
In this tutorial, I assume:– Use static scoping– Require that all names be declared before they are used.– Do not allow multiple declarations of a name in the same
scope Even for different kinds of type
– Do allow the same name to be declared in multiple nested scope
But only once per scope
What operations do we need?
Given the above assumptions, we will need:1. Look up a name in the current scope
- only to check if it is multiply declared
2. Look up a name in the current and enclosing scopes- to check for a use of an undeclared name, and- to link a use with the corresponding symbol-table entry
3. Insert a new name into the symbol table with its attributes
4. Do what must be done when a new scope is entered5. Do what must be done when a scope is exited
Possible implementation
1. a list of tables2. a table of lists3. My combination wayFor each approach, we will consider what must be done when entering and exiting a scope, when processing a declaration, and when processing a use
Simplification: assume each symbol-table entry includes only:
– the symbol name– its type– the nesting level of its declaration and the location
Approach 1: List of Hashtables
The idea:– Symbol table = a list of hashtables– One hashtable for each currently visible scope.
When processing a scope S:
Example:
Example 2:
Symbol Table for Example 2:
Operations
On scope entry:– Increment the current level number and add a ne
w empty hashtable to the front of the list
To process a declaration of x:– Look up x in the first table in the list
If it is there, then issue a “multiply declared variable” error;
Otherwise, add x to the first table in the list.
Operations (cont.)
To process a use of x:– Look up x starting in the first table in the list;
If it is not there, then look up x in each successive table in the list. (Note: sometime just look into the global table)
If it is not in any table then issue an “undeclared variable” error
On scope exit,– Remove the first table from the list and decrement
the current level number
Inserting Method/Function Names
Method names belong in the hashtable for the outermost scope
– Not in the same table as the method’s variables
For example, in the previous example– Method name f is in the symbol table for the outermost scop
e– Name f is not in the same scope as parameters a and b, an
d variable x– This is so that when the use of name f in method g is proce
ssed, the name is found in an enclosing scope’s table.
End of Tutorial 6
Part 1 END
Approach 2: Hash Table of Lists
The idea:– When processing a scope S, the structure of the symbol
table is:
Definition
In this approach, there is only ONE big hashtable, which containing an entry for each variable for which there is
– Some declration in scope S or– In a scope that enclose S
Associated with each variable is a list of symbol-table entries
– The first list item corresponds to the most closely enclosing declaration;
– The other list items correspond to declarations in enclosing scopes.
Example
Nesting level information is crucial
The level-number attribute stored in each list item enables us to determine whether the most closely enclosing declaration was made– In the current scope or– In an enclosing scope
Hash Table of List: Operations
On scope entry:– Increment the current level number
To process a declaration of x:– Look up x in the symbol table
If x is there, fetch the level number from the first list item.
– If that level number = the current level then issue a “multiply declared variable” error;
– Otherwise, add a new item to the front of the list with the appropriate type and the current level number
Hash Table of List: Operations (cont.)
To process a use of x:– Look up x in the symbol table– If it is not there, then issue an “undeclared variable” error.
On scope exit:– Scan all entries in the symbol table, looking at the first item
on each list– If that items’ level number = the current number, then
remove it from its list (and if the list becomes empty, remove the entire symbol-table entry)
– Finally, decrement the current level number
Inserting Method/Function Names
The method/functions will be treated exactly same way as the variables. From the example, we can easily see it.
Approach 3: my approach
Combination of Lazy Approach 2 and some approach 1.
Use the Hash table of the list algorithm, but there is not deletion of tables/variables.
Refer to my sample output for example
Operations
Insert – which insert the appropriate information into the hash table.
Look up – which look up for the symbol and return the memory location
LookupDecl – returns the tree node corresponding to the declaration of the given variable, or return NULL if not found
incScope/decScope – increment and decrement of the scope count
setMemLoc – set the memory location of the variable
Information stored
Necessary– Name– Type – Memory location – Scope
Optional (but always good to have)– Scope name indicate which function the variable is in. (For
debugging propose)– Line Number (also for debugging propose)– Date Type – variable, function, etc.
Conclusion
Symbol table is one of the major component of compiler. A program without symbol table, can only be called as interpreter or converter, but not a “complete” program compiler.
Symbol table is not that complicated. Once the implementation algorithm and concept is clear, it is easy to write a program which create a symbol table.
Graphical breakdown
Symbol Table
Name
Memory Location
Scope
Type
Other information …
Insert(with build in filter)
Look up
Set memory Location
Scope function(increase/decrease)
Other functions …