Compiler Design · Symbol Table
Example Symbol Table Entry and Line-of-Use Linked List
Overview
A symbol table entry stores every attribute of a single identifier (variable, array, etc.) as fields within the entry.
Because some attributes — most notably the lines at which an identifier is used — are not of fixed size, the entry holds a pointer to a linked list of use-lines rather than a fixed-size field.
Key concepts
- Symbol table entry: stores all the attributes of one identifier (variable, array, etc.); the attributes are stored as fields in the entry.
- Name: the identifier's name.
- Type: the data type of the identifier.
- Size: the size of the identifier (e.g., the number of elements for an array).
- Dimension: the number of dimensions (1 for a one-dimensional array).
- Line number: the line at which the identifier is declared.
- Line of uses: the lines at which the identifier is used, stored as a pointer to a linked list.
- Address: the memory address at which the variable is stored.
- Line-of-uses linked list: each node stores one line at which the identifier occurs; the entry keeps a pointer to the head of this list.
It is used when a variable is declared at more than one place or used at more than one place. - Variable-size attributes: the attributes are not of fixed size; different attributes can have different sizes. The line-of-uses field can vary in length, which is why it is kept as a linked list rather than a fixed-size field.
How it works
- Take an array named Ravi of type character.
- Record its size as 4 and its dimension as 1, because it is a one-dimensional array.
- Record the line number from the line at which the array is declared.
- Record the line of uses from the lines at which the array is used. A variable such as i may be used at 10 places, so the line-of-uses field can hold many values.
- Because the number of uses is not known in advance, implement the line-of-uses field as a linked list: each node stores one line at which the identifier occurs, and the entry keeps a pointer to the head of the list.
The pointer is used when a variable is used at more than one place. - Record the address field, which gives the address at which the variable is stored.
- Note that the attributes are not of fixed size; for example, the line-of-uses field can vary in length, which is why it is a linked list rather than a fixed-size field.
Worked examples
Example entry for the array Ravi:
| Field | Value |
|---|---|
| Name | Ravi |
| Type | character |
| Size | 4 |
| Dimension | 1 |
| Line number | line at which Ravi is declared |
| Line of uses | pointer to a linked list; each node holds one line at which Ravi is used |
| Address | address at which Ravi is stored |
Line-of-uses linked list: if a variable i is used at 10 places, the entry's line-of-uses pointer points to a linked list with 10 nodes, one node per line of use.
Revision summary
- A symbol table entry holds the name, type, size, dimension, declaration line number, line of uses, and address of an identifier.
- The line-of-uses field is a single field in the entry, but it is implemented as a linked list because the number of uses is not known in advance.
- Each node of the line-of-uses linked list stores one line at which the identifier occurs.
- The entry stores a pointer to the linked list when a variable is used at more than one place.
- The address field gives the storage address of the variable.
- Symbol table attributes are not all of fixed size; for example, the line-of-uses field can vary in size.
Previous-year exam insights
- A symbol table entry is a record whose fields are the identifier's attributes (name, type, size, dimension, declaration line, address, and a pointer to the list of use-lines); the table itself is usually implemented as a hash table, search tree, or ordered/unordered array of such records.
- The line-of-uses list is a linked list of line numbers; the entry stores only the head pointer, so the entry size stays fixed while the number of uses can grow arbitrarily.
- For an array, the size field gives the total number of elements and the dimension field gives the number of dimensions; the total storage is the product of the dimension bounds times the element size.
- The address field holds the address assigned to the identifier (base address for an array); for arrays the address of an element is computed as base + (offset) x element size using the dimension bounds.
- Symbol table operations are insert (on declaration), lookup/search (on use), and sometimes delete (on scope exit); the table must support these efficiently, which is why hashing gives average lookup.
- Attributes that vary in size (such as the list of use-lines or a variable-length name) are stored indirectly via pointers, keeping every entry the same fixed size so the table can be an array of records.