Which part do you need help with? We cannot do the assignment for you, due to EE rules, but we can answer questions. Have you started a program? Have you an idea of the different phases? Do you even have a program that will read your grammar file?
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char exp[500];
char op[21]={'+','-','*','/','%','^','[',']','(',')','{','}','=',';','<','>','#',',','&','"','|'};
char keys[30][30]={"if","while","for","do","include","printf","main","void","scanf","getch","int","char","break","continue","case","const","default","else","double","float","void","isaplha","isdigit","strcmp"};
char token[20];
int i,j,pos,k,h,m,len,c=0,g;
FILE *fp;
FILE *fp1;
int p=0;
clrscr();
fp=fopen("C:\\users\\nasch\\data.c","r");
fp1=fopen("C:\\users\\nasch\\result.txt","w");
while(!feof(fp))
{
p++;
if(fgets(exp,500,fp)!=NULL)
{
len=strlen(exp);
for(i=0;i<=len;i++)
{
if(isalpha(exp[i]))
{
pos=i;
g=i;
while(isalpha(exp[g])||isdigit(exp[g]))
g++;
for(j=0;j<20;j++)
token[j]=NULL;
k=0;
for(j=pos;j<g;j++)
{
3
token[k]=exp[j];
k++;
}
c=0;
if((strcmp(token,"stdio")==0)||(strcmp(token,"conio")==0)||(strcmp(token,"string")==0))
{
fprintf(fp1,"%s.h is a header file\n",token);
g=g+2;
}
else
{
fprintf(fp1,"%s",token);
for(m=0;m<30;m++)
{
if(strcmp(token,keys[m])==0)
c++;
}
if(c==0)
fprintf(fp1," - It is a identifier\n");
else
fprintf(fp1," - It is a key word\n");
}
i=g-1;
}
else if(isdigit(exp[i]))
{
while(isdigit(exp[i+1]))
{
fprintf(fp1,"%c",exp[i]);
i++;
}
fprintf(fp1,"%c",exp[i]);
fprintf(fp1," - It is a digit\n");
}
else
{
if(exp[i]=='+'||exp[i]=='-'||exp[i]=='='||exp[i]=='|'||exp[i]=='<'||exp[i]=='>'||exp[i]=='!')
{
4
i++;
if(exp[i]=='+'||exp[i]=='-'||exp[i]=='='||exp[i]=='|')
fprintf(fp1,"%c%c - It is an operator\n",exp[i-1],exp[i]);
else
{
fprintf(fp1,"%c - It is an operator\n",exp[i-1]);
i--;
}
}
else
{
for(h=0;h<21;h++)
{
if(exp[i]==op[h])
{
fprintf(fp1,"%c - It is an operator\n",op[h]);
break;
}
}
}
}
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char exp[500];
char op[21]={'+','-','*','/','%','^','[',']','(',')','{','}','=',';','<','>','#',',','&','"','|'};
char keys[30][30]={"if","while","for","do","include","printf","main","void","scanf","getch","int","char","break","continue","case","const","default","else","double","float","void","isaplha","isdigit","strcmp"};
char token[20];
int i,j,pos,k,h,m,len,c=0,g;
FILE *fp;
FILE *fp1;
int p=0;
clrscr();
fp=fopen("C:\\users\\nasch\\data.c","r");
fp1=fopen("C:\\users\\nasch\\result.txt","w");
while(!feof(fp))
{
p++;
if(fgets(exp,500,fp)!=NULL)
{
len=strlen(exp);
for(i=0;i<=len;i++)
{
if(isalpha(exp[i]))
{
pos=i;
g=i;
while(isalpha(exp[g])||isdigit(exp[g]))
g++;
for(j=0;j<20;j++)
token[j]=NULL;
k=0;
for(j=pos;j<g;j++)
{
token[k]=exp[j];
k++;
}
c=0;
if((strcmp(token,"stdio")==0)||(strcmp(token,"conio")==0)||(strcmp(token,"string")==0))
{
fprintf(fp1,"%s.h is a header file\n",token);
g=g+2;
}
else
{
fprintf(fp1,"%s",token);
for(m=0;m<30;m++)
{
if(strcmp(token,keys[m])==0)
c++;
}
if(c==0)
fprintf(fp1," - It is a identifier\n");
else
fprintf(fp1," - It is a key word\n");
}
i=g-1;
}
else if(isdigit(exp[i]))
{
while(isdigit(exp[i+1]))
{
fprintf(fp1,"%c",exp[i]);
i++;
}
fprintf(fp1,"%c",exp[i]);
fprintf(fp1," - It is a digit\n");
}
else
{
if(exp[i]=='+'||exp[i]=='-'||exp[i]=='='||exp[i]=='|'||exp[i]=='<'||exp[i]=='>'||exp[i]=='!')
{
i++;
if(exp[i]=='+'||exp[i]=='-'||exp[i]=='='||exp[i]=='|')
fprintf(fp1,"%c%c - It is an operator\n",exp[i-1],exp[i]);
else
{
fprintf(fp1,"%c - It is an operator\n",exp[i-1]);
i--;
}
}
else
{
for(h=0;h<21;h++)
{
if(exp[i]==op[h])
{
fprintf(fp1,"%c - It is an operator\n",op[h]);
break;
}
}
}
}
}
}
}
getch();
}
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
7 Experts available now in Live!