#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main() {
FILE *input = fopen("input.c", "r");
char ch, str[20], dig[20];
int l = 1, i = 0, k = 0;
char keyword[6][10] = {"int", "void", "if", "else", "do", "while"};
printf("Line No.\tToken\t\tLexeme\n");
while((ch = fgetc(input)) != EOF) {
if (ch == '\n') {
l++;
} else if (isspace(ch)) {
continue;
} else if (ispunct(ch)) {
if (ch == '/' && (ch = fgetc(input)) == '/') {
while ((ch = fgetc(input)) != '\n');
l++;
} else {
printf("%d\t\tOperator/Symbol\t%c\n", l, ch);
}
} else if (isdigit(ch)) {
dig[k++] = ch;
while (isdigit(ch = fgetc(input))) dig[k++] = ch;
dig[k] = '\0';
ungetc(ch, input);
printf("%d\t\tNumber\t\t%s\n", l, dig);
k = 0;
} else if (isalpha(ch)) {
str[i++] = ch;
while (isalnum(ch = fgetc(input))) str[i++] = ch;
str[i] = '\0';
ungetc(ch, input);
int isKeyword = 0;
for (int j = 0; j < 6; j++) {
if (strcmp(str, keyword[j]) == 0) {
printf("%d\t\tKeyword\t\t%s\n", l, str);
isKeyword = 1;
break;
}
}
if (!isKeyword) {
printf("%d\t\tIdentifier\t%s\n", l, str);
}
i = 0;
}
}
fclose(input);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8Y3R5cGUuaD4KI2luY2x1ZGU8c3RyaW5nLmg+CgppbnQgbWFpbigpIHsKICAgIEZJTEUgKmlucHV0ID0gZm9wZW4oImlucHV0LmMiLCAiciIpOwogICAgY2hhciBjaCwgc3RyWzIwXSwgZGlnWzIwXTsKICAgIGludCBsID0gMSwgaSA9IDAsIGsgPSAwOwogICAgY2hhciBrZXl3b3JkWzZdWzEwXSA9IHsiaW50IiwgInZvaWQiLCAiaWYiLCAiZWxzZSIsICJkbyIsICJ3aGlsZSJ9OwoKICAgIHByaW50ZigiTGluZSBOby5cdFRva2VuXHRcdExleGVtZVxuIik7CgogICAgd2hpbGUoKGNoID0gZmdldGMoaW5wdXQpKSAhPSBFT0YpIHsKICAgICAgICBpZiAoY2ggPT0gJ1xuJykgewogICAgICAgICAgICBsKys7CiAgICAgICAgfSBlbHNlIGlmIChpc3NwYWNlKGNoKSkgewogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9IGVsc2UgaWYgKGlzcHVuY3QoY2gpKSB7CiAgICAgICAgICAgIGlmIChjaCA9PSAnLycgJiYgKGNoID0gZmdldGMoaW5wdXQpKSA9PSAnLycpIHsKICAgICAgICAgICAgICAgIHdoaWxlICgoY2ggPSBmZ2V0YyhpbnB1dCkpICE9ICdcbicpOwogICAgICAgICAgICAgICAgbCsrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcHJpbnRmKCIlZFx0XHRPcGVyYXRvci9TeW1ib2xcdCVjXG4iLCBsLCBjaCk7CiAgICAgICAgICAgIH0KICAgICAgICB9IGVsc2UgaWYgKGlzZGlnaXQoY2gpKSB7CiAgICAgICAgICAgIGRpZ1trKytdID0gY2g7CiAgICAgICAgICAgIHdoaWxlIChpc2RpZ2l0KGNoID0gZmdldGMoaW5wdXQpKSkgZGlnW2srK10gPSBjaDsKICAgICAgICAgICAgZGlnW2tdID0gJ1wwJzsKICAgICAgICAgICAgdW5nZXRjKGNoLCBpbnB1dCk7CiAgICAgICAgICAgIHByaW50ZigiJWRcdFx0TnVtYmVyXHRcdCVzXG4iLCBsLCBkaWcpOwogICAgICAgICAgICBrID0gMDsKICAgICAgICB9IGVsc2UgaWYgKGlzYWxwaGEoY2gpKSB7CiAgICAgICAgICAgIHN0cltpKytdID0gY2g7CiAgICAgICAgICAgIHdoaWxlIChpc2FsbnVtKGNoID0gZmdldGMoaW5wdXQpKSkgc3RyW2krK10gPSBjaDsKICAgICAgICAgICAgc3RyW2ldID0gJ1wwJzsKICAgICAgICAgICAgdW5nZXRjKGNoLCBpbnB1dCk7CgogICAgICAgICAgICBpbnQgaXNLZXl3b3JkID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCA2OyBqKyspIHsKICAgICAgICAgICAgICAgIGlmIChzdHJjbXAoc3RyLCBrZXl3b3JkW2pdKSA9PSAwKSB7CiAgICAgICAgICAgICAgICAgICAgcHJpbnRmKCIlZFx0XHRLZXl3b3JkXHRcdCVzXG4iLCBsLCBzdHIpOwogICAgICAgICAgICAgICAgICAgIGlzS2V5d29yZCA9IDE7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKCFpc0tleXdvcmQpIHsKICAgICAgICAgICAgICAgIHByaW50ZigiJWRcdFx0SWRlbnRpZmllclx0JXNcbiIsIGwsIHN0cik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaSA9IDA7CiAgICAgICAgfQogICAgfQoKICAgIGZjbG9zZShpbnB1dCk7CiAgICByZXR1cm4gMDsKfQo=