(Compiler Construction — exam-ready, simple explanation with examples, diagrams, and key points)
Semantic Analysis is the phase of the compiler that checks whether the program has a meaningful (logically correct) structure according to the language rules.
👉 It is done after syntax analysis and ensures:
“The program is syntactically correct AND makes sense.”
Source Code → Lexical → Syntax → Semantic → Intermediate Code
It checks:
✔ Type checking ✔ Variable declaration ✔ Function correctness ✔ Scope rules ✔ Expression validity
Ensures correct data types are used.
int x = 10;
float y = x + 2.5;
✔ Valid (implicit conversion)
int x = "hello";
👉 Error: type mismatch
Ensures variables are used within valid scope.
{
int x = 10;
}
print(x); // ❌ Error
Checks:
int add(int a, int b);
add(5); // ❌ Error
Every variable must be declared before use.
x = 10; // ❌ Error (not declared)
A symbol table stores information about identifiers.
| Name | Type | Scope |
|---|---|---|
| x | int | global |
| y | float | local |
| Error Type | Example |
|---|---|
| Type mismatch | int x = "abc" |
| Undeclared variable | x = 10 |
| Wrong function call | add(5) |
| Scope error | using local variable outside block |
| Feature | Syntax Analysis | Semantic Analysis |
|---|---|---|
| Checks | Grammar rules | Meaning rules |
| Errors | Syntax errors | Logical errors |
| Example | missing ; | type mismatch |
| Stage | Before semantic | After syntax |
Intermediate Code Generation converts source code into an intermediate representation (IR) that is:
✔ Bridge between source code and machine code ✔ Helps optimization ✔ Makes compiler portable
Each instruction has at most 3 operands.
a = b + c * d
t1 = c * d
t2 = b + t1
a = t2
Format:
(op, arg1, arg2, result)
| op | arg1 | arg2 | result |
|---|---|---|---|
| * | c | d | t1 |
| + | b | t1 | t2 |
| = | t2 | a |
Format:
(index) op arg1 arg2
| i | op | arg1 | arg2 |
|---|---|---|---|
| 0 | * | c | d |
| 1 | + | b | (0) |
| 2 | = | (1) | a |
x = a + b * c
t1 = b * c
t2 = a + t1
x = t2
| Feature | Syntax Tree | Intermediate Code |
|---|---|---|
| Form | Tree | Linear code |
| Use | Structure | Execution steps |
| Level | High | Medium |
| Machine dependency | No | No |
Syntax Tree → Semantic Analysis → Annotated Tree → Intermediate Code
👉 Frequently asked:
| Aspect | Semantic Analysis | Intermediate Code |
|---|---|---|
| Purpose | Check meaning | Generate IR code |
| Input | Syntax tree | Annotated tree |
| Output | Annotated tree | Intermediate code |
| Focus | Correctness | Translation |
| Tool | Symbol table | TAC / Quadruples |
| Errors | Semantic errors | None (after generation) |
Open this section to load past papers