You are on page 1of 3

Q.What is an ordered and unordered symbol table?

What is the function of symbol


table in the compilation process? Explain.
A)
1) classification of languages:
Languages are classified into two types 1) block structured languages 2)non block structured
languages
2)what a language consists of ?
In general a language consists of several modules
3)non block structured language:
A language is said to be non block structured language if it consists of several modules and
each module being a separate compilation unit and does not include any other sub modules.
4)ways of organizing a symbol table
There are many ways that can be used for organization of a symbol table, among these
methods ordered and unordered symbol table are simple to implement.
5)Ordered symbol table
1) when we say that an ordered table organization is used?
When the name of the variable is used to determine the position of the attributes of a variable
then we say that ordered symbol table organization is used.
2)placement of variables
The variables are placed in the ordered symbol table in the alphabetical order of their names
3)searching of variables
Searching in an ordered symbol table can be performed in two ways 1) linear search 2) binary
search.
4)Diagram:
Variable name
Type
Dimension
Other attributes
Alpha
1
0
Beta
1
0
Comp#
2
1
Formula
1
0
N
3
2
Operator
7
0
Param
9
3
Result
6
2
Y3
1
0
Linear Search
The Linear search in the Ordered symbol table has several advantages over that of unordered
symbol table.When a variable name does not exist in an ordered symbol table then there is no
need to search the complete symbol table, we just check if the variable is lexically less than
the first variable in the table. If so it is declared that the variable cant be determined. Thus in
order to determine whether a variable is in an ordered table or not requires an average search
length of (n+1)/2 .
Advantages
1) The entire table need not be searched to determine the absence of a variable
1

2) Direct generation of a cross reference listing can use an ordered symbol table
Disadvantage
For example assume that a variable count is to be inserted in an ordered symbol table, then
it needs that all the records or names lexically larger than count must be moved down in the
table. Thus inserting a variable in an ordered symbol table with linear search requires n-m
comparisons followed by m moves.
BinarySearch
Binary Search divides the table into two halves with only one element in between these
halves called as Middle Element. The element in the left half are lexically less than the
middle element while the elements in the right half are lexically greater than middle element.
If the search element lies in the left half then that half acts as sub table and it is again divided
into two halves with one middle element in it and the procedure continues.
n-1

The number of records that needs to be compared are 2


where n is the number of binary
search made till now.
Advantages:
1)if the average number of elements in a table is more than 25,binary search must be
preferred to linear search.
2)The lookup operation is simplified.
Disadvantages
1) The upper and lower indices must be regularly updated even though the updation is
machine independent.
2) The insertion operation needs an average of (n+1)/2 record moves.
6) Unordered Symbol Tables:
When the variables declared or encountered during compilation, entries are made into the
symbol table.
Languages with implicit declarations
In languages with implicit declarations when a variable is encountered then first a lookup
operation is performed which is followed by an insert operation on the symbol table. Since
the result of a lookup operation on the symbol table determines whether the element must be
inserted into the symbol table or not. Lookup and insert operations on symbol table consider
two cases.
Case 1:
If a new variable is inserted then it requires n comparisons for insertion and lookup
operations.
Case 2:
If a previous variable is referenced then insertion is not required.It requires an average of
(n+1)/2 comparisons for lookup.
Thus the number of comparisons for an insertion and lookup process may be

n+(1-)(n+1)/2
2

Where = First Variable References / Total variable references


Languages with explicit declarations
These languages require different number of operations for insertion and lookup. An insertion
operation requires no comparison since the variable is added at the current end of the table.
However such an insertion ignores duplicate variable declarations. Therefore an insertion
operation must be preceded by a lookup operation.If there are n records in the symbol table
and if the number of comparisons for a lookup of first record is one , second record is two
and so on then the expected length of search for lookup on the symbol table may be (n+1)/2
Drawbacks
1) For a large size , unordered symbol table organization is not suitable as depending on the
size of the table the time taken for lookup and insertion operations varies largely
2)For direct generation of a cross reference listing , the unordered symbol table need to be
sorted first.

You might also like