Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

EXPERIMENT NO.

12

Problem Statement: Prepare a report on data structure in compiler


design. Specify and explain the use(s) of any data structure(s) whichever
are applicable.
Objective: To choose appropriate data structure and apply it in various
problems (LO4).

Group No. 10
Group Member Roll Number
Answeeta Pereira 46

Pon Adityan 47
Jeyamurugan

Ashiv Prajapati 48

Meet Rathod 49

Pooja Raut 50

Abstract:
The compiler is software that converts a program written in a
high-level language (Source Language) to a low-level
language (Object/Target/Machine Language/0’s, 1’s). 

During compilation, the symbol table is searched each time an identifier is


encountered. Data are added if a new name or new information about an
existing name is find. Thus, in designing a symbol table structure, it
would like a scheme that enables us to insert new entries and identify
current entries in a table effectively.

Data Structures in compiler:

There are four symbol tables used in a data structure which are as follows

 Lists− The simplest and clear to implement a data structure for a symbol is the
linear list of records as displayed in the figure.
It can use a single array or several equivalent arrays to save names and their related data.
New names are inserted to the list in the order in which they are encountered. It can retrieve
data about a name we search from the starting of the array up to the position marked by
pointer AVAILABLE, which denotes the starting of the empty portion of the array.

 Self-organizing lists− At the cost of a little extra space we can use a trick that
will save a substantial fraction of the time spent in searching the symbol table.
It can add a LINK field to each record, and we search the list in the order
indicated by LINK’s. When a name is referenced or its record is first created, it
can move the record for that name to the front of the list by moving pointers.

 Search Tree− A more effective method to symbol table organization is to


insert two link fields, LEFT and RIGHT, to each record. We use these fields to
link the records into a binary search tree.
This tree has the property that all names NAME (j) accessible from NAME (i) by following
the link LEFT (i) and then following any sequence of links will precede NAME (i) in
alphabetical order (symbolically, NAME (j) < NAME (i))
Binary Search Tree Algorithm

 Initially, Ptr will point to the root of the Tree.


 While Ptr ≠ NULL do
 If NAME = NAME (Ptr)
 then return true
 else if NAME<NAME (Ptr) then
 Ptr− LEFT(Ptr)
 else Ptr−= RIGHT (Ptr)
 End of Loop
 Hash Table− Hash table includes 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' such
that h (Name) will result in an integer between 0 to K − 1. It can search any
names by position = h(Name).

Symbol table
Symbol table is an important data structure created and maintained by
compilers in order to store information about the occurrence of various
entities such as variable names, function names, objects, classes,
interfaces, etc.
Symbol table is used by both the analysis and the synthesis parts of
a compiler.

A symbol table may serve the following purposes


 To store the names of all entities in a structured form at one place.
 To verify if a variable has been declared.
 To implement type checking, by verifying assignments and
expressions in the source code are semantically correct.
 To determine the scope of a name (scope resolution).

Implementation:
If a compiler is to handle a small amount of data, then the symbol table can
be implemented as an unordered list, which is easy to code, but it is only
suitable for small tables only.
A symbol table can be implemented in one of the following ways:
 Linear (sorted or unsorted) list
 Binary Search Tree
 Hash table
Among all, symbol tables are mostly implemented as hash tables, where
the source code symbol itself is treated as a key for the hash function and
the return value is the information about the symbol.
Operations:
A symbol table, either linear or hash, should provide the following
operations.
 Allocate: to allocate a new empty symbol table.
 Free: to remove all entries and free the storage of a symbol table.
 Insert: to insert a name in a symbol table and return a pointer to its
entry.
 lookup: to search for a name and return a pointer to its entry.
 set_ attribute: to associate an attribute with a given entry.
 get _attribute: to get an attribute associated with a given entry.

Scope Management:
A compiler maintains two types of symbol tables:
1. Global symbol table: which can be accessed by all the procedures .
2. Scope symbol tables: that are created for each scope in the
program.
To determine the scope of a name, symbol tables are arranged in
hierarchical structure as shown in the example below:
This symbol table data structure hierarchy is stored in the semantic
analyzer and whenever a name needs to be searched in a symbol table, it
is searched using the following algorithm:
1. First a symbol will be searched in the current scope, i.e. current
symbol table.
2. If a name is found, then search is completed, else it will be
searched in the parent symbol table until.
3. Either the name is found or global symbol table has been searched
for the name.
Conclusion:
Symbol table is an important data structure created and maintained by
compilers in order to store information about the occurrence of various
entities such as variable names, function names, objects, classes, interfaces,
etc. Symbol table is used by both the analysis and the synthesis parts of a
compiler.

References:
Refer websites : geeksforgeeks
Tutorialspoint

You might also like