Lexi-1 C
Lexi-1 C
c Page 1 of 2
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int i;
int kywrd(char buf[]){
char key[5][10]={"int","void","break","char","return"};
for(i=0;i<5;i++){
if(strcmp(key[i],buf)==0)
return 1;
}
}
void main(){
char ch,buf[20],oprt[]={"+-/*%="};
int j=0;
FILE *fp;
fp=fopen("prog.txt","r");
while((ch=getc(fp))!=EOF){
if(isalnum(ch)){
buf[j]=ch;
j+=1;
}
else if((ch==' '||ch=='\n') && j!=0){
buf[j]='\0';
j=0;
if(kywrd(buf)==1)
printf("%s is a keyword\n",buf);
else if(isfunc(buf)==1)
printf("%s is a function\n",buf);
else
printf("%s is a identifier\n",buf);
}
else if(ch=='('||ch==')'){
buf[j]=ch;
j+=1;
}
else{
for(i=0;i<6;i++){
if(ch==oprt[i]){
printf("%c operator\n",ch);
break;
}
}
if(j!=0){
buf[j]='\0';
if(kywrd(buf)==1)
printf("%s is a keyword\n",buf);
else if(isfunc(buf)==1)
printf("%s is a function\n",buf);
else
printf("%s is a identifier\n",buf);
}
}
}
}
File: /home/student/s7cse43/lexi.c Page 2 of 2
/*
Output:
(prog.txt)
void main(){
int a=c+b;
}
*/