1 symbol tables. 2 contents introduction introduction a simple compiler a simple compiler scanning...

18
1 Symbol Tables Symbol Tables

Upload: daryl-currington

Post on 31-Mar-2015

225 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

1

Symbol TablesSymbol Tables

Page 2: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

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

Page 3: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

3

A Symbol Table InterfaceA Symbol Table Interface

Page 4: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

4

Page 5: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

5

A Symbol Table Interface A Symbol Table Interface (cont’d.)(cont’d.)

Page 6: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

6

A Symbol Table Interface A Symbol Table Interface (cont’d.)(cont’d.)

Page 7: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

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).

Page 8: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

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

Page 9: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

9

Basic Implementation Techniques Basic Implementation Techniques (cont’d.)(cont’d.)

String Space ArraysString Space Arrays A fixed-size arrayA fixed-size array

Page 10: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

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

Page 11: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

11

Block-Structured Symbol TablesBlock-Structured Symbol Tables

Block structured languagesBlock structured languages

Page 12: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

12

Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)

Visibility rulesVisibility rules

Page 13: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

13

Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)

An Individual Table for Each ScopeAn Individual Table for Each Scope

Page 14: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

14

Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)

Page 15: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

15

Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)

Page 16: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

16

Block-Structured Symbol Tables Block-Structured Symbol Tables (cont’d.)(cont’d.)

Page 17: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

17

Page 18: 1 Symbol Tables. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

18