#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char keywords[32][10] =
{
"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"
};
const char *operators[] = {"+", "-", "*", "/", "=", "==", "!=", ">", "<", ">=", "<=", "&&", "||", "++", "--"};
const char *separators[] = {",", ";", "(", ")", "{", "}", "[", "]"};
int isKeyword(char buffer[])
{
for(int i = 0; i < 32; ++i)
{
if(strcmp(keywords
[i
], buffer
) == 0) {
return 1;
}
}
return 0;
}
int isOperator(char ch)
{
for(int i = 0; i < sizeof(operators) / sizeof(operators[0]); ++i)
{
if(ch == operators[i][0])
{
{
return 2;
}
return 1;
}
}
return 0;
}
int isSeparator(char ch)
{
for(int i = 0; i < sizeof(separators) / sizeof(separators[0]); ++i)
{
if(ch == separators[i][0])
{
return 1;
}
}
return 0;
}
int main()
{
char ch, buffer[15];
FILE *fp;
int j = 0,token=0;
fp
= fopen("Firoz.txt", "r"); if(fp == NULL)
{
printf("Error opening the file\n"); }
while((ch
= fgetc(fp
)) != EOF
) {
{
buffer[j++] = ch;
}
else
{
if(j != 0)
{
buffer[j] = '\0';
j = 0;
token++;
if(isKeyword(buffer))
{
printf("%s is a keyword\n", buffer
); }
else
{
if(buffer[0]-'0'<= 48 || buffer[0]-'0' >= 57)
{
printf("%s is an not identifier\n", buffer
); }
else
{
printf("%s is an identifier\n", buffer
); }
}
}
if(isOperator(ch)==1)
{
token++;
printf("%c is an operator\n", ch
); if(isOperator(ch) == 2)
{
printf("Multi-character operator detected\n"); }
}
if(isSeparator(ch))
{
token++;
printf("%c is a separator\n", ch
); }
}
}
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiNpbmNsdWRlPHN0cmluZy5oPgojaW5jbHVkZTxjdHlwZS5oPgpjaGFyIGtleXdvcmRzWzMyXVsxMF0gPQp7CiAgICAiYXV0byIsImJyZWFrIiwiY2FzZSIsImNoYXIiLCJjb25zdCIsImNvbnRpbnVlIiwiZGVmYXVsdCIsCiAgICAiZG8iLCJkb3VibGUiLCJlbHNlIiwiZW51bSIsImV4dGVybiIsImZsb2F0IiwiZm9yIiwiZ290byIsCiAgICAiaWYiLCJpbnQiLCJsb25nIiwicmVnaXN0ZXIiLCJyZXR1cm4iLCJzaG9ydCIsInNpZ25lZCIsCiAgICAic2l6ZW9mIiwic3RhdGljIiwic3RydWN0Iiwic3dpdGNoIiwidHlwZWRlZiIsInVuaW9uIiwKICAgICJ1bnNpZ25lZCIsInZvaWQiLCJ2b2xhdGlsZSIsIndoaWxlIgp9OwoKY29uc3QgY2hhciAqb3BlcmF0b3JzW10gPSB7IisiLCAiLSIsICIqIiwgIi8iLCAiPSIsICI9PSIsICIhPSIsICI+IiwgIjwiLCAiPj0iLCAiPD0iLCAiJiYiLCAifHwiLCAiKysiLCAiLS0ifTsKY29uc3QgY2hhciAqc2VwYXJhdG9yc1tdID0geyIsIiwgIjsiLCAiKCIsICIpIiwgInsiLCAifSIsICJbIiwgIl0ifTsKCmludCBpc0tleXdvcmQoY2hhciBidWZmZXJbXSkKewogICAgZm9yKGludCBpID0gMDsgaSA8IDMyOyArK2kpCiAgICB7CiAgICAgICAgaWYoc3RyY21wKGtleXdvcmRzW2ldLCBidWZmZXIpID09IDApCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQoKaW50IGlzT3BlcmF0b3IoY2hhciBjaCkKewogICAgZm9yKGludCBpID0gMDsgaSA8IHNpemVvZihvcGVyYXRvcnMpIC8gc2l6ZW9mKG9wZXJhdG9yc1swXSk7ICsraSkKICAgIHsKICAgICAgICBpZihjaCA9PSBvcGVyYXRvcnNbaV1bMF0pCiAgICAgICAgewogICAgICAgICAgICBpZihzdHJsZW4ob3BlcmF0b3JzW2ldKSA+IDEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJldHVybiAyOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9CgppbnQgaXNTZXBhcmF0b3IoY2hhciBjaCkKewogICAgZm9yKGludCBpID0gMDsgaSA8IHNpemVvZihzZXBhcmF0b3JzKSAvIHNpemVvZihzZXBhcmF0b3JzWzBdKTsgKytpKQogICAgewogICAgICAgIGlmKGNoID09IHNlcGFyYXRvcnNbaV1bMF0pCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQoKaW50IG1haW4oKQp7CiAgICBjaGFyIGNoLCBidWZmZXJbMTVdOwogICAgRklMRSAqZnA7CiAgICBpbnQgaiA9IDAsdG9rZW49MDsKCiAgICBmcCA9IGZvcGVuKCJGaXJvei50eHQiLCAiciIpOwogICAgaWYoZnAgPT0gTlVMTCkKICAgIHsKICAgICAgICBwcmludGYoIkVycm9yIG9wZW5pbmcgdGhlIGZpbGVcbiIpOwogICAgICAgIGV4aXQoMCk7CiAgICB9CgogICAgd2hpbGUoKGNoID0gZmdldGMoZnApKSAhPSBFT0YpCiAgICB7CiAgICAgICAgaWYoaXNhbG51bShjaCkpCiAgICAgICAgewogICAgICAgICAgICBidWZmZXJbaisrXSA9IGNoOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBpZihqICE9IDApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGJ1ZmZlcltqXSA9ICdcMCc7CiAgICAgICAgICAgICAgICBqID0gMDsKICAgICAgICAgICAgICAgIHRva2VuKys7CiAgICAgICAgICAgICAgICBpZihpc0tleXdvcmQoYnVmZmVyKSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBwcmludGYoIiVzIGlzIGEga2V5d29yZFxuIiwgYnVmZmVyKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZihidWZmZXJbMF0tJzAnPD0gNDggfHwgYnVmZmVyWzBdLScwJyA+PSA1NykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHByaW50ZigiJXMgaXMgYW4gbm90IGlkZW50aWZpZXJcbiIsIGJ1ZmZlcik7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIHByaW50ZigiJXMgaXMgYW4gaWRlbnRpZmllclxuIiwgYnVmZmVyKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGlmKGlzT3BlcmF0b3IoY2gpPT0xKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0b2tlbisrOwogICAgICAgICAgICAgICAgcHJpbnRmKCIlYyBpcyBhbiBvcGVyYXRvclxuIiwgY2gpOwogICAgICAgICAgICAgICAgaWYoaXNPcGVyYXRvcihjaCkgPT0gMikKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBwcmludGYoIk11bHRpLWNoYXJhY3RlciBvcGVyYXRvciBkZXRlY3RlZFxuIik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGlmKGlzU2VwYXJhdG9yKGNoKSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgdG9rZW4rKzsKICAgICAgICAgICAgICAgIHByaW50ZigiJWMgaXMgYSBzZXBhcmF0b3JcbiIsIGNoKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHByaW50ZigiJWQiLHRva2VuKTsKICAgIGZjbG9zZShmcCk7CiAgICByZXR1cm4gMDsKfQo=