Checks in semantic analysis include:
- Type checking :
- Type mismatch checking. E.g,
int x = “Hello”;
- Type cast checking. E.g,
int radius = 3;
float Pi = 3.14;
float diameter = 2*radius*Pi;
/*radius will be typecast to float
during the calculation, i.e 3.00
(This result is reflected in the next step,
intermediate code generation
as TAC)
*/
the TAC code generated from the above example:
t1 = 2.0 // Represents the float constant 2.0
t2 = (float)radius // Type casting 'radius' to float
t3 = t1 * t2 // Multiplying 2.0 by the type-casted 'radius'
t4 = t3 * Pi // Multiplying the result by 'Pi'
- Variable Declaration checks :
- Ensuring all variables are declared. Eg,
int x = 10;
int y = 10;
sum = x+y; //sum was not declared
- Reserved identifier / keyword use. E.g,
int char = '1';//char is a reserved identifier
int char_2;
int if; //if is a reserved keyword
int if_2;
- Variable scope check
A unique variable declared within a function cannot be used by any other structures or functions outside of the original function block. Eg,
void demoFunction()
{
int uniqueX = 10;
}
int main()
{
printf("%d\n", uniqueX); // Error: 'uniqueX' is out of scope
return 0;
}
- Function Declaration and Invocation checks:
- Function declaration check – missed function declaration. e.g
int main() {
// Calling a function before it is declared or defined
hello(); // Error: 'hello' is called before declaration/definition
return 0;
}
/*the declaration was omitted,
this function declaration
is required:
void hello();
*/
void hello() {
// Function definition for 'hello'
printf("Hello, world!\n");
}
- Function declaration check – missing function implementation when a function prototype (function declaration) was declared. E.g,
// Function prototype (declaration)
int multiply(int a, int b);
int main() {
int result = multiply(3, 7); /* Error: Function
'multiply'
is declared but not
defined/implemented*/
return 0;
}
- Function invocation checks.
(NB, function invocation simply means to call a function correctly)
int add(int a, int b);
int add(int a, int b) {
return a + b;
}
int main() {
int sum1 = add(5, 10, 15); /* Error: Too many arguments
in the function call*/
int sum2 = add(5);/*Error: Missing arguments
in the function call*/
int sum3 = add(1,2) /*proper invocation of add,
no error here*/
return 0;
}
- Control Flow Checks -Verify the correct structure and use of control structures: if, while, for etc
Other checks exist , we have only examined some of the main semantic checks.
Recall that once all semantic checks are passed, Intermediate code generation occurs next.
Examples of Semantic errors in C
| Example | Description/Explanation |
| int a = “hello”; | Type mismatch: assigning a string to an integer variable. |
| int a; return a; | Using uninitialized variable a. |
| int a = 5 / 0; | Division by zero. |
| char *str = malloc(10); free(str); str[0] = ‘A’; | Using a pointer str after it has been freed. |
| int* ptr; *ptr = 10; | Dereferencing an uninitialized pointer ptr. |
| int arr[5]; arr[10] = 50; | Array index out of bounds. |
| double result = sqrt(-1); | Passing invalid argument to a function (e.g., square root of a negative number). |
| FILE *fp = fopen(“nonexistent.txt”, “r”); if(fp) { /* … */ } | Incorrect error checking on fopen. Should be if(!fp) for error checking. |
© 2024 Vedesh Kungebeharry. All rights reserved.