DOC PREVIEW
UW-Madison CS 536 - Lecture 06

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

59CS 536 Spring 2008©Block Structure Concepts• Nested VisibilityNo access to identifiers outsidetheir scope.• Nearest Declaration AppliesUsing static nesting of scopes.• Automatic Allocation and Deallocationof LocalsLifetime of data objects isbound to the scope of theIdentifiers that denote them.60CS 536 Spring 2008©Block-Structured SymbolTablesBlock structured symbol tablesare designed to support thescoping rules of blockstructured languages.For our CSX project we’ll useclassSymb to represent symbolsandSymbolTable toimplemented operationsneeded for a block-structuredsymbol table.ClassSymb will contain amethodpublic String name()that returns the nameassociated with a symbol.61CS 536 Spring 2008©Class SymbolTable contains thefollowing methods:• public void openScope() {A new and empty scope is opened.• public void closeScope() throwsEmptySTExceptionThe innermost scope is closed. Anexception is thrown if there is noscope to close.• public void insert(Symb s)throws DuplicateException,EmptySTExceptionA Symb is inserted in the innermostscope. An exception is thrown if aSymb with the same name isalready in the innermost scope or ifthere is no symbol table to insertinto.62CS 536 Spring 2008©• public Symb localLookup(String s)The innermost scope is searchedfor a Symb whose name is equal tos. Null is returned if no Symbnamed s is found.• public Symb globalLookup(String s)All scopes, from innermost tooutermost, are searched for a Symbwhose name is equal to s. The firstSymb that matches s is found;otherwise null is returned if nomatching Symb is found.63CS 536 Spring 2008©Is Case Significant?In some languages (C, C++,Java and many others) case issignificant in identifiers. Thismeansaa and AA are differentsymbols that may have entirelydifferent definitions.In other languages (Pascal, Ada,Scheme, CSX) case is notsignificant. In such languagesaa and AA are two alternativespellings of the same identifier.Data structures commonly usedto implement symbol tablesusually treat different cases asdifferent symbols. This is finewhen case is significant in alanguage. When case isinsignificant, you probably will64CS 536 Spring 2008©need to strip case beforeentering or looking upidentifiers.This just means that identifiersare converted to a uniform casebefore they are entered orlooked up. Thus if we choose touse lower case uniformly, theidentifiersaaa, AAA, and AaA areall converted toaaa forpurposes of insertion orlookup.BUT, inside the symbol table theidentifier is stored in the form itwas declared so thatprogrammers see the form ofidentifier they expect inlistings, error messages, etc.65CS 536 Spring 2008©How are Symbol TablesImplemented?There are a number of datastructures that can reasonablybe used to implement a symboltable:• An Ordered ListSymbols are stored in a linked list,sorted by the symbol’s name. Thisis simple, but may be a bit too slowif many identifiers appear in ascope.• A Binary Search TreeLookup is much faster than inlinked lists, but rebalancing may beneeded. (Entering identifiers insorted order turns a search treeinto a linked list.)• Hash TablesThe most popular choice.66CS 536 Spring 2008©Implementing Block-Structured Symbol TablesTo implement a blockstructured symbol table weneed to be able to efficientlyopen and close individualscopes, and limit insertion tothe innermost current scope.This can be done using onesymbol table structure if we tagindividual entries with a “scopenumber.”It is far easier (but morewasteful of space) to allocateone symbol table for eachscope. Open scopes arestacked, pushing and poppingtables as scopes are openedand closed.67CS 536 Spring 2008©Be careful though—manypreprogrammed stackimplementations don’t allowyou to “peek” at entries belowthe stack top. This is necessaryto lookup an identifier in allopen scopes.If a suitable stackimplementation (with a peekoperation) isn’t available, alinked list of symbol tables willsuffice.68CS 536 Spring 2008©More on HashtablesHashtables are a very usefuldata structure. Java provides apredefinedHashtable class.Python includes a built-indictionary type.Every Java class has ahashCodemethod, which allows anyobject to be entered into a JavaHashtable.For most objects, hash codesare pretty simple (the addressof the corresponding object isoften used).But for strings Java uses a muchmore elaborate hashfunction: ci37×ii0=n1–∑69CS 536 Spring 2008©n is the length of the string, ciis the i-th character and allarithmetic is done withoutoverflow checking.Why such an elaborate hashfunction?Simpler hash functions canhave major problems.Consider (add thecharacters).For short identifiers the sumgrows slowly, so large indiceswon’t often be used (leading tonon-uniform use of the hashtable).cii0=n1–∑70CS 536 Spring 2008©We can try (product ofcharacters), but now(surprisingly) the size of thehash table becomes an issue.The problem is that if even onecharacter is encoded as an evennumber, the product must beeven.If the hash table is even in size(a natural thing to do), mosthash table entries will be ateven positions. Similarly, ifeven one character is encodedas a multiple of 3, the wholeproduct will be a multiple of 3,so hash tables that are amultiple of three in size won’tbe uniformly used.cii0=n1–∏71CS 536 Spring 2008©To see how bad things can get,consider a hash table with size210 (which is equal to 2×3×5×7).This should be a particularlybad table size if a product hashis used. (Why?)Is it? As an experiment, all thewords in the Unix spellchecker’s dictionary (26000words) where entered. Over50% (56.7% actually) hitposition 0 in the table!Why such non-uniformity?If an identifier containscharacters that are multiples of2, 3, 5 and 7, then their hashwill be a multiple of 210 andwill map to position 0.72CS 536 Spring 2008©For example, in Wisconsin, nhas an ASCII code of 110 (2×55)andi has a code of 105(7×5×3).If we change the table size everso slightly, to 211, no tableentry gets more than 1% of the26000 words hashed, which isvery good.Why such a big difference? Well211 is prime and there is a bit afolk-wisdom that states thatprime numbers are goodchoices for hash table sizes.Now our product hash willcover table entries far moreuniformly (small factors in thehash don’t divide the table sizeevenly).73CS 536 Spring 2008©Now the reason for Java’s morecomplex string hash functionbecomes evident—it canuniformly fill a hash tablewhose size isn’t


View Full Document

UW-Madison CS 536 - Lecture 06

Download Lecture 06
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 06 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 06 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?