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

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 minutes read.

Syntax Analysis Compiler Design

Syntax Analysis This article will describe the parsing method used in the compiler. The grammatical rule of programming language can be constructed with the help of context-free grammars or BNF (Backus–Naur...

3 minutes read.

Phases of Compiler

Phases of Compiler The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input. Here we will see all...

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

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

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

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.

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.

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

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

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.

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.

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.

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.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

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.

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.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

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

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.