Page 1 of 24
Page 2 of 24
Page 3 of 24
if E1 and E2 are int, then E1 + E2 is int.
Page 4 of 24
C-style struct equivalence can differ based on how they are declared.
int float char boolean array record pointer
function
int + float → float
(int)3.14 → 3
Page 5 of 24
cpp
int add(int a, int b); // integer addition
float add(float a, float b); // float addition
cpp
template <typename T>
T add(T a, T b) { return a + b; }
Page 6 of 24
Type Checking Description
Performed at compile time (e.g., C,
Java)
Performed at runtime (e.g., Python,
JavaScript)
plaintext
Source Code
↓
Syntax Analyzer (Parser)
↓
Abstract Syntax Tree (AST)
↓
Type Checker
↓
Type Correct? → Yes → Continue Compilation
→ No → Report Type Error
Page 7 of 24
E1 + E2
plaintext
If type(E1) = int and type(E2) = int ⇒ type(E1 + E2) = int
Else ⇒ Type Error
python
def type_check(node):
if node is a constant:
return type of constant
if node is an identifier:
return symbol_table[[Link]]
if node is binary_op:
left_type = type_check([Link])
right_type = type_check([Link])
if left_type == int and right_type == int:
return int
else:
raise TypeError("Invalid operands")
typedef struct { int x; } A;
typedef struct { int x; } B;
A a; B b;
a = b; // Name equivalence: error, Structural equivalence: okay
Page 8 of 24
int char*
Page 9 of 24
plaintext
High Memory Address
----------------------
| Command-line args |
| Environment vars |
----------------------
| Stack | ← grows downward
----------------------
| Heap | ← grows upward
----------------------
| Static/Data | ← global, static vars
----------------------
| Code/Text | ← machine instructions
----------------------
Low Memory Address
Page 10 of 24
malloc free new delete
Method Description
Pass copy of value
Pass address; changes reflect outside
Return value to caller after function
ends
Reevaluate expression each time used
plaintext
+----------------------+
| Return Address |
| Control Link (caller)|
| Parameters |
| Local Variables |
| Temporary Values |
+----------------------+
malloc()
Page 11 of 24
plaintext
+-------------+--------+--------+------------+
| Identifier | Type | Scope | Location |
+-------------+--------+--------+------------+
| sum | int |1 | 0x1000 |
| result | float | 1 | 0x1004 |
+-------------+--------+--------+------------+
Page 12 of 24
Page 13 of 24
if (a > b {
printf("Hello");
}
)
Page 14 of 24
; }
stmt → if ( expr error stmt // missing closing )
Feature Ad-Hoc Methods Systematic Methods
Basis Heuristics Formal theory
(grammar-based)
Implementation Simple, hard-coded Modular, structured
Language Specificity Tied to specific Generalizable
languages
Robustness Low (may miss or High (predictable
miscorrect) handling)
Use in Modern Rare Common (yacc,
Compilers bison, ANTLR)
Page 15 of 24
malloc()
new
ptr =
malloc(size)
Page 16 of 24
free(ptr)
Method Description
Maintain a list of free memory blocks.
Divide memory into blocks of size 2^k.
Easy to split and merge.
Allocate the first block that is large
enough.
Allocate the smallest block that fits.
Automatically reclaims unused memory
(e.g., Java, Python).
Problem Description
Free memory is scattered in small
blocks.
Memory not freed, leading to
depletion.
Pointers refer to deallocated memory.
Same memory block is freed more than
once.
int *ptr = (int *)malloc(sizeof(int) * 10); // Allocating array of 10 integers
if (ptr == NULL) {
printf("Memory allocation failed");
Page 17 of 24
}
free(ptr); // Freeing allocated memory
malloc()
plaintext
High Memory Address
-----------------------
| Stack (grows ↓) |
| Activation Records |
| Function Parameters |
-----------------------
↓ ↑ (gap)
-----------------------
| Heap (grows ↑) |
| Dynamic Objects |
| malloc/new memory |
-----------------------
Low Memory Address
Page 18 of 24
@ #
123abc
"Hello
int $value = 10; // '$' is not a valid character for identifiers
Page 19 of 24
if (a > b // Missing closing parenthesis and opening brace
printf("Greater");
int a = 10;
char b = a + "text"; // Type mismatch: cannot add int and string
Page 20 of 24
Error Type Detected In Example Description
Lexical Analysis int $x = 10; Invalid
tokens/character
s
Syntax Analysis if (x > 5 Grammar
violation
(structure)
Semantic x = "abc" + 10; Type mismatch
Analysis or undeclared
variable
Page 21 of 24
; } end
int a = 10
int b = 20;
;
Page 22 of 24
if (x > y printf("Hello");
) y
stmt → if ( expr error stmt
expr
pgsql
+----------------+
| Source Program |
+----------------+
|
v
+----------------+
| Lexical Phase |
+----------------+
| \
| -> [Lexical Error?] — Recover or report
v
Page 23 of 24
+----------------+
| Syntax Phase |
+----------------+
| \
| -> [Syntax Error?] — Use panic mode / phrase-level
v
+----------------+
| Semantic Phase |
+----------------+
| \
| -> [Semantic Error?] — Use type checking
v
Code Generation
Technique Description Pros Cons
Panic Mode Skip until Simple, fast May skip too
synchronizing token much input
Phrase-Level Make local changes Precise error Complex to
to fix recovery implement
Error Add grammar rules Good Increases
Productions to detect common diagnostics grammar size
errors
Global Find least changes to Theoretically Computational
Correction fix optimal ly expensive
Page 24 of 24