Friday, May 28, 2010

WAP to perform macro expansion


#include<stdio.h>
#include<conio.h>
void main()
{
      char words[25][25];
      char str[25],ch;
      int i,j,w=0,m=0,flag=0;
      char macro[10][25];
      char val[10][25];
      long l;
      FILE *fp,*fp1;
      fp=fopen("text.c","r");
      fp1=fopen("temp.c","w");
      clrscr();
      printf("\n File: text.c before MACRO EXPANSION\n\n");
      if(fp!=NULL)
      {
                  do
                  {
                              j=0;
                              ch='d';
                              strcpy(str,"");
                              while(ch!=' ')
                              {
                                          ch=getc(fp);
                                          printf("%c",ch);
                                          if(ch==EOF) break;
                                          putc(ch,fp1);

                                          str[j]=ch;
                              if(ch!=' '&&ch!='\n'&&ch!=';'&&ch!='='&&ch!='+')
                                          j++;
                                          if(ch==' '||ch=='\n') break;
                                          if(ch==';'||ch=='='||ch=='+') break;
                              }
                              str[j]='\0';
                              strcpy(words[w++],str);
                              j=0;
                  }while(ch!=EOF);
      }


      for(i=0;i<w;i++)
      {
                  if(!strcmp(words[i],"#define"))
                  {
                              strcpy(macro[m++],words[++i]);
                  }
      }



      for(i=0;i<w;i++)
      {
                  if(!strcmp(words[i],"main()"))
                  break;
                  for(j=0;j<m;j++)
                  {
                              if(!strcmp(words[i],macro[j]))
                              {
                                          strcpy(val[j],words[++i]);
                              }
                  }
      }


      fclose(fp);
      fclose(fp1);
      fp=fopen("text.c","w");
      fp1=fopen("temp.c","r");
      if(fp1!=NULL)
      {
                  do
                  {
                              j=0;
                              ch='d';
                              strcpy(str,"");
                              while(ch!=' ')
                              {
                                          ch=getc(fp1);
                                          if(ch==EOF) break;
                                          putc(ch,fp);
                                          str[j]=ch;

                              if(ch!=' '&&ch!='\n'&&ch!=';'&&ch!='='&&ch!='+')
                                          j++;
                                          if(ch==' '||ch=='\n') break;
                                          if(ch==';'||ch=='='||ch=='+') break;
                              }
                              str[j]='\0';
                              if(flag)
                              {
                                          for(i=0;i<m;i++)
                                          {
                                                      if(!strcmp(str,macro[i]))
                                                      {
                                                                  l=strlen(str);
                                                                  fseek(fp,-(1*l+1),SEEK_CUR);
                                                                  fputs(val[i],fp);
                                                      }//if
                                          }//for
                              }//if-flag

                              if(ch==';')
                              putc(ch,fp);

                              if(!strcmp(str,"main()"))
                              flag=1;
                              j=0;
                  }while(ch!=EOF);
      }

      fclose(fp);
      fclose(fp1);
      fp=fopen("text.c","r");
      printf("\n\n\n File: text.c after MACRO EXPANSION\n\n");
      if(fp!=NULL)
      {
                  do
                  {
                              ch=getc(fp);
                              printf("%c",ch);
                  }while(ch!=EOF);
      }
      fclose(fp);
      getch();
}

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...