Write a program in C to perform token separation


#include<stdio.h>
#include<conio.h>
void main()
{
      FILE *fp;
      char words[50][25],str[25],ch;
      char key[50][25],op[50][10],id[50][25];

      int i,j,k=0,w=0,p=0,d=0;
      int yes=0;
      fp=fopen("ttoken.c","r");
      clrscr();
      printf("\n File: ttoken.c\n\n");
      if(fp!=NULL)
      {
                  do
                  {
                              j=0;
                              ch='d';
                              strcpy(str,"");

                              while(ch!=' ')
                              {
                                          ch=getc(fp);
                                          printf("%c",ch);

                                          if(ch==EOF) break;

                                          str[j++]=ch;

                              switch(ch)
                              {
                                          case '+':strcpy(op[p++],"+");break;
                                          case '-':strcpy(op[p++],"-");break;
                                          case '*':strcpy(op[p++],"*");break;
                                          case '/':strcpy(op[p++],"/");break;
                                          case '%':strcpy(op[p++],"%");break;
                                          case '=':strcpy(op[p++],"=");break;
                              }

                                          if(ch==' '||ch=='\n'||ch=='=')
                                          {
                                                      j--;
                                                      break;
                                          }

                                          if(ch==';'||ch=='('||ch==')'||ch==',')
                                          {
                                                      j--;
                                                      break;
                                          }

                              }

                              str[j]='\0';



                              strcpy(words[w++],str);


                              if(ch=='(')
                              strcpy(words[w++],"(");
                              if(ch==';')
                              strcpy(words[w++],";");
                              if(ch==')')
                              strcpy(words[w++],")");
                              if(ch=='=')
                              strcpy(words[w++],"=");
                              if(ch==',')
                              strcpy(words[w++],",");

                              j=0;
                  }while(ch!=EOF);
      }


      for(i=0;i<w;i++)
      {//1
                  if((!strcmp(words[i],"for"))||(!strcmp(words[i],"if")))
                  {
                              strcpy(key[k++],words[i]);
                  }
                  if((!strcmp(words[i],"void"))||(!strcmp(words[i],"else")))
                  {
                              strcpy(key[k++],words[i]);
                  }
                  if((!strcmp(words[i],"char"))||(!strcmp(words[i],"int")))
                  {
                              strcpy(key[k++],words[i]);
                              yes=1;
                              for(j=i+1;strcmp(words[j],";");j++)
                              {
                                          if(!strcmp(words[j],"="))
                                          {
                                                      yes=0;
                                                      j++;
                                          }
                                          if(!strcmp(words[j],","))
                                          {
                                                      yes=1;
                                                      j++;
                                          }
                                          if(yes)
                                          strcpy(id[d++],words[j]);
                              }
                  }
     
                  if((!strcmp(words[i],"float"))||(!strcmp(words[i],"double")))
                  {
                              strcpy(key[k++],words[i]);
                              for(j=i+1;strcmp(words[j],";");j++)
                              {
                                          if(!strcmp(words[j],"="))
                                          {
                                                      yes=0;
                                                      j++;
                                          }
                                          if(!strcmp(words[j],","))
                                          {
                                                      yes=1;
                                                      j++;
                                          }
                                          if(yes)
                                          strcpy(id[d++],words[j]);
                              }
                  }

      }//1

      printf("\n\n* Identifiers:\n");
      for(i=0;i<d;i++)
      {
                  printf("%s  ",id[i]);
      }
     
      printf("\n\n* Keywords:\n");
      for(i=0;i<k;i++)
      {
                  printf("%s  ",key[i]);
      }

      printf("\n\n* Operators:\n");
      for(i=0;i<p;i++)
      {
                  printf("%s  ",op[i]);
      }

      fclose(fp);
      getch();
}

Post a Comment

0 Comments