//copyright this program is made by Jayesh Amin....
//This program is used to perform lexical Analysis....
//function for checking duplicate values
int check(char a[],char x,int z)
{
int y;
for(y=0;y<z;y++)
{
if(a[y]==x)
return 1;
}
return 0;
}
//function for printing lexical analysis
int print(char a[],char x1)
{
int q=0;
while(a[q]!=x1)
{
q++;
}
return q;
}
//main program starts
void main()
{
//variable declaration
int no,i,p,opi=0,keyi=0,idi=0,keyj=0,idp,opp,j,c,keyflag=1;
char stat[10][30],key[50],op[50],id[50],temp;
clrscr();
/* entering statements */
printf("\nHow many statements you want to enter???\n");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nenter statement%d\n",i+1);
scanf("%s",stat[i]);
}
clrscr();
printf("\nyour statements are:\n\n");
for(i=0;i<no;i++)
{
printf("%s\n",stat[i]);
}
printf("\n");
//logic of the program starts
printf("your answer of lexical analysis is:\n\n");
for(i=0;i<no;i++)/*loop for no. of statements*/
{
for(j=0;j<strlen(stat[i]);j++)/*loop for statement length*/
{
/*if 1st char is identifier and after that ':' is there then do this*/
if((stat[i][j]>=97 && stat[i][j]<=122) && (stat[i][j+1]==':'))
{
if(check(id,temp,idi)==0)
{
id[idi]=stat[i][j];
idi++;
}
temp=stat[i][j];
idp=print(id,temp);
printf("(id#%d)",idp+1);
j++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
j++;
while(stat[i][j]!=';')
{
key[keyi]=stat[i][j];
keyi++;
j++;
}
key[keyi]=' ';
keyi++;
printf("(key#%d)",keyflag);
keyflag++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
break;
}
/*if 1st char is identifier and after that ',' is there then do this*/
else if((stat[i][j]>=97 && stat[i][j]<=122) && (stat[i][j+1]==','))
{
if(check(id,temp,idi)==0)
{
id[idi]=stat[i][j];
idi++;
}
temp=stat[i][j];
idp=print(id,temp);
printf("(id#%d)",idp+1);
j++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
}
/*if 1st char is identifier and after that '=' is there then do this*/
else if((stat[i][j]>=97 && stat[i][j]<=122) && (stat[i][j+1]=='='))
{
temp=stat[i][j];
idp=print(id,temp);
printf("(id#%d)",idp+1);
j++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
j++;
while(stat[i][j]!=';')
{
c=(int)stat[i][j];
if((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122))
{
temp=c;
idp=print(id,temp);
printf("(id#%d)",idp+1);
}
else
{
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
}
j++;
}
temp=stat[i][j];
opp=print(op,temp);
printf("(op#%d)",opp+1);
break;
}
else
{
}
}
printf("\n");
}
op[opi]=NULL;
key[keyi]=NULL;
id[idi]=NULL;
/* To print identifiers,operators and keywords seperately */
printf("\nidentifiers are--->%s\n",id);
printf("\noperators are----->%s\n",op);
printf("\nkeywords are------>%s\n",key);
getch();
}