Symbol table in compiler Design

7,691 views 28 slides Mar 08, 2021
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Symbol table in compiler Design


Slide Content

Symbol Table

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
•Symboltable:Adatastructureusedbyacompilertokeeptrackof
semanticsofnames.
–Determinewhetherthevariableisdefinedalreadyornot.
–Determinethescope.
•Theeffectivecontextwhereanameisvalid.
–Whereitisstored:storageaddress.
–Typecheckingforsemanticcorrectnessdetermination.
•Operations:
–Find/Lookup/Search:Accesstheinformationassociatedwithgiven
name.
–Insert:addanameintothetable.
–Delete:removeanamewhenitsscopeisclosed.

5
Symbol Table
•Compilerusessymboltabletokeeptrackofscope(block)andbindinginformation
aboutnames
•symboltableischanged(updated)everytime
–ifanewnameisdiscovered
–ifnewinformationaboutanexistingnameisdiscovered
•Symboltablemusthavemechanismto:
–addnewentries
–findexistinginformationefficiently
•Twocommonmechanism:
–linearlists,simpletoimplement,poorperformance
–hashtables,greaterprogramming,goodperformance
•Compilershouldbeabletogrowsymboltabledynamically
•Ifsizeisfixed,itmustbelargeenoughforthelargestprogram

Symbol table Information
Symboltablestores:
•Foreachtypename,itstypedefinition(eg.fortheCtype
declarationtypedefint*mytype,itmapsthenamemytypetoadata
structurethatrepresentsthetypeint*).
•Foreachvariablename,itstype.Ifthevariableisanarray,italsostores
dimensioninformation.Itmayalsostorestorageclass,offsetinactivation
recordetc.
•Foreachconstantname,itstypeandvalue.
•Foreachfunctionandprocedure,itsformalparameterlistanditsoutput
type.Eachformalparametermusthavename,type,typeofpassing(by-
referenceorby-value),etc.

Symbol table

•VariableName:
–Mustbepresenttoletotherphasesknowwhichisaparticularvariable
–Majorissueisvariabilityoflengthofname
–Twopopularapproaches
•Tosetafixedmaximumlengthforvariablename
•Tokeeponlyadescriptorinthevariablenamefieldandkeepthe
nameingeneralstringareareferencedbythisdescriptor
•Firstapproachgivesquicktableaccesswhiletheothersupportsefficient
storageofvariablenames
•Firstapproachisinefficientinshortnamedvariableswhilesecondhasslow
tableaccessduetoreferencing

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