|
Symbol table
The symbol table is a data structure containing a record for each variable name, with fields for the attributes of the name. The data structure should be designed to allow the compiler to find the record for each name quickly and to store or retrieve data from that record quickly.
-
The symbol table is an important data structure that is used in a compiler.
-
The information is collected incrementally by the analysis phases of a compiler and used by the synthesis phases to generate the target code.
-
The symbol table is used to store the information about the occurrence of various entities such as objects, classes, variable names, interfaces, function names, etc.
-
it is used in both the analysis and synthesis phases.
-
Entries in the symbol table contain information about an identifier such as its character string (or lexeme), its type, its position in storage, and any other relevant information.
Example 1:
void main()
{
int ng;
}
S.No.
|
Name
|
Type
|
Size
|
Address
|
Properties..
|
1.
|
ng
|
int
|
4
|
011011010
|
…………
|
Example 2:
extern double var(double a); // Declare an external function
double fun(int count) // Define a public function
{
double sum = 0.0;
for (int i = 1; i <= count; i++)
sum += var((double) i);
return sum;
}
The symbol table for the above code:
Symbol name
|
Type
|
Scope
|
var
|
function, double
|
extern
|
a
|
double
|
function parameter
|
fun
|
function, double
|
global
|
count
|
int
|
function parameter
|
sum
|
double
|
block local
|
i
|
int
|
for-loop statement
|
Who Creates Symbol-Table Entries?
Symbol-table entries are created and used during the analysis phase by the lexical analyzer, the parser, and the semantic analyzer.
Phases of Compiler:

The uses of the symbol table:
-
It is used to store the name of all entities in one place.
-
It is used to verify if a variable has been declared or not.
-
It is used to determine the scope of a name.
-
It is used to implement type checking by verifying assignments and expressions in the source code are semantically correct or not.
Format of symbol table to maintain the entries for each name.
<symbol name, type, attribute>
Example: static int ngtuto
Then it should store the entry such as
<ngtuto, int, static>
|