1 Symbol Tables Symbol Tables
Mar 31, 2015
1
Symbol TablesSymbol Tables
2
ContentsContents IntroductionIntroduction A Simple CompilerA Simple Compiler Scanning – Theory and PracticeScanning – Theory and Practice Grammars and ParsingGrammars and Parsing LL(1) ParsingLL(1) Parsing Lex and yaccLex and yacc LR ParsingLR Parsing Semantic ProcessingSemantic Processing Symbol TablesSymbol Tables Run-time Storage OrganizationRun-time Storage Organization Code Generation and Local Code OptimizationCode Generation and Local Code Optimization Global OptimizationGlobal Optimization
3
A Symbol Table InterfaceA Symbol Table Interface
4
5
A Symbol Table Interface A Symbol Table Interface (cont’d.)(cont’d.)
6
A Symbol Table Interface A Symbol Table Interface (cont’d.)(cont’d.)
7
Basic Implementation TechniquesBasic Implementation Techniques Unordered ListUnordered List Ordered ListOrdered List Binary Search TreesBinary Search Trees
The expected search time is O(log(n))The expected search time is O(log(n)) For a balanced tree, the worst-case performance is For a balanced tree, the worst-case performance is
O(n).O(n).
8
Basic Implementation Techniques Basic Implementation Techniques (cont’d.)(cont’d.)
Hash TablesHash Tables Hash functionHash function
Resolving collisionsResolving collisions LinearLinear
If position h(n) is occupied, try (h(n)+1) mod mIf position h(n) is occupied, try (h(n)+1) mod m Add-the-hash rehashAdd-the-hash rehash
If position h(n) is occupied, try (2*h(n)) mod mIf position h(n) is occupied, try (2*h(n)) mod m Quadratic rehashQuadratic rehash
If position h(n) is If position h(n) is occupied, try (h(n)+1**2) occupied, try (h(n)+1**2) mod mmod m
Collision resolution by Collision resolution by chainingchaining
9
Basic Implementation Techniques Basic Implementation Techniques (cont’d.)(cont’d.)
String Space ArraysString Space Arrays A fixed-size arrayA fixed-size array
10
Basic Implementation Techniques Basic Implementation Techniques (cont’d.)(cont’d.)
Segmented String Space ArraysSegmented String Space Arrays A dynamic-allocation arrayA dynamic-allocation array
11
Block-Structured Symbol TablesBlock-Structured Symbol Tables
Block structured languagesBlock structured languages
12
Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)
Visibility rulesVisibility rules
13
Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)
An Individual Table for Each ScopeAn Individual Table for Each Scope
14
Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)
15
Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)
16
Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)
17
18