Implementation of Symbol table in C | Compiler Design

If you are here to read about the symbol table, then it means that you already know about the basic flow of all the phases of the compiler as demonstrated in the diagram. If you want to read about phases of the compiler and their relation with symbol table then must read the article “phases of compiler“.

Phases of Compiler
Phases of Compiler

Symbol table in Compiler Design

The compiler creates and maintains a data structure to store information about the occurrence of various entities such as variable and function names, objects and classes, etc. This kind of data structure is known as a symbol table.

C++  Code 

The symbol table for C++ Code

symbol table compiler
symbol table compiler

Operations on Symbol table

Allocate Operation on Symbol table

Allocate Operation can be performed on a symbol table to allocate a new empty symbol table.

Insert Operation on Symbol table

Insert Operation can be performed on a symbol table to insert a name in a symbol table and return a pointer to its entry.

Set_attribute Operation on Symbol table

Set_attribute Operation can be performed on a symbol table to associate an attribute with a given entry.

Get_attribute Operation on Symbol table

Get_attribute Operation can be performed on a symbol table to get an attribute associated with a given entry.

Lookup Operation on Symbol table

Lookup Operation can be performed on a symbol table to search for a name and return a pointer to its entry.

Free Operation on Symbol table

Free Operation can be performed on a symbol table to remove all entries and free the storage of a symbol table.

Some other operations just like delete operations can also be performed on the symbol table.

 Implementation of Symbol table

Symbol tables can be implemented with different kinds of data structure techniques. Some of the techniques are mentioned below;

  1. LinkedList
  2. Hash Table
  3. Tree

A basic example of creating the symbol table of C++ Code

int t4tutorials(int x, int y)
{
int addition = 0;
addition = x + y;
return sum;
}

Symbol Table for the code shown above.

NAME TYPE SCOPE
t4tutorials function global
x int function parameter
y int function parameter
addition int local

Symbol table implementation with hashing in C++

Basic Symbol table Program in C

Write a program for implementing a Symbol Table using C.

Output

Symbol table Program in C
Symbol table Program in C

Lab Exercises of Compiler Construction

Compiler Construction MCQs