C Program for Lexical Analyzer

Thursday, 6 March 2014

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
char fname[20],ch,ch1[2],arr[60];
int i,j,flag=0,k;
FILE *f1;
char keyw[32][10]={"auto","break","case","const","continue",
"default","do","double","else","enum","extern","float","goto","if",
"long","register","return","short","sizeof","static","struct",
"switch","typedef","union","volatile","while","getch","void","int",
"char","signed","unsigned"};
clrscr();
printf("\n\tENTER THE FILE NAME:");
scanf("%s",fname);
f1=fopen(fname,"r");
if(f1==NULL)
{
printf("\n\tFILE NOT FOUND");
getch();
exit(0);
}
else
{
ch=fgetc(f1);
while(ch!=EOF)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='||
ch=='('||ch==')'||ch=='.'||ch=='['||ch==']'||ch=='{'||
ch=='}'||ch=='|'||ch=='&'||ch=='^'||ch=='%'||ch=='$'||
ch==':'||ch=='?'||ch=='('||ch==')'||ch=='.'||ch=='<'||
ch=='>'||ch=='!'||ch=='?'||ch=='~')
{
i=0;
ch1[i]=ch;
i++;
ch=fgetc(f1);
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||
ch=='='||ch=='('||ch==')'||ch=='.'||ch=='['||
ch==']'||ch=='{'||ch=='}'||ch=='|'||ch=='&'||
ch=='^'||ch=='%'||ch=='$'||ch==':'||ch=='?'||
ch=='('||ch==')'||ch=='.'||ch=='<'||ch=='>'||
ch=='!'||ch=='?'||ch=='~')
{
ch1[i]=ch;
printf("\n\t%c%c IS AN OPERATOR",ch1[i-1],ch1[i]);
}
else
{
printf("\n\t%c IS AN OPERATOR",ch1[i-1]);
continue;
}
}
else if(ch>='a'&&ch<='z')
{
flag=0;
j=0;
while(ch>='a'&&ch<='z'||ch>='0'&&ch<='9')
{
arr[j]=ch;
j++;
ch=fgetc(f1);
}
arr[j]='\0';
for(k=0;k<32;k++)
{
if((strcmp(keyw[k],arr))==0)
{
printf("\n\t%s IS A KEYWORD",arr);
flag=1;
break;
}
}
if(flag==0)
{
printf("\n\t%s IS A IDENTIFIER",arr);
}
continue;
}
else if(ch>='0'&&ch<='9')
{
j=0;
while(ch>='0'&&ch<='9')
{
arr[j]=ch;
j++;
ch=fgetc(f1);
}
arr[j]='\0';
printf("\n\t%s IS A VALUE",arr);
continue;
}
else if(ch==' '||ch=='\n'||ch=='\t')
{
ch=fgetc(f1);
continue;
}
else
{
printf("\n\t%c IS A SPECIAL CHARACTER",ch);
}
ch=fgetc(f1);
}
}
getch();
}


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels