0% found this document useful (0 votes)
825 views4 pages

Type Checking

The document discusses type checking, which ensures operations are performed on compatible data types to prevent errors and improve code reliability. It outlines the differences between static and dynamic type checking, and their roles in the compilation process, including semantic analysis and code optimization. A C program is provided to demonstrate type checking implementation, including error handling for type mismatches.

Uploaded by

Dinesh0109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
825 views4 pages

Type Checking

The document discusses type checking, which ensures operations are performed on compatible data types to prevent errors and improve code reliability. It outlines the differences between static and dynamic type checking, and their roles in the compilation process, including semantic analysis and code optimization. A C program is provided to demonstrate type checking implementation, including error handling for type mismatches.

Uploaded by

Dinesh0109
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EX NO : 6 NAME : DINESH RAM A

DATE : 14/03/2025 REG NO : 2212046


TYPE CHECKING

DESCRIPTION

• Type checking ensures that operations are performed on compatible data types,
preventing type errors.
• It is a part of semantic analysis in the compilation process.
• Helps detect type mismatches in expressions and assignments before execution.
• Improves code reliability by catching errors at compile-time or runtime.
• Prevents unintended operations between incompatible data types, ensuring type
safety.
• Reduces the chances of undefined behaviour and runtime crashes.
• Can be classified into Static Type Checking (done at compile-time) and
Dynamic Type Checking (done at runtime).
• Static Type Checking is used in languages like C, Java, and C++, where type
errors are caught during compilation.
• Dynamic Type Checking is used in languages like Python and JavaScript,
where types are checked at runtime.
• Compiler lexical analysis recognizes tokens but does not perform type
checking.
• Syntax analysis ensures correct syntax but does not validate data types.
• Semantic analysis performs type checking to ensure correct operations on
variables.
• Helps in intermediate code generation by determining appropriate temporary
variables.
• Used in code optimization to enhance program performance by ensuring type
consistency.
• During target code generation, type checking ensures correct instruction
generation for execution.
• Prevents common programming errors such as assigning a float value to an
integer variable without explicit conversion.
• Example of type mismatch error:

int x = 10;
float y = 5.5;
x = x + y;

• Ensures that operations like arithmetic, relational, and logical operations follow
correct type rules.
• Type checking is crucial for memory management, avoiding issues like
accessing incompatible memory locations.
PROGRAM :

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_SYMBOLS 100

typedef struct {
char name[20];
char type[10];
} Symbol;

Symbol symbolTable[MAX_SYMBOLS];
int symbolCount = 0;

void addSymbol(char *name, char *type) {


for (int i = 0; i < symbolCount; i++) {
if (strcmp(symbolTable[i].name, name) == 0) {
printf("\n\tError: Label '%s' already defined.\n", name);
return;
}
}
strcpy(symbolTable[symbolCount].name, name);
strcpy(symbolTable[symbolCount].type, type);
symbolCount++;
}

char* getType(char *name) {


for (int i = 0; i < symbolCount; i++) {
if (strcmp(symbolTable[i].name, name) == 0) {
return symbolTable[i].type;
}
}
return NULL;
}

void checkType(char *res, char *op1, char *op2, char operator) {


char *type1 = getType(op1);
char *type2 = getType(op2);
char *typeRes = getType(res);

printf("\nSEMANTIC ANALYZER (TYPE CHECKING):\n");

if (typeRes == NULL) {
printf("\n\tError: Undefined variable '%s'.\n", res);
return;
}
if (type1 == NULL) {
printf("\n\tError: Undefined variable '%s'.\n", op1);
return;
}
if (type2 == NULL) {
printf("\n\tError: Undefined variable '%s'.\n", op2);
return;
}

if (strcmp(type1, type2) != 0) {
printf("\n\tError: Type mismatch in expression '%s = %s %c %s'.\n", res, op1, operator,
op2);
return;
}

printf("\n\tThere is no type mismatch in the expression '%s = %s %c %s'.\n", res, op1,


operator, op2);
}

int main() {
char varName[20], type[10];
char res[20], op1[20], op2[20], operator;
int n;

printf("\n\tIMPLEMENTATION OF TYPE CHECKING\n");

printf("\nEnter the number of variables: ");


scanf("%d", &n);
getchar();

for (int i = 0; i < n; i++) {


printf("Enter variable name and type (e.g., x int): ");
scanf("%s %s", varName, type);
addSymbol(varName, type);
}

printf("\nEnter expression in the form 'res = var1 operator var2' (e.g., a = b + c):\n");
scanf("%s = %s %c %s", res, op1, &operator, op2);

checkType(res, op1, op2, operator);

printf("\nPRESS ENTER TO EXIT FROM TYPE CHECKING\n");


getchar();
getchar();
return 0;
}
OUTPUT:

You might also like