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

Symbol Table Run Time Environment

The symbol table is a data structure used by compilers to track the names, types, and attributes of identifiers in a source program. It contains entries with the name and associated information. Symbol tables are built during lexical and syntax analysis and used throughout compilation. They allow efficient name lookup and disambiguation of scopes. Storage for variables can be allocated statically at compile-time, via stacks for local variables, or dynamically from the heap.

Uploaded by

Ravi Teja
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
308 views

Symbol Table Run Time Environment

The symbol table is a data structure used by compilers to track the names, types, and attributes of identifiers in a source program. It contains entries with the name and associated information. Symbol tables are built during lexical and syntax analysis and used throughout compilation. They allow efficient name lookup and disambiguation of scopes. Storage for variables can be allocated statically at compile-time, via stacks for local variables, or dynamically from the heap.

Uploaded by

Ravi Teja
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Symbol Table

&
Run Time Environment
Symbol Table
• Symbol table is a data structure which is used
by compiler to keep track of scope and binding
information about names i.e, a complier
needs to collect and use information about
the names appearing in the source program.

• The Symbol table is searched every time a


name is encountered in the source text.
Changes will be made to the table if a new
name is found.
• The information includes the string of
characters by which it is denoted, its type, its
form, its location in memory and other
attributes depending on the language.

• Each entry in the symbol table is a pair of the


form (name, information).

• Normally symbol table is built in lexical and


syntax analysis phases.
The symbol table is used by various
phases as follows
• Lexical analyzer stores the information of the
symbols in symbol table.
• Parser while checking the syntax of the source,
makes use of symbol table to verify the
information about the tokens being generated.
• Semantic analysis phase refers symbol table for
type conflict issue.
• Code generation refers symbol table knowing
how much run time space is allocated? What type
of run time space is allocated?
Symbol Table Format
• Entries in Symbol Table
Names Attributes

• To achieve compile time efficiency compiler


makes use of symbol table.
• It associates lexical names with their attribute.
The following items to be stored in symbol Table
• Variable Names
• Comments
• Procedure Names
• Function Names
• Literal constants and strings
• Compiler generated temporaries
• Labels in source languages
Compiler uses following types of information from
symbol table

• Data Type
• Name
• Declaring Procedures
• Offset in Storage
• If structure or record then pointer to structure table.
• For parameters, whether passing is by value or
reference?
• Numbers and type of arguments passed to the function
• Base Address.
Name Representations in Symbol Table

There are two types of name representations

• Fixed length Name


• Variable length Name
Name Representations in Symbol Table
Fixed length Name: A fixed space for each name
in allocated in symbol table. In this type of
storage if name is too small then there is
wastage of space.

Variable length Name: The amount of space


required by string is used to store the names.
The name can be stored with the help of
starting index and length of each name.
Need for Symbol Table
• For quick insertion of identifier and related
information.

• For quick searching of identifier.


Organization of
Block Structures Languages
• The block structured languages is a kind of
language in which section of source code is
within some matching pair of delimiters such
as “ {“ and “}” or begin and end.

• Normally block structured languages support


structured programming approach.
Following are data structures that are used for
organization of block structured languages

• Arrays (Liner List)


• List (Self organizing list)
• Hashing
• Tree structure representation of scope
information.
Block Structure and Non Block Structure
Storage Allocation
The Storage allocation can be done for two
types of data variables they are

• Local Data: The local data can be handled


using activation record.

• Non local Data: The non local data can be


handled using scope information.
Block Structure and Non Block Structure
Storage Allocation
• The block structured storage allocation can be
done using static scope or lexical scope.

• The non block structured storage allocation


can be done using dynamic scope.
Activation Records
• The activation record is a block of memory
used for managing information needed by a
single execution of procedure.
Various Fields of Activation Records
Actual parameters
Return Values
Control Link
(Dynamic Link)
Access Link
(Static Link)
Saved Machine Status
Local Variables
Temporaries
local data can be handled using activation
record. The offset relative to base pointer of
an activation record points to local data
variables within an activation record.

Reference to any variable x in procedure = Base


pointer pointing to start of procedure + Offset
of variable x from base pointer.
Example:
procedure A
Int a;
Procedure B
Int b;
Body of B;
Body of A;
Access to Non Local Names
Non local names there are two types of scope rules
that can be defined as, Static and dynamic.

Access to Non Local Data


Static Scope Rule:
• The static scope rule is called as lexical scope.
In this type of the scope is determined by
examining the program text.
• Pascal , C and ADA are the langauges that use
the static scope rule.
• These languages are also called as block
structured languages.
Dynamic Scope Rule:
• For non block structured languages this
dynamic scope allocation rules are used.
• The dynamic scope rule determines the scope
of declaration of the names at run time by
considering the current activation.
• LISP and SNOBOL are the languages which use
dynamic scope rule.
Static scope or Lexical Scope
The scope of declaration in a block structured
languages is given by most closely nested loop or
static rule. It is given below.
• The declaration are visible at a program point are:
• The declarations that are made locally in the
procedure.
• The names of all enclosing procedures.
• The declarations of names made immediately within
such procedures.
The storage can be allocated for a complete procedure
body at one time
Lexical Scope for Nested Procedure
Nesting Depth: Nesting depth of a procedure is
used to implement lexical scope. The nesting
depth can be calculated as follows,
• The nesting depth of main program is 1.
• Add 1 to depth each time when a new
procedure begins.
• Subtract 1 from depth each time when your
exit from a nested procedure.
• The variable declared in specific procedure is
associated with nesting depth.
Example:
Procedure main
Procedure p1
Procedure p2
Procedure p3
Procedure p4

Representing the Depths of Nested procedures


Access Link
The implementation of lexical scope can be
obtained by using pointer to each activation
record. These pointers are called access links.
If procedure p is nested within a procedure q
then access link of p points to access link of
most recent activation record of procedure q.
Displays
It is expensive to traverse down access link every time
when a particular non local variable is accessed.
Speed up the access to non locals can be achieved by
maintaining an array of pointers called display.
In display
• An array of pointers to activation record is
maintained.
• Array is indexed by nesting level.
• The pointers point to only accessible record.
• The display changes when a new activation occurs
and it must be reset when control returns from the
new activation.
Access Link Vs Display
• Access link take more time to access non local
variable especially when non local variables
are at many nested levels away.
• Display is used to generate code efficiently.
• Display required more space at the run time
than access links.
Dynamic Scoping
(storage Allocation for Non Block Structured languages)

• In dynamic scoping a use of non local variable


refers to the non local data declared in most
recently called and still active procedure.
• Therefore each time new bindings are set up
for local names called procedure. In dynamic
scoping symbol tables can be required at run
time.
There are two ways to implement under
dynamic scoping
Deep Access :
The idea is keep a stack of activate variables, use
control links instead of access links and when
you want to find a variable then search the
stack from top to bottom looking for most
recent activation record that contains the
space for desired variables.
This method of accessing non local variable is
called deep access.
Shallow access:
The idea is to keep a central storage with one
slot for every variable name. if the names are
not created at run time then that storage
layout can be fixed at compile them other
wise when new activation of procedure
occurs, then that procedure changes the
storage entries for tis locals at entry and exit.
Comparison of Deep and Shallow Access
•Deep access takes longer time to access the
non locals while shallow access fast access.

•Shallow access has a overhead of handling


procedure entry and exit.

•Deep access needs a symbol table at run time


Variable Length Data:
For a procedure call sequence can be implemented
by caller and callee procedures. The caller
procedure is a procedure which calls another
procedure whereas the callee is a procedure
which gets called by another procedure.
Parameter Passing: There are two types of
parameters they are,
• Formal Parameters
• Actual Parameters
And based on these parameters there are
various parameters passing methods, the
most common methods are,
• Call by value
• Call by Reference
• Copy restore
• Call by Name
Storage Allocation Strategies
Different storage allocation strategies are,
• Static allocation lays out storage for all data
objects at compile time.
• Stack allocation manages the run-time
storage as a stack.
• Heap allocation allocates and deallocates
storage as needed at run time from a data
area known as a heap.
Static Allocation
In static allocation, names are bound to storage as the
program is compiled. Since the bindings do not change
at run time, its names are bound to the same storage,
whenever a procedure is activated. Thus values of locals
names are retained across activations of a procedure.
The limitations of the static allocation are:
• The size of a data object in memory must be known at compile
time.
• Recursive procedure are not allowed, because all activations of a
procedure us the same bindings for local names.
• Data structures cannot be created dynamically, since there is no
mechanism for storage allocation at run time.
Stack Allocation
In stack allocation storage is organized as a stack and
activation records are pushed and popped as
activation begin and end respectively. Storage for the
locals in each call of a procedure is contained in the
activation record for that call. Thus, locals are bound
to fresh storage in each activation, because a new
activation record is pushed onto the stack when a
call is made. The values of locals are deleted when
the activation ends i.e., the values are lost because
the storage for locals disappears when the activation
record is popped.
Heap Allocation
The stack allocation strategy is not possible if,
• The values of local names must be retained when
an activation ends.
• A called activation outlives the caller.
In the above cases the deallocation of activation
records will not occur in a last in first out fashion.
So storage cannot be organized as a stack.
Heap gives out pieces of storage, as needed for
activation records or other objects. These pieces
can be deallocated at any time and the free
space is used by other objects.

You might also like