EXPERIMENT 1
To develop a lexical analyzer to recognize a few patterns in C. (Ex. identifiers, constants, comments, operators etc.). Create a symbol table, while recognizing identifiers.

========================================================================

C Program
---------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isKeyword(char str[]) {
    char keywords[][10] = {
        "int","float","char","double","if",
        "else","for","while","return","void"
    };
for(int i = 0; i < 10; i++) {
    if(strcmp(str, keywords[i]) == 0)
    return 1;
}
return 0;
}
int main() {
    FILE *fp;
    char ch, word[50];
    int i;
    fp = fopen("input.c", "r");
    if(fp == NULL) {
        printf("File not found!\n");
        return 0;
    }
printf("\nKEYWORDS\n");
printf("---------\n");
rewind(fp);
i = 0;
while((ch = fgetc(fp)) != EOF) {
    if(isalnum(ch) || ch == '_')
    word[i++] = ch;
    else {
        if(i > 0) {
            word[i] = '\0';
            if(isKeyword(word))
            printf("%s\n", word);
            i = 0;
        }
}
}
printf("\nIDENTIFIERS\n");
printf("-----------\n");
rewind(fp);
i = 0;
while((ch = fgetc(fp)) != EOF) {
    if(isalnum(ch) || ch == '_')
    word[i++] = ch;
    else {
        if(i > 0) {
            word[i] = '\0';
            if(!isKeyword(word) && !isdigit(word[0]))
            printf("%s\n", word);
            i = 0;
        }
}
}
printf("\nNUMBERS\n");
printf("-------\n");
rewind(fp);
i = 0;
while((ch = fgetc(fp)) != EOF) {
    if(isdigit(ch)) {
        printf("%c ", ch);
    }
}
printf("\n\nOPERATORS\n");
printf("---------\n");
rewind(fp);
while((ch = fgetc(fp)) != EOF) {
    if(ch=='+' || ch=='-' || ch=='*' || ch=='/' ||
    ch=='=' || ch=='<' || ch=='>' || ch=='%') {
        printf("%c ", ch);
    }
}
printf("\n");
fclose(fp);
return 0;
}

Example Input Program
---------------------
#include <stdio.h>
int main() {
    int a = 10, b = 20;
    int sum = a + b;
    printf("Sum = %d\n", sum);
    if (sum > 20) {
        printf("Greater than 20\n");
    }
return 0;
}
