Semantic analysis is one of the most important compiler phases. It checks whether the program is meaningful (logically correct) according to language rules, not just syntactically correct.
A Semantic Analyzer is a phase of the compiler that checks the meaning and consistency of the program after syntax analysis.
👉 It ensures that the program follows language rules related to meaning, such as:
Ensures that operations are performed on compatible data types.
int a = 5;
float b = a + 2.5;
✔ Valid because int can be converted to float
❌ Invalid:
int a = "hello";
Ensures variables are declared before use.
x = 10; // ❌ error (x not declared)
Checks whether a variable/function is accessible in a given scope.
{
int x = 10;
}
printf("%d", x); // ❌ x out of scope
Ensures:
int sum(int a, int b);
sum(5); // ❌ missing argument
int a[5];
a[10] = 20; // ⚠ semantic error (out of range conceptually)
Checks correctness of goto, break, continue.
Handles implicit conversions.
float x = 10; // int → float (automatic conversion)
Maintains information about:
👉 Very important for semantic analysis.
Syntax Tree
↓
Semantic Analyzer
↓
Annotated Syntax Tree
↓
Intermediate Code
Semantic errors occur when syntax is correct but meaning is wrong.
Occurs when incompatible types are used.
int x = "hello";
Variable used without declaration.
sum = a + b; // a, b not declared
Same variable declared more than once in same scope.
int a;
int a; // ❌ error
int add(int a, int b);
add(10); // ❌ error
Using variable outside its scope.
{
int x = 5;
}
x = 10; // ❌ error
Using invalid operations on types.
int x = 10;
int y = x + "abc"; // ❌ error
int a[5];
a[100] = 10;
Function returns wrong type.
int f() {
return "hello"; // ❌ error
}
✔ Semantic analysis checks meaning, not grammar ✔ Uses syntax tree + symbol table ✔ Performs type checking and scope checking ✔ Detects errors like:
| Task | Description | Example |
|---|---|---|
| Type checking | Ensures compatible types | int + float OK |
| Declaration check | Variable must be declared | x not declared |
| Scope checking | Valid access region | out of block error |
| Function checking | Correct arguments/return | missing arguments |
| Symbol table | Stores identifiers | variables/functions |
| Array check | Logical index validation | a[100] in a[5] |
| Error Type | Meaning | Example |
|---|---|---|
| Type mismatch | Wrong data types | int = "abc" |
| Undeclared variable | Not defined | x used without declaration |
| Multiple declaration | Same variable twice | int a; int a; |
| Function error | Wrong call | wrong arguments |
| Scope error | Outside block | variable out of scope |
| Operator error | Invalid operation | int + string |
| Return error | Wrong return type | int returns string |
The Semantic Analyzer is a compiler phase that checks the meaningfulness of the source program by verifying type correctness, variable declarations, scope rules, and function usage. It detects semantic errors such as type mismatch, undeclared variables, and scope violations using the symbol table and annotated syntax tree.
Open this section to load past papers