Chapter 7 - Run Time Environment
Chapter 7 - Run Time Environment
1
Symbol Table
• Symbol table is an important data structure used in a compiler.
• Symbol table is used to store the information about the occurrence of various
entities such as objects, classes, variable name, interface, function name etc.
• It is used by both the analysis and synthesis phases.
• The symbol table used for following purposes:
ü It is used to store the name of all entities in a structured form at one place.
3
Cont'd ...
Implementation
• The symbol table can be implemented in the unordered list if the
compiler is used to handle the small amount of data.
• A symbol table can be implemented in one of the following techniques:
1. Linear (sorted or unsorted) list
2. Hash table
3. Binary search tree
• Symbol table are mostly implemented as hash table.
4
Cont'd ...
Operations
• The symbol table provides the following operations:
1. Insert ()
– is more frequently used in the analysis phase when the tokens are identified and
names are stored in the table.
– is used to insert the information in the symbol table like the unique name occurring
in the source code.
– In the source code, the attribute for a symbol is the information associated with that
symbol.
– The information contains the state, value, type and scope about the symbol.
– The insert () function takes the symbol and its value in the form of argument.
5
Cont'd ...
– For example: int x; insert (x, int)
2. lookup()
– is used to search a name.
– It is used to determine:
• The existence of symbol in the table.
• The declaration of the symbol before it is used.
• Check whether the name is used in the scope.
• Initialization of the symbol.
• Checking whether the name is declared multiple times.
– The basic format of lookup() function is as follows:
lookup (symbol)
– This format is varies according to the programming language. 6
Hash Table
– Hashing is an important procedure used to search the records of the symbol table.
– This method is superior to the list organization.
– In the hashing scheme, two tables are maintained:
1. Hash Table
2. Symbol Table
– The hash table consists of K entries from 0 to K−1.
– These entries are a pointer to the symbol table pointing to the names of the symbol table.
– It can decide whether "Name" is in the symbol table we use a hash function 'h'
including h (Name) will result in an integer between 0 to K − 1.
7
Hash Table Cont.…
– Using this position, we can access the exact locations of the name in the symbol table.
– The hash function should result in the distribution of the name in the symbol table.
– The hash function should be that there will be a minimum number of collisions.
– Collision is a situation where the hash function results in the same location for storing
names.
– There are various collision resolution techniques are open addressing, chaining, and
rehashing.
Complexity
– This scheme gives the capability of performing m access on n names in time proportional
to n(n + m)/k for any constant k of our choice.
– Since k can be made as large as we like. 8
Hash Table Cont.…
– This approach is usually superior to linear lists or search trees and is the technique of
option for symbol tables in most situations, especially if storage is not incredibly costly.
9
Representing Scope Information
– In the source program, every name possesses a region of validity, called the scope of
that name.
– The rules in a block-structured language are as follows:
10
Cont'd ...
• Tables are organized into stack and each table contains the list of names
and their associated attributes.
• When the name's reference is translated then each table is searched,
starting from the each table on the stack.
• Whenever a new block is entered then a new table is entered onto the
stack.
• The new table holds the name that is declared as local to this block.
• When the declaration is compiled then the table is searched for a name.
• If the name is not found in the table then the new name is inserted.
11
Cont'd ...
• For example:
Figure: Symbol table organization that complies with static scope information 12
rules