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

Module 3 (Compiler Design) 1

Uploaded by

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

Module 3 (Compiler Design) 1

Uploaded by

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

Compiler Design

Module-3
By
Saifuddin Ahmed
Department of CSE, BVEC
Symbol Table
 symbol table is abstract data structure used by compilers to
store the complete information about an identifiers (symbols)
encountered in the source code.
 These identifiers can include variables, functions, types,
labels, and other language constructs.
 The symbol table allows the compiler to manage and analyze
these symbols during various phases of compilation, such as
lexical analysis, syntax analysis, semantic analysis, and code
generation.
Structure of a Symbol Table:
• Entries: Each entry in the symbol table corresponds to a unique
identifier in the source code.
• Attributes: Each symbol entry contains attributes associated with
the identifier. These attributes provide crucial information about
the symbol. The following information of identifiers is stored in symbol
table such as,
i. Name
ii. Value
iii. Size
iv. Address
v. Scope
vi. Life span
vii. Other information in case of complex data type.
Note: Each and every phases of the compiler interact with symbol table
Structure of a Symbol Table (cont…)
• Organization: Symbol tables are typically organized based on
scopes. Scopes can include global scope, local scope within
functions or blocks, and nested scopes. The symbol table may
have separate entries for each scope, and scopes may be
hierarchical, with nested scopes inheriting properties from
their enclosing scopes.
• Data Structure: Symbol tables can be implemented using
various data structures, such as hash tables, binary search
trees, linked lists, or a combination of these, depending on
the compiler's requirements and the language being
compiled.
Symbol Attributes:
• Name: The name of the identifier.
• Type: The data type of the identifier (e.g., integer, float, pointer, struct,
function).
• Scope: The scope in which the identifier is defined (e.g., global scope,
local scope).
• Memory Location: The memory address or storage location of the
identifier.
• Size: The size of the identifier in memory (e.g., number of bytes for
variables, number of parameters for functions).
• Visibility: Whether the identifier is visible within a specific scope or
accessible from other scopes.
• Initialization Status: Whether the identifier has been initialized or
assigned a value.
• Other Properties: Additional properties specific to the language being
compiled, such as access modifiers, static/dynamic linkage, constant
status, etc.
Management of Symbol Tables
• Creation: The symbol table is typically created at the beginning of
compilation and populated as the compiler processes the source
code.
• Insertion: Symbols are inserted into the symbol table as they are
encountered in the source code. Each symbol's attributes are
recorded in the corresponding symbol table entry.
• Lookup: The symbol table supports efficient lookup operations to
retrieve information about symbols based on their names and
scopes. This allows the compiler to resolve identifiers and perform
semantic analysis.
• Update: Symbols may need to be updated in the symbol table
during compilation, such as when their attributes change (e.g.,
reassignment of variables).
Run time Environment
 The runtime environment encompasses various memory
allocation strategies, including static allocation, stack
allocation, and heap allocation.
1. Static Allocation:
a) Static allocation refers to the allocation of memory for variables
at compile time.
b) Memory allocation is done at the compile time.
c) Memory is allocated for variables before the program starts
executing and remains fixed throughout the program's lifetime.
d) Binding do not change at run time
e) Recursion is not supported
f) Static allocation is efficient in terms of memory management,
but it lacks flexibility as the size of variables must be known at
compile time.
g) Dynamic data structure not supported.
2. Stack Allocation:
a) Stack allocation involves allocating memory for variables within
the function's stack frame.
b) Recursion supported.
c) Local variable belongs to new activation record.
d) Dynamic data structure not supported.
3. Heap Allocation:
a) Allocation and de-allocation can be done at any time based on
user requirement.
b) Heap allocation allows for flexible memory management, as
memory can be allocated and deallocated at runtime using
functions like malloc() and free() in languages like C and C++.
c) Recursion supported
d) Dynamic data structure supported.
Run time environment in compiler design
 the runtime environment refers to the support system
required to execute the compiled code produced by the
compiler. It includes various components and services needed
for the execution of programs, such as:
– Memory Management: Runtime environment manages memory
allocation and de-allocation during program execution. It includes
mechanisms for allocating memory for variables, data structures, and
managing memory usage efficiently.
– Execution Environment: This encompasses the environment in which
the compiled code runs, including the operating system, hardware
platform, and any other system software necessary for program
execution.
– Input/output Operations: Runtime environment provides facilities for
input and output operations, such as reading from files, writing to
files, interacting with the user through standard input/output
streams, etc.
– Exception Handling: It is used for handling runtime errors/exceptions
that occur during program execution. This may involve providing
support for try-catch blocks or other error-handling mechanisms.
– Runtime Libraries: It may include libraries that provide various
functionalities to the compiled code, such as mathematical functions,
string manipulation functions, data structures, etc.
– Dynamic Linking/Loading: In cases where the compiler produces code
that depends on external libraries or modules, the runtime
environment may include mechanisms for dynamically linking or
loading these libraries during program execution.
– Concurrency and Multithreading Support: If the language or the compiled code
supports concurrency and multithreading, the runtime environment may provide
facilities for creating and managing threads, synchronization primitives, and other
concurrency-related features.
– Garbage Collection (in some languages): For languages with automatic memory
management, such as Java or C#, the runtime environment may include a garbage
collector responsible for reclaiming memory occupied by objects that are no longer
needed.
Activation record
 Activation records play a fundamental role in managing function
calls and the execution of programs
 Activation record, also known as a stack frame or activation frame,
is a data structure used to manage the execution of a function or
procedure during program execution.
Activation record
 It represents the state of a function call, it includes a total seven
parameters such as,
1) Local variables: Holds

You might also like