Type checking and environments are core concepts of Semantic Analysis in a compiler. They ensure that a program is logically correct and consistent in data usage.
Type checking is the process of verifying that operations in a program are performed on compatible data types according to language rules.
👉 It ensures that expressions, assignments, and function calls use correct types.
int a = 5;
int b = 10;
int c = a + b;
✔ Valid because all operands are integers
int x = "hello"; // ❌ type mismatch
👉 String cannot be assigned to integer
Type checking performed at compile time.
Type checking performed at runtime.
| Feature | Static | Dynamic |
|---|---|---|
| Time | Compile time | Runtime |
| Languages | C, C++ | Python |
| Speed | Fast | Slower |
| Error detection | Early | During execution |
Left-hand side must match right-hand side type.
int x = 10.5; // ❌ mismatch
Operands must be compatible.
int a = 10 + "abc"; // ❌ invalid
Arguments must match parameter types.
int add(int a, int b);
add(10.5, 20); // ❌ mismatch
Return value must match function type.
int f() {
return "hello"; // ❌ error
}
A type expression describes the structure of a type in a program.
int a[10];
Type expression:
array(10, int)
int *p;
Type expression:
pointer(int)
int f(int, float);
Type expression:
(int, float) → int
Automatic or manual conversion of one type into another.
float x = 10; // int → float
int x = (int) 10.5;
An environment is a mapping between names (variables/functions) and their meaning (types, memory location, values) at a particular point in a program.
👉 In simple words:
Environment = information about variables at a given time
C, C++
Python, JavaScript
| Feature | Static Environment | Dynamic Environment |
|---|---|---|
| Time | Compile time | Runtime |
| Binding | Fixed | Changes dynamically |
| Speed | Faster | Slower |
| Example | C, C++ | Python |
A symbol table is a data structure used to implement the environment.
It stores:
| Name | Type | Scope | Address |
|---|---|---|---|
| x | int | global | 1000 |
| y | float | local | 2000 |
✔ Environment stores information about variables ✔ Type checking uses environment to verify correctness
👉 Example: To check:
x = y + z;
Compiler checks:
✔ Type checking ensures correct use of data types ✔ Two types: Static and Dynamic ✔ Uses type rules (assignment, function, operators) ✔ Environments store variable meaning and scope ✔ Symbol table is implementation of environment ✔ Type expressions define structure of types
| Concept | Description |
|---|---|
| Static type checking | Compile-time checking |
| Dynamic type checking | Runtime checking |
| Type error | Mismatch of types |
| Coercion | Type conversion |
| Type expression | Structure of type |
| Concept | Meaning |
|---|---|
| Environment | Mapping of names to meanings |
| Static environment | Compile-time mapping |
| Dynamic environment | Runtime mapping |
| Symbol table | Implementation of environment |
Type checking is the process of verifying that all operations in a program use compatible data types, while an environment is a mapping that stores information about identifiers such as their types, scope, and memory locations. The environment is used by the compiler during type checking to ensure semantic correctness of the program.
Open this section to load past papers