1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

Hash Tables.
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
© 2004 Goodrich, Tamassia Hash Tables1  
TTIT33 Algorithms and Optimization – Lecture 5 Algorithms Jan Maluszynski - HT TTIT33 – Algorithms and optimization Lecture 5 Algorithms ADT Map,
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing CS 3358 Data Structures.
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Lecture 11 March 5 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Introduction to Hashing CS 311 Winter, Dictionary Structure A dictionary structure has the form: (Key, Data) Dictionary structures are organized.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Types Type = Why? a set of values
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
1. 2 Problem RT&T is a large phone company, and they want to provide enhanced caller ID capability: –given a phone number, return the caller’s name –phone.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Hash Table March COP 3502, UCF.
Spring 2015 Lecture 6: Hash Tables
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Search  We’ve got all the students here at this university and we want to find information about one of the students.  How do we do it?  Linked List?
Storage and Retrieval Structures by Ron Peterson.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hashing Hashing is another method for sorting and searching data.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
CS201: Data Structures and Discrete Mathematics I Hash Table.
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Ihab Mohammed and Safaa Alwajidi. Introduction Hash tables are dictionary structure that store objects with keys and provide very fast access. Hash table.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
CSE 373 Data Structures and Algorithms Lecture 17: Hashing II.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Data Structure & Algorithm Lecture 8 – Hashing JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
CSC 413/513: Intro to Algorithms Hash Tables. ● Hash table: ■ Given a table T and a record x, with key (= symbol) and satellite data, we need to support:
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
Hash table CSC317 We have elements with key and satellite data
Hashing - resolving collisions
Hashing Alexandra Stefan.
Hashing Alexandra Stefan.
Quadratic probing Double hashing Removal and open addressing Chaining
Resolving collisions: Open addressing
CS202 - Fundamental Structures of Computer Science II
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
EE 312 Software Design and Implementation I
Hashing.
EE 312 Software Design and Implementation I
CSE 373: Data Structures and Algorithms
Presentation transcript:

1 Symbol Tables The symbol table contains information about –variables –functions –class names –type names –temporary variables –etc.

2 Symbol Tables What kind of information is usually stored in a symbol table? –type –storage class –size –scope –stack frame offset –register

3 Symbol Tables How is a symbol table implemented? –array simple, but linear LookUp time However, we may use a sorted array for reserved words, since they are generally few and known in advance. –tree O(lgn) lookup time if kept balanced –hash table most common implementation O(1) LookUp time

4 Symbol Tables Hash tables –use array of size m to store elements –given key k (the identifier name), use a function h to compute index h(k) for that key –collisions are possible two keys hash into the same slot. Hash functions –A good hash function is easy to compute avoids collisions (by breaking up patterns in the keys and uniformly distributing the hash values)

5 Symbol Tables Hash functions –A common hash function is h(k) =  m*(k*c-  k*c  ) , for some constant 0<c<1 –In English multiply the key k by the constant c Take the fractional part of k*c Multiply that by size m Take the floor of the result –A good value for c:

6 Resolving collisions Chaining –Put all the elements that collide in a chain (list) attached to the slot. Insert/Delete/Lookup in expected O(1) time However, this assumes that the chains are kept small. –If the chains start becoming too long, the table must be enlarged and all the keys rehashed.

7 Resolving collisions Open addressing –Store all elements within the table The space we save from the chain pointers is used up to make the array larger. –If there is a collision, probe the table in a systematic way to find an empty slot. –If the table fills up, we need to enlarge it and rehash all the keys. Open addressing with linear probing –Probe the slots in a linear manner –Simple but Bad: results in clustering (long sequences of used slots build up very fast)

8 Resolving collisions Open addressing with double hashing –Use a second hash function. – The probe sequence is: (h(k) + i*h 2 (k) ) mod m, with i=0, 1, 2,... –Good performance Since we use a second function, keys that originally collide will subsequently have different probe sequences. –No clustering –A good choice for h 2 (k) is p-(k mod p) where p is a prime less than m

9 Scope issues Block-structured languages allow nested name scopes. Usual visibility rules –Only names created in the current or enclosing scopes are visible –When there is a conflict, the innermost declaration takes precedence.

10 Scope issues One idea is to have a global hash table and save the scope information for each entry. When an identifier goes out of scope, scan the table and remove the corresponding entries –We may even link all same-scope entries together for easier removal. Careful: deleting from a hash table that uses open addressing is tricky –We must mark a slot as Deleted, rather than Empty, otherwise later LookUp operations may fail.

11 Scope issues Another idea is to maintain a separate, local hash table for each scope. We may store the tables in a tree or a stack (that mirrors the stack frames).

12 Structure tables Where should we store struct field names? –Separate mini symbol table for each struct Conceptually easy –Separate table for all struct field names We need to somehow uniquely map each name to its structure (e.g. by concatenating the field name with the struct name) –No special storage struct field names are stored in the regular symbol table. Again we need to be able to map each name to its structure.