Symbol Table
. Stores information for subsequent phases
. Interface to the symbol table
- Insert(s,t): save lexeme s and token t and return pointer
- Lookup(s): return index of entry for lexeme s or 0 if s is not found
Implementation of symbol table
. Fixed amount of space to store lexemes. Not advisable as it waste space.
. Store lexemes in a separate array. Each lexeme is separated by eos. Symbol table has pointers to lexemes.
A data structure called symbol table is generally used to store information about various source language constructs. Lexical analyzer stores information in the symbol table for the subsequent phases of the compilation process. The symbol table routines are concerned primarily with saving and retrieving lexemes. When a lexeme is saved, we also save the token associated with the lexeme. As an interface to the symbol table, we have two functions
- Insert( s , t ): Saves and returns index of new entry for string s , token t .
- Lookup( s ): Returns index of the entry for string s , or 0 if s is not found.
Next, we come to the issue of implementing a symbol table. The symbol table access should not be slow and so the data structure used for storing it should be efficient. However, having a fixed amount of space to store lexemes is not advisable because a fixed amount of space may not be large enough to hold a very long identifier and may be wastefully large for a short identifier, such as i . An alternative is to store lexemes in a separate array. Each lexeme is terminated by an end-of-string, denoted by EOS, that may not appear in identifiers. The symbol table has pointers to these lexemes.
|