Module 4
Module 4
Symbol table: Symbol table format, organization of symbol table-Linear, hashing ,tree.
Symbol Table
he symbol table acts as a bridge between the analysis and synthesis phases of the compiler.
It collects information during the analysis phases and utilizes it during the synthesis phases
to generate efficient code, ultimately enhancing compile-time performance.
Lexical Analysis: Creates new table entries in the table, for example, entries about
tokens.
Syntax Analysis: Adds information regarding attribute type, scope, dimension, line of
reference, use, etc in the table.
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.
Intermediate Code Generation: Refers to symbol table for knowing how much and
what type of run-time is allocated and table helps in adding temporary variable
information.
Code Optimization: Uses information present in the symbol table for machine-
dependent optimization.
Target Code generation: Generates code by using the address information of the
identifier present in the table.
Symbol Table entries - Each entry in the symbol table is associated with attributes
that support the compiler in different phases.
Organization of Symbol Table: Linear
The Linear Symbol Table is the simplest way to organize a symbol table. It stores entries in a
linear list (array or linked list) and performs operations like insertion, lookup, and deletion
using linear search.
Hashing
Hashing is a technique that uses a hash function to map an identifier (name) to an index in a
hash table.
omponents:
🔹 Collision Handling:
Left child < root < right child (based on lexeme name).
🔹 Self-Balancing Trees:
Storage Allocation
compiler is a program that converts HLL(High-Level Language) to LLL(Low-Level
Language) like machine language. It is also responsible for organizing the memory
layout of a program by allocating space for global and local variables, constants, and
dynamic data structures.
As the program begins execution, it is under the control of the operating system,
which sets up the environment by allocating space for the whole process.
Compiler is responsible for deciding which variable gets which part of the memory.
For example, the compiler needs to decide whether variables are best placed in the
stack (for local variables) or in the heap (for dynamically allocated memory) or data
segment (global and stack variables)
Activation Record
An Activation Record is a data structure that is activated/ created when a
procedure/function is invoked, and it includes the following data about the function.
Saved machine status: holds info about machine status before procedure call
The activation record is used to store the information required by a single procedure
call. Not all the fields shown in the figure may be needed for all languages. The
record structure can be modified as per the language/compiler requirements. For
Pascal and C, the activation record is generally stored on the runtime stack during the
period when the procedure is executing. Of the fields shown in the figure, access link
and control link are optional (e.g. FORTRAN doesn't need access links). Also, actual
parameters and return values are often stored in registers instead of the activation
record, for greater efficiency.
The activation record for a procedure call is generated by the compiler. Generally,
all field sizes can be determined at compile time.
Here, Old SP stores the value of stack pointer of Activation Record of procedure
which has called this procedure which leads to the generation of this Activation
Record, i.e., It is a pointer to the activation record of the caller.
Heap Allocation
Heap allocation is the most flexible allocation scheme. Allocation and deallocation of
memory can be done at any time and any place depending upon the user's requirement.
Heap allocation is used to allocate memory to the variables dynamically and when the
variables are no more used then claim it back.
Heap management is specialized in data structure theory. There is generally some time and
space overhead associated with heap manager. For efficiency reasons, it may be useful to
handle small activation records of a particular size as a special case
There are two methods used for Heap management are as follows −
When the all-access path to an object is destroyed, but data object continues to exist, such
types of objects are said to be garbage. Garbage collection is a technique that is used to
reuse that object space.
In garbage collection, firstly, we mark all the active objects, and all the remaining elements
whose garbage collection is 'on' are garbaged and returned to the free space list.
Reference Counter
Each memory cell on the heap has a reference counter associated with it that contains a
count of the number of values that points to it. The count has incremented each time a new
value point to the cell and decremented each time an amount ceases to point to it. When
the counter becomes zero, the cell is returned to the free list for further allocation.
Space Efficiency− A memory manager should minimize the total heap space needed
by a program.
Program Efficiency− A memory manager should make good use of the memory
subsystem to allow programs to run faster. As the time taken to execute an
instruction can vary widely depending on where objects are placed in memory.