Compiler Design · Symbol Table
Attributes of a Symbol Table Entry
Overview
A symbol table is a table whose columns are called attributes; each row (entry) describes one entity (identifier) in the program. These attributes carry the information the compiler needs across its different phases, from type checking to error reporting and code generation.
Key concepts
- Attribute: a column of the symbol table. Each entry (row) is associated with these attributes, and they hold the pieces of information that support the compiler in different phases.
- Name: the name of the entity (identifier) being talked about.
- Size: the size of the entity — the size of an int, the size of a character, or, for an array, the size of the array.
- Dimension: used when the entity is an array — 0 when it is not an array, 1 for a one-dimensional array, 2 for a two-dimensional array.
- Type: the data type — integer, character, boolean, or any other type.
- Line of declaration: the line number at which that particular variable is declared.
The line at which the variable is used is stored as well, so that when the compiler reports an error it can also give the line at which the error occurred. - Address: the address of that particular entity.
- The exact set of attributes depends on the implementation: some programming languages may add a few more attributes and a few attributes may be deleted, but the six above are typically what is seen in most symbol tables.
How it works
- The symbol table is laid out as a table: the column names are the attributes, and each entry (row) is one entity.
- For each entity, record its name.
- Record its size — the size of its type for a scalar, the size of the array for an array.
- Record its dimension — 0 for a non-array, 1 for a one-dimensional array, 2 for a two-dimensional array.
- Record its type — integer, character, boolean, or any other type.
- Record the line of declaration, and also the line at which it is used, so error messages can point to the exact line where the error occurred.
- Record the address of the entity.
Worked examples
Attribute values for typical entities, showing how each column is filled:
| Attribute | Value for a non-array variable | Value for an array |
|---|---|---|
| Name | name of the entity | name of the entity |
| Size | size of its type (e.g., size of int, size of char) | size of the array |
| Dimension | 0 | 1 for one-dimensional, 2 for two-dimensional |
| Type | integer / character / boolean / etc. | element type |
| Line of declaration | line number where declared | line number where declared |
| Line of use | line number(s) where used (for error reporting) | line number(s) where used |
| Address | address of the entity | address of the entity |
Revision summary
- Columns of the symbol table = attributes of a symbol table entry.
- The six typical attributes are name, size, dimension, type, line of declaration, address; line of use is kept additionally for error reporting.
- Dimension is 0 for non-arrays, 1 for 1-D arrays, 2 for 2-D arrays.
- The line of declaration (and line of use) is stored so error messages can point to the exact line where the error occurred.
- The set of attributes varies with the language/implementation — some may be added, some removed.
Previous-year exam insights
- The symbol table is accessed in essentially all phases of the compiler (lexical analysis, syntax analysis, semantic analysis, intermediate/code generation), not only in lexical and syntax analysis.
Asked inGATE 2020 - Detection of 'variable used before declaration' is a semantic-analysis (type-checking) job, not a syntax-analysis job; syntax analysis only checks the grammar.
Asked inGATE 2020 - Recursion in a language requires a run-time stack for activation records, not heap storage; heap is needed only for dynamically allocated/run-time-sized data.
Asked inGATE 2020 - Standard applications of the symbol table: storage allocation, checking type compatibility, and suppressing duplicate error messages (also scope/name resolution and code generation).
Asked inUGC NET 2020UGC NET 2007UGC NET 2005 - The symbol table is the compiler data structure that manages information about variables/identifiers and their attributes; it is commonly implemented as a hash table (also linear list, search tree).
Asked inGATE 2010UGC NET 2019 - In a two-pass assembler the symbol table stores labels together with their assigned values/addresses (used to resolve forward references in pass 2).
Asked inUGC NET 2004