Compiler Chap 6' Symbol Table - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Compiler Chap 6' Symbol Table

Description:

Contents. Introduction. Various implementation techniques ... e.g. int x; vs. struct x { float y, z; } variable name vs. tag name. The content of entries ... – PowerPoint PPT presentation

Number of Views:1045
Avg rating:3.0/5.0
Slides: 11
Provided by: suno
Category:

less

Transcript and Presenter's Notes

Title: Compiler Chap 6' Symbol Table


1
CompilerChap 6. Symbol Table
  • Internet Management Technology Lab.

2
Contents
  • Introduction
  • Various implementation techniques
  • Representing scope information
  • Forward reference

3
Introduction
  • Symbol Table
  • A data structure that stores information about
    various source language constructs
  • A compiler uses symbol table to keep track of
    scope and binding information about names
  • Entries
  • An entry is for the declaration of a name
  • The format depends on the usage of the name (e.g.
    records)
  • It could have multiple entries for the same name
    in some cases
  • e.g. int x vs. struct x float y, z
  • ? variable name vs. tag name
  • The content of entries
  • attribute names, scope identifier, type, size,
    etc.

4
Introduction
  • Basic symbol table interface
  • Insert function
  • insert a new entry into the table
  • Lookup function
  • find an existing entry (even reserved words can
    be handled in the same manner)

5
Various implementation techniques (1/4)
  • Unordered list
  • Simplest possible storage mechanism
  • But impractically slow for large table
  • Ordered list
  • The entries are kept ordered
  • Lookup is fairly efficient (O(logn), but
    insertion operation is relatively expensive
  • It is useful for small sized tables
  • Binary or 2-3 search trees
  • Designed to combine the size flexibility and
    insertion efficiency of a linked data structure
  • O(logn) time for average case, but not guaranteed
    because the variable uses are not random

6
Various implementation techniques (2/4)
  • Hash tables
  • The most common means of implementing the symbol
    tables in production compilers
  • It needs a good hash function and collision
    handing techniques
  • O(c) time complexity
  • hash function
  • Map each of possible names into one of a fixed
    number of positions
  • The properties are
  • hash function h(n) is function of n
  • h can be computed quickly
  • h is uniform and randomizing in order to minimize
    collision

7
Various implementation techniques (3/4)
  • Hash tables
  • Simple techniques to find h(n)
  • Add up the integer values of the characters in a
    string
  • hi  hi-1 ci where h1 0
  • hi  ahi-1 xor ci
  • Resolving collisions when h(n) is occupied
  • linear resolution
  • try (h(n) 1) mod n, (h(n) 2) mod n
  • should be careful about the long chain
  • add-the-hash rehash
  • try (2 h(n)) mod m, (3 h(n)) mod m
  • Prevent long chain and m must be prime to try
    every slots

8
Various implementation techniques (4/4)
  • Hash tables
  • Resolving collisions when h(n) is occupied
    (contd)
  • Quadratic rehash
  • Try (h(n) 12) mod m, (h(n) 22)
  • Chaining (Dynamic hash)
  • Records that hash to a given value are chained
    together on a linked list

9
Representing scope information
  • Scope rules of the source language determine
    which declaration is appropriate
  • Example
  • main()
  •    
  •        int i
  •            i 5
  •                   
  • int i
  •                    i 7 print i
  •            
  •    
  • Simple approach is to keep separate tables for
    each scope

10
Forward reference
  • Problem
  • Reference to a name appears before definition of
    the language
  • e.g.
  • S1
  • TEST
  • JUMP  place
  • S2      
  • place
  • Resolutions
  • Backpatching
  • Leave space for unknown value and fill it later
  • Multiple pass resolution
  • Resolve it at the later passes
Write a Comment
User Comments (0)
About PowerShow.com