Compiler Design and Construction - Old Questions

10.  Discuss the importance of symbol table in computer. How is it manipulated in the different phases of compilation?

6 marks | Asked in 2073

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 depending upon the language in hand:

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

It is used by various phases of compiler as follows:

  • Lexical Analysis: Creates new table entries in the table, example like entries about token.
  • Syntax Analysis: Adds information regarding attribute type, scope, dimension, line of reference, use, etc. in the table.
  • Semantic Analysis: Uses available information in the table to check for semantics i.e. to verify that expressions and assignments are semantically correct (type checking) and update it accordingly.
  • Intermediate Code generation: Refers symbol table for knowing how much and what type of run-time is allocated and table helps in adding temporary variable information.
  • Code Optimization: Uses information present in symbol table for machine dependent optimization.
  • Target Code generation: Generates code by using address information of identifier present in the table.