Practical Implementation of Scanner
Practical Implementation of Scanner
Static Table:
OP , ; = * + -
TABLE
Dynamic table:
ID TABLE a b c d
CO TABLE 100 90
OUTPUT:
Code:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h> // TurboC specific header for console I/O
char op[15] = {'+', '-', '*', '/', '=', ':', ';', '<', '>',','};
int main() {
FILE *file;
char str[100];
if (file == NULL) {
printf("Error opening the file.\n");
getch(); // Wait for a key press
return 1; // Return an error code
}
fclose(file);
getch(); // Wait for a key press before closing the console window
return 0;
}
if (flag == 0) {
for (j = 0; j < 10; j++) {
if (ptr[0] == op[j]) {
printf("OP#%d ", j + 1);
flag = 1;
break;
}
}
}
if (flag == 0) {
if (isalpha(ptr[0])) {
int isRepeated = 0;
for (i = 0; i < ic; i++) {
if (strcmp(ptr, identifiers[i]) == 0) {
printf("ID#%d ", i + 1);
isRepeated = 1;
break;
}
}
if (!isRepeated) {
strcpy(identifiers[ic++], ptr);
printf("ID#%d ", ic);
}
} else if (isdigit(ptr[0])) {
int isRepeated = 0;
for (i = 0; i < cc; i++) {
if (strcmp(ptr, constants[i]) == 0) {
printf("CO#%d ", i + 1);
isRepeated = 1;
break;
}
}
if (!isRepeated) {
strcpy(constants[cc++], ptr);
printf("CO#%d ", cc);
}
}
}
File—
int a , b ;
float c , d ;
a = b + c * 100 ;
d = a – 90 ;
c = 90 ;