Symbol Table Design (Compiler Construction)
Symbol Table Design (Compiler Construction)
Symbol Table Design (Compiler Construction)
9/3/2012
Source Program
COMPILER
Target Program
9/3/2012
ANALYSIS
SYNTHESIS
9/3/2012
L E X I C A L A N A L Y Z E R
Stream of tokens
S Y N T A X A N A L Y Z E R
Parse tree
S S Y E M N A T N A T X
I C
INTER
INTERMEDIA MEDIATE CODE GENERCODE ATOR
TE
C O D E
C O D E
A N A L Y Z E R
GENERA T OR
O P T I M I Z E R
G E N E R A T O R
Analysis 9/3/2012
SYMBOL TABLE
Synthesis
ANALYSIS
Breaks up the source program into constituent pieces and imposes a grammatical structure on them. It then uses this structure to create an intermediate representation of the source program. If the analysis part detects that the source program is either syntactically ill formed or semantically unsound, then it must provide informative messages, so the user can take corrective action. The analysis part also collects information about the source program and stores it in a data structure called a symbol table, which is passed along with the intermediate representation to the synthesis part.
9/3/2012
SYNTHESIS
The synthesis part constructs the desired target program from the intermediate representation and the information in the symbol table
ANALYSIS SYNTHESIS
9/3/2012
COMPILERS ROLE??
An essential function of a compiler
Record the variable names used in the source program and collect information about various attributes of each name.
These attributes may provide information about the storage allocated for a name , its type and its scope , procedure names ,number and types of its arguments, the method of passing each argument and the type returned
9/3/2012
9/3/2012
Given an Identifier which name is it? What information is to be associated with a name? How do we access this information?
9/3/2012
Parameter
Constant NAME Record RecordField Procedure Array and files
9/3/2012 10
SYMBOL TABLE-ATTRIBUTES
Each piece of information associated with a name is called an attribute. Attributes are language dependent. Different classes of Symbols have different Attributes
Variable, Constants Type , Line number where declared , Lines where referenced , Scope
9/3/2012
11
9/3/2012
13
int
LB1 UB1
SYMBOL TABLE
A pointer steers the symbol table to remotely stored information for array a.
9/3/2012 15
9/3/2012
16
LB1
int
UB1
SYMBOL TABLE
B
STRING TABLE
9/3/2012
17
9/3/2012
18
The rules governing the scope of names in a blockstructured language are as follows 1. A name declared within a block B is valid only within B. 2. If block B1 is nested within B2, then any name that any name that is valid for B2 is also valid for B1,unless the identifier for that name is re-declared in B1. The scope rules required a more complicated symbol table organization than simply a list association between names and attributes. Each table is list names and there associated attributes and the tables are organized into a stack.
9/3/2012 19
Real
Var x,y : integer Procedure P: Var x,a :boolean; Procedure q: Var x,y,z : real; begin end begin .. 9/3/2012 End
q a x Real Real Real Symbol table for p
Y X
NESTING DEPTH
Another technique can be used to represent scope information in the symbol table. We store the nesting depth of each procedure block in the symbol table and use the [procedure name , nesting depth] pairs as the key to accessing the information from the table.
A nesting depth of a procedure is a number that is obtained by starting with a value of one for the main and adding one to it every time we go from an enclosing to an enclosed procedure.
This number is basically a count of how many procedures are there in the referencing environment of the procedure .
9/3/2012 21
Var x,y : integer Procedure P: Var x,a :boolean; Procedure q: Var x,y,z : real; begin end begin .. End
x y z q a
3 3 3 2 2
x
P y z
2
1 1 1
Boolean
Proc Integer integer
9/3/2012
22
LINKED LIST
A linear list of records is the easiest way to implement symbol table. The new names are added to the symbol table in the order they arrive. Whenever a new name is to be added to be added it is first searched linearly or sequentially to check if or the name is already present in the table or not and if not , it is added accordingly. Time complexity O(n) Advantage less space , additions are simple Disadvantages - higher access time.
9/3/2012 24
UNSORTED LIST
01 PROGRAM Main 02 GLOBAL a,b 03 PROCEDURE P (PARAMETER x) 04 LOCAL a 05 BEGIN {P} 06 a 07 b 08 x 09 END {P} 10 BEGIN{Main} 11 Call P(a) 12 END {Main}
Name Main a b P x a 9/3/2012 Characteristic Class Program Variable Variable Procedure Parameter Variable 0 0 0 0 1 1 Scope
Look up Complexity
On
Other Attributes Declared Referenced Other Line 1 Line 2 Line 11 Line 2 Line 7 Line 3 Line 11 1, parameter, x Line 3 Line 8 Line 4 Line 6 25
SORTED LIST
01 PROGRAM Main 02 GLOBAL a,b 03 PROCEDURE P (PARAMETER x) 04 LOCAL a 05 BEGIN {P} 06 a 07 b 08 x 09 END {P} 10 BEGIN{Main} 11 Call P(a) 12 END {Main}
Name a a b Main P x 9/3/2012
Look up Complexity
Olog 2 n On
Other Attributes Declared Reference Other Line 2 Line 11 Line 4 Line 6 Line 2 Line 7 Line 1 Line 3 Line 11 1, parameter, x 26 Line 3 Line 8
SEARCH TREES
Efficient approach for symbol table organisation We add two links left and right in each record in the search tree. Whenever a name is to be added first the name is searched in the tree. If it does not exists then a record for new name is created and added at the proper position. This has alphabetical accessibility.
9/3/2012
27
BINARY TREE
Main Program 0 Line1
Procedure
Line3
Line11
Parameter
1 Line3
Line8
Variable 0
Line2 Line11
a Variable 1 9/3/2012
Line4 Line6
28
BINARY TREE
Lookup complexity if tree balanced
Olog 2 n
On
9/3/2012
29
HASH TABLE
Table of k pointers numbered from zero to k-1 that points to the symbol table and a record within the symbol table. To enter a name in to the symbol table we found out the hash value of the name by applying a suitable hash function. The hash function maps the name into an integer between zero and k-1 and using this value as an index in the hash table.
9/3/2012
30
b 98
77 110 97
80 120
PROGRAM Main
P Procedure 1 Line 3 a Variable 0 Line2 1 Line4 b Variable 0 1Line2 x Parameter Line3 a Variable 0 Line2 b Variable 0 Line2
31
REFERENCES
O.G.Kakde - Compiler design Compilers Principles, Techniques & Tools Second Edition : Alfred V Aho , Monica S Lam, Ravi Sethi, Jeffery D Ullman
9/3/2012
32
9/3/2012
33