Symbol Table Compiler Design

Symbol Table

It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc. 

The purpose of the symbol table is given below: 

  • It stores the name of different entities in the structured form in one place. 
  • It checks the variable has been initialized or not.
  • It is used to determine scope resolution.
  • They are used by the compiler to achieve compile-time efficiency.

Syntax of symbol table:

<symbol name, type attribute>

Example

Suppose a variable store the information about the following variable declaration as follows:

static int marks;

Then, it will store information in this way:

<marks, int, static>

A various phase of the compiler uses the symbol table:

  1. Lexical Analysis– Maintain new entries in the table. 
  • Syntax Analysis– Add the information about the scope, dimension, attribute type, use, etc., in the table.
  • Semantic Analysis– It uses all information present in the table to check semantics by verifying that expression and assignment are semantically correct or not and then update it.
  • Intermediate Code Generation– To check how much and what type of run-time is allocated to help in adding temporary variable information.
  • Code optimization– It uses information for machine-dependent optimization.
  • Code Generation– Generated code by using identifiers available in the symbol table.

Implementation

The implementation of the symbol table can be done in any of the following ways:

  • Linear (sorted or unsorted ) List
  • Hash table
  • Binary search tree (BST)

1. List

  • It uses an array to store the name and associated information.
  • A pointer “Present" is marked at the end of all stored records, and new names are added in the same order as they arrived.
  • To find any name, we will search from the beginning of the list till the present pointer. If the match wasn't found in the table, it gave an error message as the use of an undeclared name.
  • If we insert any new name, we must ensure that the name should not already present otherwise, occur an error.
  • Insertion operation is fast, whereas lookup is slow for large tables.
  • It takes less amount of space.

2. Linked List

  • A link field is inserted into every record.
  • A pointer “XYZ" is marked to designate the first name of the symbol table.
  • Insertion operation is fast, whereas lookup is slow for large tables.

3. Hash Table

  • There have to be two tables in the hashing technique– a hash table and a symbol table. 
  • A table with an array of index range 0 to table size-1 is known as a hash table.
  • A hash function can search any name.
  • Insertion operation and lookup operation can be made very fast.

4. Binary Search Tree

  • A binary search tree is also a method to implement a symbol table.
  • Names are created as child nodes of root and obey the binary search tree's property.
  • Insertion operation and lookup operation are O(log2 n) on average.

Symbol Table Operations:

The following are the operations of the symbol table:

Insert()

  • The compiler's analysis phases often use this operation, where tokens are identified and kept in the table.
  • This is used to insert information about the unique name occurring in the source code.
  • It takes the symbol and attributes as an argument and stores the information in a symbol table.

Example:

int b;

should be proceeded by the compiler as:

insert(b, int);

loockup()

This operation is used to discover a name in the symbol table to detect:

  • If any symbol in the table exists.
  • A symbol is declared before being used or not.
  • Name is used in the scope or not.
  • Symbol’s initialization.
  • Check whether the name is declared more than once.

The format of the lookup() function is:

lookup(symbol)

The format may be distinct for different programming languages.


Related Topics

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 minutes read.

Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator...

4 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.

Target Machine

Target Machine A target machine is a byte-addressable machine. This machine has n general-purpose registers, R0, R1,.....Rn-1. A Simple Target Machine Model has three-address instruction. A full-edged assembly language would have...

3 minutes read.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

8 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Finite State Machine

Finite State Machine A finite state machine is a simple machine to recognize patterns. It takes a string of symbols as the input and changes its state to another state, but...

3 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 minutes read.

Basic Blocks and Flow Graphs in Compiler Design

Basic Blocks and Flow Graphs In this section, we are going to learn how to work with basic block and flow graphs in compiler design. Basic Block The basic block is a set...

3 minutes read.

LALR 1 Parsing | Compiler Design

LALR (1) Parsing The LALR parsing refers to the "lookahead LR" that has many lesser steps than typical parsers based on LR(1) items. For constructing the LALR(1) parsing table, the canonical...

6 minutes read.

Switch Case Statement Compiler Design

Case Statement The “case” or “switch” statement is available in various languages. The following is the syntax for the case statement: switch (E)  {             case V1: S1             case V2: S2 ...

1 minute read.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

4 minutes read.

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

3 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

3 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.