You are on page 1of 2

COMPILER PROGRAMMING

8. Write a program of Lexical Analyzer.

16.
8. Write a program of Lexical Analyzer.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int isKeyword(char buffer[]){
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"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
37
if(strcmp(keywords[i], buffer) == 0){
38
flag = 1;
39
break;
40
}
41
}
42
return flag;
43
}
44
int main(){
45
char ch, buffer[15], operators[] = "+-*/%=";
46
FILE *fp;
47
int i,j=0;
48
fp = fopen("program.txt","r");
49
if(fp == NULL){
50
printf("error while opening the file\n");
51
exit(0);
52
}
53
while((ch = fgetc(fp)) != EOF){
54
for(i = 0; i < 6; ++i){
55
if(ch == operators[i])
56
printf("%c is operator\n", ch);
57
}
58
if(isalnum(ch)){
59
buffer[j++] = ch;
60
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is indentifier\n", buffer);
}}
fclose(fp);
return 0;
}

17.

You might also like