0% found this document useful (0 votes)
12 views2 pages

Lexi-1 C

Uploaded by

arjunreddy002739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Lexi-1 C

Uploaded by

arjunreddy002739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

File: /home/student/s7cse43/lexi.

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;
}
}

int isfunc(char buf[]){


i=strlen(buf);
if(i<2)
return 0;
else if(buf[i-1]==')' && buf[i-2]=='(')
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;
}

student@ccf30:~/s7cse43$ gcc lexi.c -o lexianalyser


student@ccf30:~/s7cse43$ ./lexianalyser
void is a keyword
main() is a function
main() is a function
int is a keyword
= operator
a is a identifier
+ operator
ac is a identifier
acb is a identifier
acb is a identifier

*/

You might also like