0% found this document useful (0 votes)
10 views

Runtime Environment

Uploaded by

vakame5133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Runtime Environment

Uploaded by

vakame5133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Injibara University

Department of Computer Science


Compiler Design (CoSc3112)

Chapter 7: Run time- Environments


Minychil F.
Contents
Chapter 7: Run time- Environments (4hr)
7.1. Symbol table

7.2. Hash Table

7.3. Representing Scope Information


Runtime Environment
• What is Runtime Environment?
➢It is the environment created and managed by compiler in which the compiler
assumes that its target programs are being executed.

• Need of the Runtime environment


➢To successfully implement the various issues of the source languages, like names,
scope, bindings, data types, procedures, parameters, etc.. And also the compiler
must be cooperate with OS.
➢The compiler must also work together with the other system software to support
the issues on the target machine
Runtime Environment
• What are the issues allocated by Runtime Environment?
• Issues handled by runtime environments are:

• Layout and allocation of storage locations for objects named in the source program.

• Mechanisms used by target program to access variables.

• Linkage between procedures.

• Mechanisms for passing parameters.

• Interface to the operating system, I/O devices and other programs.


7.1. Symbol table
▪ Definition: The symbol table is defined as the set of Name and Value pairs.

▪ Symbol Table is an important data structure created and maintained by the


compiler in order to keep track of semantics of variables
• i.e. it stores information about the scope and binding information about names, information
about instances of various entities such as variable and function names, classes, objects, etc.

▪ It is built-in lexical and syntax analysis phases.

▪ The information is collected by the analysis phases of the compiler and is used by
the synthesis phases of the compiler to generate code.

▪ It is used by the compiler to achieve compile-time efficiency.


Cont…
It is used by various phases of the compiler as follows:-
1. Lexical Analysis: Creates new table entries in the table, for example like entries about tokens.
2. Syntax Analysis: Adds information regarding attribute type, scope, dimension, line of reference,
use, etc in the table.
3. Semantic Analysis: Uses available information in the table to check for semantics i.e. to verify that
expressions and assignments are semantically correct(type checking) and update it accordingly.
4. Intermediate Code generation: Refers symbol table for knowing how much and what type of run-
time is allocated and table helps in adding temporary variable information.
5. Code Optimization: Uses information present in the symbol table for machine-dependent
optimization.
6. Target Code generation: Generates code by using address information of identifier present in the
table.
Cont…
▪ Symbol Table entries:
• Each entry in the symbol table is associated with attributes that support the compiler in different phases.

▪ Use of Symbol Table:


• The symbol tables are typically used in compilers. Basically compiler is a program which scans the application
program (for instance: your C program) and produces machine 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

➢Compiler generated temporaries ➢ If structure or record then, a pointer to structure


table.
➢Labels in source languages
➢ For parameters, whether parameter passing by
value or by reference
➢ Number and type of arguments passed to function
➢ Base Address
Cont…
▪ Operations of Symbol table:
➢The basic operations defined on a symbol table include:
Cont…
• Use and Need of symbol tables
▪ Symbol table is an important data structure created and maintained by compilers in order to
store information about the occurrence of various entities such as variable names, function
names, objects, classes, interfaces, etc.
▪ Symbol table is used by both the analysis and the synthesis parts of a compiler.
▪ A symbol table may serve the following purposes depending upon the language in hand:
➢ To store the names of all entities in a structured form at one place.
➢ To verify if a variable has been declared.
➢ To implement type checking, by verifying assignments and expressions in the source code are
semantically correct.
➢ To determine the scope of a name (scope resolution).
Cont…
Operations on Symbol Table :

• Following operations can be performed on symbol table-


1. Insertion of an item in the symbol table.

2. Deletion of any item from the symbol table.

3. Searching of desired item from symbol table.


Implementation of symbol table
▪ If a compiler is to handle a small amount of data, then the symbol table can be
implemented as an unordered list, which is easy to code, but it is only suitable for
small tables only.
▪ A symbol table can be implemented in one of the following ways:
1. Linear (sorted or unsorted) list
2. Binary Search Tree
3. Hash table

▪ 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.

You might also like