What is role of different data structures in compiler design?

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.

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).

Updated on: 08-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started