Compiler Design (MCA554) – Unit 4: Type Checking and Run Time Environments

 Compiler Design (MCA554) – Unit 4: Type Checking and Run Time Environments
Based on MCA Semester III Syllabus 

---
Introduction to Type Checking
Type Checking is the process of verifying that operands and expressions are used with compatible data types.
Its purpose is to detect type errors before program execution.
Example:
int x;
x = 10;
Valid Assignment.
Example:
int x;
x = "Hello";
Type Error.

---
Need for Type Checking
Type checking helps:
Detect programming errors
Improve reliability
Ensure correct operations
Prevent invalid assignments


---
Type Expressions
A Type Expression represents the type of a variable, function, or expression.
Examples:
int a;
float b;
char c;
Type Expressions:
a → int
b → float
c → char

---
Primitive Data Types
Basic data types available in programming languages.
Examples:
int
float
char
double
boolean

---
Constructed Data Types
Created using primitive types.
Examples:
Array
Pointer
Structure
Function

---
Type Equivalence
Determines whether two type expressions represent the same type.

---
Name Equivalence
Two variables are equivalent if they have the same declared type name.
Example:
typedef int Marks;
Marks m1;
Marks m2;
Equivalent types.

---
Structural Equivalence
Two types are equivalent if their structures are identical.
Example:
struct A
{
   int x;
};
struct B
{
   int x;
};
Structurally equivalent.

---
Type Conversion
Converting one data type into another.

---
Implicit Type Conversion
Performed automatically by compiler.
Example:
int a = 10;
float b;
b = a;
Integer automatically becomes float.

---
Explicit Type Conversion
Performed by programmer.
Example:
float a = 10.5;
int b = (int)a;
Result:
b = 10

---
Static Type Checking
Type checking performed during compilation.
Examples:
int a;
a = 20;
Compiler verifies types before execution.
Advantages:
Faster execution
Early error detection


---
Dynamic Type Checking
Type checking performed during execution.
Examples:
x = 10
x = "Hello"
Type determined at runtime.
Advantages:
Flexibility

Disadvantages:
Slower execution


---
Comparison of Static and Dynamic Type Checking
Static Checking Dynamic Checking
Compile Time Run Time
Faster Slower
Early Detection Late Detection
C, C++ Python, JavaScript


---
Specification of a Simple Type Checker
A Type Checker:
1. Reads declarations

2. Stores type information

3. Verifies assignments

4. Reports errors


Example:
int x;
float y;
x = y;
Possible warning:
Possible Loss of Data

---
Run Time Environment
The Run Time Environment manages memory and resources during program execution.
Responsibilities:
Memory allocation
Function calls
Parameter passing
Stack management
Variable storage


---
Source Language Issues
Different programming languages have different requirements.
Examples:
Dynamic arrays
Recursion
Pointers
Garbage collection

Compiler must support these features.

---
Storage Organization
Memory is divided into different sections.
+----------------+
| Code Segment |
+----------------+
| Data Segment |
+----------------+
| Heap |
+----------------+
| Stack |
+----------------+

---
Code Segment
Stores:
Program Instructions
Example:
printf("Hello");
Compiled instructions are stored here.

---
Data Segment
Stores:
Global Variables
Static Variables
Example:
int total = 100;

---
Stack
Stores:
Function calls
Local variables
Return addresses

Example:
void display()
{
   int x = 10;
}
Variable x is stored on the stack.

---
Heap
Stores dynamically allocated memory.
Example:
int *p;
p = malloc(sizeof(int));
Memory allocated from heap.

---
Storage Allocation Strategies
Methods used to allocate memory.

---
Static Allocation
Memory assigned during compilation.
Example:
static int count;
Advantages:
Simple
Fast

Disadvantages:
Less flexible


---
Stack Allocation
Memory allocated during function calls.
Example:
void test()
{
   int x;
}
Advantages:
Fast allocation
Automatic deallocation


---
Heap Allocation
Memory allocated dynamically.
Example:
malloc()
new
Advantages:
Flexible

Disadvantages:
Slower
Memory leaks possible


---
Access to Non-Local Names
Occurs when a function accesses variables declared outside its scope.
Example:
int global = 10;
void display()
{
   printf("%d", global);
}
Compiler must locate the variable correctly.

---
Parameter Passing
Method of transferring values to functions.

---
Call By Value
A copy of the variable is passed.
Example:
void fun(int x)
{
   x = 100;
}
Original variable remains unchanged.

---
Call By Reference
Address of variable is passed.
Example:
void fun(int *x)
{
   *x = 100;
}
Original variable changes.

---
Symbol Table
One of the most important compiler data structures.
Stores information about identifiers.

---
Information Stored
Variable Name
Data Type
Scope
Memory Address
Example:
Identifier Type
age int
salary float


---
Functions of Symbol Table
Store identifiers
Type checking
Scope management
Memory allocation


---
Dynamic Storage Allocation
Memory allocated during execution.
Examples:
malloc()
calloc()
realloc()
free()
Benefits:
Efficient memory usage
Flexible allocation


---
Language Facilities for Dynamic Storage Allocation
Programming languages provide built-in support.
Examples:
C
malloc()
free()
C++
new
delete
Java
Objects created dynamically

---
Applications of Runtime Environment
Program execution
Memory management
Function calls
Dynamic allocation


---
Important Exam Questions
Short Questions
1. Define Type Checking.

2. What is Type Conversion?

3. Define Static Type Checking.

4. What is Dynamic Type Checking?

5. What is a Symbol Table?

6. Define Heap Memory.

7. What is Stack Allocation?

8. What is Call By Reference?



---
Long Questions
1. Explain Type Checking with examples.

2. Differentiate Static and Dynamic Type Checking.

3. Explain Storage Allocation Strategies.

4. Discuss Runtime Environment.

5. Explain Symbol Tables and their functions.

6. Explain Parameter Passing mechanisms.



---
Quick Revision Notes
Type Checking = Verification of data types.
Static Checking = Compile-time checking.
Dynamic Checking = Run-time checking.
Type Conversion = Data type transformation.
Stack = Local variables and function calls.
Heap = Dynamic memory allocation.
Symbol Table = Stores identifier information.
Call By Value = Copy passed.
Call By Reference = Address passed.
Runtime Environment = Manages execution resources.


---
Next Blog: Compiler Design (MCA554) – Unit 5: Code Generation, Register Allocation, Peephole Optimization, Basic Blocks, Flow Graphs, DAG Representation and Code Optimization.
x

Comments

Popular posts from this blog

Raster scan Vs Vector Scan

MCA SYLLABUS ALLAHABAD UNIVERSITY 2025

📘 PAPER 4 – DESIGN & ANALYSIS OF ALGORITHMS (UNIT 1 – INTRODUCTION & SORTING TECHNIQUES ) university of allahabad