3. DATA STRUCTURES FOR SYMBOL
TABLES
Symbol Table in Compiler
A symbol table is a data structure employed by a
language translator, like a compiler or interpreter,
within which each identifier in a program's source
code is connected with information about its
declaration or presence in the source code, like
its type, scope level, and infrequently its position.
A symbol table is a significant data structure
used in a compiler that correlates characteristics
with program identifiers. The analysis and
synthesis stages employ symbol table
information to verify that used identifiers have
been specified, to validate that expressions and
assignments are semantically accurate, and to
build intermediate or target code.
4. There are three data structures used to
implement symbol table:
Linear List
Binary Search Tree
Hash Table
5. LINEAR LISTS
Linear Lists
It is the simplest and most
straightforward method of
implementing data structures. To store
names and their accompanying
information, we use a single array.
New names are added to the list in the
order that they appear. To add a new
name, we must first check the list to
ensure that it does not already exist.
6.
7. TREES
Trees
It is a more efficient method of
organizing symbol tables. Each record
now has two link fields, LEFT and
RIGHT. The procedure below is used
to find NAME in a binary search tree
where x is originally a reference to the
root.
8. 1. while x ≠ null do
2. if NAME = NAME(x) then /* NAME
found take action on success*/
3. else if NAME < NAME(x) then
x:=LEFT(x) /* visit left child*/ 5. else /*
NAME (p)<NAME*/ X:= RIGHT(x)
/*visit right child*/
11. HASH FUNCTION
Hash Function
The hash function is the mapping
between an item and the place in the
hash table where that item resides.
The hash function takes a collection
item and returns an integer in the
range of slot names from 0 to m-1.
12. Example:
Assume that we have the set of integer items 59,
28, 93, 18, 77 and 31.
Consider m=11 (number of slots of hash table)
Suppose we want to insert the above elements
within the slots by using formula:
h(item) % m
59 % 11 = 4
28 % 11 = 6
93 % 11 = 5
18 % 11 = 7
77 % 11 = 0
31 % 11 = 9