cpsc 325 - compiler tutorial 6 & 7 symbol table. what is symbol table?
Post on 20-Dec-2015
227 views
TRANSCRIPT
![Page 1: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/1.jpg)
CPSC 325 - Compiler
Tutorial 6 & 7
Symbol Table
![Page 2: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/2.jpg)
What is Symbol Table?
![Page 3: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/3.jpg)
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).
![Page 4: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/4.jpg)
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?
![Page 5: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/5.jpg)
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.
![Page 6: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/6.jpg)
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.
![Page 7: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/7.jpg)
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.
![Page 8: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/8.jpg)
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.
![Page 9: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/9.jpg)
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
![Page 10: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/10.jpg)
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
![Page 11: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/11.jpg)
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
![Page 12: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/12.jpg)
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:
![Page 13: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/13.jpg)
Example:
![Page 14: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/14.jpg)
Example 2:
![Page 15: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/15.jpg)
Symbol Table for Example 2:
![Page 16: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/16.jpg)
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.
![Page 17: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/17.jpg)
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
![Page 18: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/18.jpg)
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.
![Page 19: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/19.jpg)
End of Tutorial 6
Part 1 END
![Page 20: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/20.jpg)
Approach 2: Hash Table of Lists
The idea:– When processing a scope S, the structure of the symbol
table is:
![Page 21: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/21.jpg)
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.
![Page 22: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/22.jpg)
Example
![Page 23: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/23.jpg)
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
![Page 24: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/24.jpg)
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
![Page 25: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/25.jpg)
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
![Page 26: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/26.jpg)
Inserting Method/Function Names
The method/functions will be treated exactly same way as the variables. From the example, we can easily see it.
![Page 27: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/27.jpg)
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
![Page 28: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/28.jpg)
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
![Page 29: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/29.jpg)
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.
![Page 30: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/30.jpg)
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.
![Page 31: CPSC 325 - Compiler Tutorial 6 & 7 Symbol Table. What is Symbol Table?](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d445503460f94a219cc/html5/thumbnails/31.jpg)
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 …