Unit 4 Symbol Table
Unit 4 Symbol Table
Symbol Table,
Run-time Storage Administration,
Error Detection and Recovery
Symbol Table
• Symbol table is an important data structure used in a compiler in order
to keep track of semantics of variables
• It stores information about the scope and binding information about
names, information about instances of various entities
• 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
Used of symbol table in various phases of
compiler
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.
Symbol Table Entries
• Items stored in Symbol table: • Information used by the compiler
• Variable names from Symbol table:
• Constants • Data type and name
• Declaring procedures
• Procedure name
• Offset in storage
• Function names • If structure or record then, a pointer to
• Literal constants and strings structure table.
• Compiler generated temporaries • For parameters, whether parameter
passing by value or by reference
• Labels in source languages • Number and type of arguments passed to
function
• Base Address
Basic operations of Symbol table
Feature of Symbol Table
• Insert
• Delete
• Lookup
• Modify
Name Representation in Symbol Table
0 10
10 4
14 2
16 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
S U M M A T I O N $ S U M $ N $ N 1 $
Data Structure used for Symbol Table
• Requirements for symbol table:
• For quick insertion of identifier and related information
• For quick searching of identifier
Available
(start of empty slot)
2. Self Organizing List
• Linked list is used.
• A link field is added to each record.
• We search the records in the order pointed by the link of link field.
• A pointer “first” is maintained to point to first record of the symbol
table.
Name 1 Info 1
Name 2 Info 2
1. Static allocation
2. Stack Allocation
3. Heap Allocation
1. Static Allocation
• Size of data objects is known at compile time.
• Binding of object with allocated storage do not change. (Static
Allocation)
• In static allocation, the compiler can determine the amount of storage
required by each data object. (Easy to find the address of data objects
in activation record)
Limitations of Static Allocation
• Static allocation can be done only if the size of data object is known
at compile time.
• The data structure cannot be created dynamically. (static allocation
cannot manage the allocation of memory at run-time).
• Recursive procedures are not supported by static allocation.
2. Stack Allocation
• Storage is organized as STACK (LIFO).
• This stack is called control stack.
• As activation begins, the activation records are pushed into the stack and no
completion of this activation, the corresponding activation records can be popped.
• The locals are stored in each activation records. Hence locals are bound to
corresponding activation record on each fresh activation.
• The data structure cab be created dynamically for stack allocation.
Limitations:
• The memory addressing can be done using pointer and index registers. Hence the
stack allocation is slower than static allocation
3. Heap Allocation
• If the values of non local variables must retained even after the activation
record then such a retaining is not possible in stack allocation. For this
limitation, we use heap allocation
• Based on Linked list
• Dynamic allocation
• Heap allocation allocates the continuous block of memory when required
for storage of activation records or other data objects (malloc).
• Free function is used for deallocation.
• Efficient allocation strategy:
• Creates a linked list for free blocks and when any memory is deallocated that block
of memory is appended in the linked list.
• Allocates the most suitable block of memory from linked list (Use best fit technique
for the allocation of block)
Activation Record
• Manages the information needed by a single execution of a procedure.
1. Local Data
2. Non-Local Data
Local Data
• The local data can be accessed with the help of activation record.
C()
{ int k; A();}
A()
{ int d; d=1;}
Display
p0()
{
p1()
{
p2()
{….}
}
p3()
{
p4()
{…..}
}
}
Error detection and Recovery in Compiler
• In this phase of compilation, all possible errors made by the user are
detected and reported to the user in form of error messages. This
process of locating errors and reporting it to user is called Error
Handling process.
• Functions of Error handler
• Detection
• Reporting
• Recovery
Classification of Errors
Lexical phase errors
• errors detected during the lexical analysis phase.