Professional Documents
Culture Documents
CD Lab Exercise 2
CD Lab Exercise 2
AIM:
ALGORITHM:
Step 2: Feed the program with all the operators, functions, keywords and special characters in a
separate array.
Step 3: Open the input file and start reading the words one by one.
Step 4: Compare them with all the four array if found in any one then print the character and its
specification in the output file and goto step 6.
Step 5: If not any of four arrays contains it then check whether it is a constant if not then print it
as identifier in the output file.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
int a,b,I,c;
char*str;
char *key[]={"int","char","for","while","void","main"};
char oper[]={'+','-','*','/','&','+','=','!'};
char spl[]={'(',')','{','}','[',']',';',':','%'};
char *func[]={"printf","scanf","gatch","clrscr"};
FILE *fp1,*fp2;
fp1=fopen("add.c","r");
fp2=fopen("output.txt","w");
clrscr();
while(!feof(fp1))
{
fscanf(fp1,"%s",str);
b=0;c=0;
for(i=0;i<6;i++)
{
if(strcmp(str,key[i])==0)
{
b=1;
fprintf(fp2,"%s is a keyword\n",str);
break;
}
}
for (i=0;i<4;i++)
{
if(strcmp(str,func[i])==0)
{
b=1;
fprintf(fp2,"%s is a function\n",str);
break;
}
}
for(i=0;i<7;i++)
{
if(str[0]==oper[i])
{
b=1;
fprintf(fp2,"%s is a operator\n",str);
break;
}
}
for(i=0;i<11;i++)
{
if(str[0]==spl[i])
{
b=1;
fprintf(fp2,"%s is a special character\n",str);
break;
}
}
if(b==0)
{
c=atoi(str);
if(c!=0)
fprintf(fp2,"%s is a constant\n",c);
else
fprintf(fp2,"%s is a identifier\n",str);
}
}
fclose(fp1);
fclose(fp2);
getch();
}
IMPUT:
ADD.c
void main()
int a=10;
printf("sample %d",a);
OUTPUT:
Result:
Thus the above program for developing the lexical analyzer and recognizing the few patterns in
C is executed successfully and the output is verified.