0% found this document useful (0 votes)
70 views7 pages

CD Notes - Unit-5

A symbol table is a major data structure used in a compiler to associate attributes with identifiers used in a program. Symbol tables allow compilers to check for correctly declared and typed identifiers. There are various techniques for implementing symbol tables, with hash tables being commonly used due to fast insertion and lookup times of O(1). At runtime, programs are organized with a code segment, stack, and heap. The stack stores procedure activations and parameters via activation records, while the heap dynamically allocates data structures. Parameters can be passed by value or reference depending on the language.

Uploaded by

Balaji Dande
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)
70 views7 pages

CD Notes - Unit-5

A symbol table is a major data structure used in a compiler to associate attributes with identifiers used in a program. Symbol tables allow compilers to check for correctly declared and typed identifiers. There are various techniques for implementing symbol tables, with hash tables being commonly used due to fast insertion and lookup times of O(1). At runtime, programs are organized with a code segment, stack, and heap. The stack stores procedure activations and parameters via activation records, while the heap dynamically allocates data structures. Parameters can be passed by value or reference depending on the language.

Uploaded by

Balaji Dande
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/ 7

Instructor: P Naga Deepthi

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

 Symbol Table Interface

The basic operations defined on a symbol table include:

 allocate – to allocate a new empty symbol table

 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

 set_attribute – to associate an attribute with a given 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

This interface provides an abstract view of a symbol table


Supports the simultaneous existence of multiple tables
Implementation can vary without modifying the interface
Basic Implementation Techniques
First consideration is how to insert and lookup names
Variety of implementation techniques
Unordered List
Simplest to implement
Implemented as an array or a linked list
Linked list can grow dynamically – alleviates problem of a fixed size array
Insertion is fast O(1), but lookup is slow for large tables – O(n) on average
Ordered List
If an array is sorted, it can be searched using binary search – O(log2 n)
Insertion into a sorted array is expensive – O(n) on average
Useful when set of names is known in advance – table of reserved words
Binary Search Tree
Can grow dynamically
Insertion and lookup are O(log2 n) on average

HASH TABLES AND HASH FUNCTIONS


 A hash table is an array with index range: 0 to TableSize – 1
 Most commonly used data structure to implement symbol tables
 Insertion and lookup can be made very fast – O(1)
 A hash function maps an identifier name into a table index
 A hash function, h(name), should depend solely on name
 h(name) should be computed quickly
 h should be uniform and randomizing in distributing names
 All table indices should be mapped with equal probability.
 Similar names should not cluster to the same table index

- 78 -
Instructor: P Naga Deepthi

HASH FUNCTIONS

_ Hash functions can be defined in many ways . . .

_ A string can be treated as a sequence of integer words

_ Several characters are fit into an integer word

_ Strings longer than one word are folded using exclusive-or or addition

_ Hash value is obtained by taking integer word modulo TableSize

_ We can also compute a hash value character by character:

_ h(name) = (c0 + c1 + … + cn–1) mod TableSize, where n is name length

_ h(name) = (c0 * c1 * … * cn–1) mod TableSize

_ h(name) = (cn–1 + cn–2 + … + c1 + c0))) mod TableSize

_ h(name) = (c0 * cn–1 * n) mod TableSize

RUNTIME ENVIRONMENT

 Runtime organization of different storage locations

 Representation of scopes and extents during program execution.

 Components of executing program reside in blocks of memory (supplied by OS).

 Three kinds of entities that need to be managed at runtime:

o Generated code for various procedures and programs.

forms text or code segment of your program: size known at compile time.

o Data objects:

Global variables/constants: size known at compile time


Variables declared within procedures/blocks: size known
Variables created dynamically: size unknown.
o Stack to keep track of procedure activations.
Subdivide memory conceptually into code and data areas:

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

2. Run-time stack and heap


The STACK is used to store:
o Procedure activations.
o The status of the machine just before calling a procedure, so that the status can be
restored when the called procedure returns.
o The HEAP stores data allocated under program control (e.g. by malloc() in C).

- 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:

− The size required must be known at compile time.


− Recursive procedures cannot be implemented as all locals are statically
allocated.

− No data structure can be created dynamically as all data is static.

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

 Address generation in stack allocation


The position of the activation record on the stack cannot be determined statically.
Therefore the compiler must generate addresses RELATIVE to the activation record. If we
have a downward-growing stack and a stack_top pointer, we generate addresses of the form
stack_top + offset

- 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

�Allocation and deallocation may be completely manual (C/C++), semi-automatic(Java), or


fully automatic (Lisp)
PARAMETERS PASSING
A language has first-class functionsif functions can bedeclared within any scope
passed as arguments to other functions returned as results of functions.�In a language with
first-class functions and static scope, a function value is generally represented by a closure. a
pair consisting of a pointer to function code a pointer to an activation record.�Passing
functions as arguments is very useful in structuring of systems using upcalls

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
}

Passing Functions as Parameters – Implementation with Static Scope

- 83 -

You might also like