Runtime Environment
Runtime Environment
• Layout and allocation of storage locations for objects named in the source program.
▪ The information is collected by the analysis phases of the compiler and is used by
the synthesis phases of the compiler to generate code.
▪ During this scan compiler stores the identifiers of that application program in the symbol table. These
identifiers are stored in the form of name, value address, type.
▪ Here the name represents the name of identifier, value represents the value stored in an identifier,
the address represents memory location of that identifier and type represents the data type of
identifier.
▪ Thus compiler can keep track of all the identifiers with all the necessary information.
Cont…
▪ Items stored in Symbol table: ▪ Information used by the compiler from
Symbol table:
➢Variable names and constants
➢ Data type and name
➢Procedure and function names
➢ Declaring procedures
➢Literal constants and strings ➢ Offset in storage
▪ Among all, symbol tables are mostly implemented as hash tables, where the
source code symbol itself is treated as a key for the hash function and the return
value is the information about the symbol.
Cont…
▪ Linear (sorted or unsorted) list:
➢In this implementation, the symbol table is represented as a list, either sorted or
unsorted.
➢Each entry in the list consists of a symbol (such as a variable or function name) and
its associated value or information.
➢In a sorted list, the symbols are arranged in a specific order (e.g., alphabetical order)
to facilitate efficient searching using techniques like binary search.
➢In an unsorted list, symbols are appended to the list without any specific order, and
searching requires iterating through the list linearly.
Cont…
▪ Binary Search Tree (BST):
▪ A BST is a binary tree data structure where each node has a key-value pair.
▪ The nodes are organized in such a way that the left subtree of a node contains keys
smaller than the node's key, and the right subtree contains keys greater than the node's
key.
▪ In a symbol table implemented as a BST, each symbol is associated with a value and
stored in the tree based on its key.
▪ Searching, insertion, and deletion operations in the symbol table can be performed
efficiently in logarithmic time complexity.
Advantage and disadvantage of Symbol Table
▪ Advantages of Symbol Table
1. The efficiency of a program can be increased by using symbol tables, which give
quick and simple access to crucial data such as variable and function names, data kinds,
and memory locations.
2. Better coding structure Symbol tables can be used to organize and simplify code,
making it simpler to comprehend, discover, and correct problems.
3. Faster code execution: By offering quick access to information like memory addresses,
symbol tables can be utilized to optimize code execution by lowering the number of
memory accesses required during execution.
4. Symbol tables can be used to increase the portability of code by offering a
standardized method of storing and retrieving data, which can make it simpler to
migrate code between other systems or programming languages.
5. Improved code reuse: By offering a standardized method of storing and accessing
information, symbol tables can be utilized to increase the reuse of code across multiple
projects.
6. Symbol tables can be used to facilitate easy access to and examination of a program’s
state during execution, enhancing debugging by making it simpler to identify and correct
mistakes.
Advantage and disadvantage of Symbol Table
▪ Disadvantages of Symbol Table
1. Increased memory consumption: Systems with low memory resources may suffer from symbol
tables’ high memory requirements.
2. Increased processing time: The creation and processing of symbol tables can take a long time, which
can be problematic in systems with constrained processing power.
3. Complexity: Developers who are not familiar with compiler design may find symbol tables difficult to
construct and maintain.
4. Limited scalability: Symbol tables may not be appropriate for large-scale projects or applications that
require o the management of enormous amounts of data due to their limited scalability.
5. Upkeep: Maintaining and updating symbol tables on a regular basis can be time- and resource-
consuming.
6. Limited functionality: It’s possible that symbol tables don’t offer all the features a developer needs,
and therefore more tools or libraries will be needed to round out their capabilities.
Application of Symbol Table
1. Resolution of variable and function names: Symbol tables are used to identify the data types and
memory locations of variables and functions as well as to resolve their names.
2. Resolution of scope issues: To resolve naming conflicts and ascertain the range of variables and
functions, symbol tables are utilized.
3. Symbol tables, which offer quick access to information such as memory locations, are used to
optimize code execution.
4. Code generation: By giving details like memory locations and data kinds, symbol tables are utilized
to create machine code from source code.
5. Error checking and code debugging: By supplying details about the status of a program during
execution, symbol tables are used to check for faults and debug code.
6. Code organization and documentation: By supplying details about a program’s structure, symbol
tables can be used to organize code and make it simpler to understand.
7.2. Hash Table
▪ A hash table (or hash map) is a data structure that uses a hash function to map keys to an
array index.
▪ In a symbol table implemented as a hash table, symbols are hashed to compute their
corresponding array index, and the associated values are stored at those indices.
▪ Hash tables provide fast access to values based on their keys, typically with constant time
complexity on average.
▪ However, collisions (two or more symbols hashing to the same index) need to be handled
to ensure correct retrieval of values.
How hash table operated in the symbol table?
▪ In the context of symbol tables, a hash table can be used to store the information about the
occurrence of various entities such as objects, classes, variable names, interface names,
and function names.
▪ The symbol table is used by both the analysis and synthesis phases of a compiler.
▪ To use a hash table as a symbol table, the compiler needs to perform the following
operations:
➢ Insert(): This operation is used to insert a new name into the symbol table with its attributes.
➢ Lookup(): This operation is used to search a name in the symbol table. It is used to determine if a name
is multiply declared or to link a use with the corresponding symbol-table entry.
How hash table operated in the symbol table?
▪ The hash table is usually used to organize a symbol table, where the keyword or identifier
is 'hashed' to produce an array subscript.
▪ The time for searching in hash tables is independent of the number of elements stored in
the table, so it is efficient for a large number of elements.
7.3. Representing Scope Information
▪ In the source program, every name possesses a region of validity, called the scope of
that name.
▪ The rules in a block-structured language are as follows:
➢If a name declared within block B then it will be valid only within B.
➢If B1 block is nested within B2 then the name that is valid for block B2 is also valid for B1
unless the name's identifier is re-declared in B1.
➢These scope rules need a more complicated organization of symbol table than a list of
associations between names and attributes.
➢Tables are organized into stack and each table contains the list of names and their
associated attributes.
➢Whenever a new block is entered then a new table is entered onto the stack. The new table
holds the name that is declared as local to this block.
➢When the declaration is compiled then the table is searched for a name.
➢If the name is not found in the table then the new name is inserted.
➢When the name's reference is translated then each table is searched, starting from the each
table on the stack.
7.3. Representing Scope Information
• For example:
int x;
void f(int m) {
float x, y;
{
int i, j;
int u, v;
}
}
int g (int n)
{
bool t;
}
Fig: Symbol table organization that complies with static scope information rules
Cont…
▪ To manage these rules, tables can be organized into a stack, with each table
containing a list of names and their associated attributes.
▪ When a new block is entered, a new table is added to the stack, holding the names
declared as local to that block.
▪ During declaration compilation, the table is searched for the name. If the name is not
found, a new name is inserted.
▪ When translating a name reference, each table is searched, starting from the most
recent one on the stack.