Symbol Table
•Whennamesarefound,theywillbeenteredintoasymbol
table,whichwillholdallrelevantinformationabout
identifiers,functionnames,objects,classes,interfaces,etc.
•Thisinformationwillbeusedlaterbythesemanticanalyzer
andthecodegenerator.
Lexical
Analyzer
Semantic
Analyzer
Code
Generator
Symbol
Table
Syntax
Analyzer
Usage of Symbol table by Various Phases of Compiler
•LexicalAnalysis:Createsnewentriesinthetableabouttoken.
•SyntaxAnalysis:Addsinformationregardingattributetype,scope,
dimension,lineofreference,etcinthetable.
•SemanticAnalysis:Usesavailableinformationinthetabletocheckfor
semanticsi.e.toverifythatexpressionsandassignmentsaresemantically
correct(typechecking)andupdateitaccordingly.
•IntermediateCodegeneration:Referssymboltableforknowinghowmuch
memoryandwhattypeisallocatedandtablehelpsinaddingtemporary
variableinformation.
•CodeOptimization:Usesinformationpresentinsymboltableformachine
dependentoptimization.
•TargetCodegeneration:Generatescodebyusingaddressinformationof
identifierpresentinthetable.
Symbol Table Organization for Block Structured
Languages
Symbol Table Organization for Non
Block Structured Languages
Four Structures of Non Block Structured Languages
•Unordered list: (linked list/array)
–for a very small set of variables;
–coding is easy, but performance is bad for large number of variables.
•Ordered linear list:
–use binary search on arrays;
–insertion and deletion are expensive;
–coding is relatively easy.
•Binary search tree:
–O(log n) time per operation (search, insert or delete) for n variables;
–coding is relatively difficult.
•Hash table:
–most commonly used;
–very efficient provided the memory space is adequately larger than the
number of variables;
–performance maybe bad if unlucky or the table is saturated;
–coding is not too difficult.
Hash Table Efficiency
•For a given hash table capacity,
–If there are too many buckets, then many buckets will
not be used, leading to space inefficiency.
–If there are too few buckets, then there will be many
clashes, causing the searches to degenerate into
predominately sequential searches, leading to time
inefficiency.
Symbol tables in block-structured languages
Symboltablesinblock-structuredlanguages:
–1.manysmallsymboltables
–2.oneglobalsymboltable
1.manysmalltables(StackbasedImplementation)
–onesymboltableperscope.
–useastackoftables.
–Thesymboltableforthecurrentscopeisontopofthestack.
–Thesymboltablesforotherenclosingscopesareplacedunderthecurrent
one.
–Pushanewtablewhenanewscopeisentered.
–Popasymboltablewhenascopeisclosed.
Example
Multiple symbol tables in one stack
H:int
A:int
L:int
x:real
y:real
:
symbol table
stack
{
intH,A,L;
{
real x,y;
:
:
}
{
char A,C,M;
print(M);
H + A ..... ;
X + L ...... ;
}
}
symbol table
Example
Multiple symbol tables in one stack
H:int
A:int
L:int
symbol table
stack
{
intH,A,L;
{
real x,y;
:
:
}
{
char A,C,M;
print(M);
H + A ..... ;
X + L ...... ;
}
} Secondscope is
completed. So Pop
(remove) the symbol
table of x,y
Example
H:int
A:int
L:int
A:char
C:char
M:char
:
symbol table
stack
{
intH,A,L;
{
real x,y;
:
:
}
{
char A,C,M;
print(M);
H + A ..... ;
X + L ...... ;
}
}
This symbol table in inserted
after popping a symbol table
consisting of x,y
symbol table
many small tables
•Tosearchforaname,wecheckthesymboltablesonthestackfromtopto
bottom.
•Wemayneedtosearchmultipletables.
E.g.Aglobalnameisdefinedinthebottom-mostsymboltable.
•Spacemaybewastedifafixed-sizedhashtableisusedto
implementsymboltables.
-hashtabletoobig–wastethememoryspace
-hashtabletoosmall--collisions
2. one global table
One Symbol Table with chaining
•Allnamesareinthesingleglobaltable.
•Whataboutthesamenameisdeclaredseveraltimes?
•Eachnameisgivenascopenumber.
•<name,scopenumber>shouldbeuniqueinthetable.
•Easytosearchaname.
•Newnamesareplacedatthefrontoflists.
•Tocloseascope,weneedtoremoveallentriesdefinedinthat
scope.
Example
Hash based Chaining
{
intH,A,L;
{
float x,y,H;
:
:
}
{
char A,C,M;
}
}
}
Scope
number
1
2
3
Scope
number
1
2
3
One Symbol Table with chaining
•Binary search tree with chaining.
•Use a doubly linked list to chain all entries with the same name.
Reference
•A.V. Aho, M.S. Lam, R. Sethi, J. D. Ullman,
Compilers Principles, Techniques and Tools,
Pearson Edition, 2013.
P. Kuppusamy -Lexical Analyzer