Compiler Design Questions RGPV
Compiler Design Questions RGPV
Page 2 of 24
Page 3 of 24
C-style struct equivalence can differ based on how they are declared.
function
(int)3.14 → 3
Page 5 of 24
cpp
cpp
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
python
def type_check(node):
if node is a constant:
return type of constant
if node is an identifier:
return symbol_table[node.name]
if node is binary_op:
left_type = type_check(node.left)
right_type = type_check(node.right)
if left_type == int and right_type == int:
return int
else:
raise TypeError("Invalid operands")
int char*
Page 9 of 24
plaintext
Method Description
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
; }
malloc()
new
ptr =
malloc(size)
Page 16 of 24
free(ptr)
Method Description
Problem Description
malloc()
plaintext
@ #
123abc
"Hello
int a = 10;
char b = a + "text"; // Type mismatch: cannot add int and string
Page 20 of 24
; } end
int a = 10
int b = 20;
;
Page 22 of 24
if (x > y printf("Hello");
) y
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