CD Notes - Unit-5
CD Notes - Unit-5
UNIT-V
SYMBOL TABLES
A symbol table is a major data structure used in a compiler. Associates attributes with
identifiers used in a program. For instance, a type attribute is usually associated with each
identifier. A symbol table is a necessary component Definition (declaration) of identifiers
appears once in a program .Use of identifiers may appear in many places of the program text
Identifiers and attributes are entered by the analysis phases. When processing a definition
(declaration) of an identifier. In simple languages with only global variables and implicit
declarations. The scanner can enter an identifier into a symbol table if it is not already there
In block-structured languages with scopes and explicit declarations:
The parser and/or semantic analyzer enter identifiers and corresponding attributes
Symbol table information is used by the analysis and synthesis phases
To verify that used identifiers have been defined (declared)
To verify that expressions and assignments are semantically correct – type checking
To generate intermediate or target code
free – to remove all entries and free the storage of a symbol table
insert – to insert a name in a symbol table and return a pointer to its entry lookup – to search
for a name and return a pointer to its entry
get_attribute – to get an attribute associated with a given entry Other operations can be added
depending on requirement For example, a delete operation removes a name previously inserted Some
identifiers become invisible (out of scope) after existing a block.
- 77 -
Instructor: P Naga Deepthi
- 78 -
Instructor: P Naga Deepthi
HASH FUNCTIONS
_ Strings longer than one word are folded using exclusive-or or addition
RUNTIME ENVIRONMENT
forms text or code segment of your program: size known at compile time.
o Data objects:
- 79 -
Instructor: P Naga Deepthi
Code: Program
Instructions
Stack: Manage activation of procedures at runtime.
Heap: holds variables created dynamically
STORAGE ORGANIZATION
1.Fixed-size objects can be placed in predefined locations.
- 80 -
Instructor: P Naga Deepthi
Activation records
Any information needed for a single activation of a procedure is stored in the
ACTIVATION RECORD (sometimes called the STACK FRAME). Today, we‟ll assume the
stack grows DOWNWARD, as on, e.g., the Intel architecture. The activation record gets
pushed for each procedure call and popped for each procedure return.
STATIC ALLOCATION
Statically allocated names are bound to storage at compile time. Storage bindings of
statically allocated names never change, so even if a name is local to a procedure, its name is
always bound to the same storage. The compiler uses the type of a name (retrieved from the
symbol table) to determine storage size required. The required number of bytes (possibly
aligned) is set aside for the name.The address of the storage is fixed at compile time.
Limitations:
Stack-dynamic allocation
Storage is organized as a stack.
Activation records are pushed and popped.
Locals and parameters are contained in the activation records for the call.
This means locals are bound to fresh storage on every call.
If we have a stack growing downwards, we just need a stack_top pointer.
To allocate a new activation record, we just increase stack_top.
To deallocate an existing activation record, we just decrease stack_top.
- 81 -
Instructor: P Naga Deepthi
HEAP ALLOCATION
Some languages do not have tree-structured allocations. In these cases, activations
have to be allocated on the heap. This allows strange situations, like callee activations that
live longer than their callers‟ activations. This is not common Heap is used for allocating
space for objects created at run timeFor example: nodes of dynamic data structures such as
linked lists and trees
�Dynamic memory allocation and deallocation based on the requirements of the
programmalloc() and free() in C programs
new()and delete()in C++ programs
new()and garbage collection in Java programs
An example:
main()
{ int x = 4;
int f (int y) {
return x*y;
}
int g (int →int h){
int x = 7;
return h(3) + x;
}
- 82 -
Instructor: P Naga Deepthi
g(f);//returns 12
}
- 83 -