Compiler Design and Construction - Old Questions

5. “Symbol table is a necessary component of compiler”, justify this statement with examples.

5 marks | Asked in Model Question

Symbol tables are data structures that are used by compilers to hold information about source-program constructs. The information is collected incrementally by the analysis phase of a compiler and used by the synthesis phases to generate the target code. Entries in the symbol table contain information about an identifier such as its type, its position in storage, and any other relevant information.

Symbol tables typically need to support multiple declarations of the same identifier within a program. The lexical analyzer can create a symbol table entry and can return token to the parser, say id, along with a pointer to the lexeme. Then the parser can decide whether to use a previously created symbol table or create new one for the identifier.

The basic operations defined on a symbol table include

  • allocate – to allocate a new empty symbol table
  • free – to remove all entries and free the storage of a symbol table
  • insert – to insert a name in a symbol table and return a pointer to its entry
  • lookup – to search for a name and return a pointer to its entry
  • set_attribute – to associate an attribute with a given entry
  • get_attribute – to get an attribute associated with a given entry