TEXT.C
上传用户:ziyourenyl
上传日期:2013-06-02
资源大小:2k
文件大小:3k
源码类别:

词法分析

开发平台:

C/C++

  1. #include"stdio.h"
  2. #include"string.h"
  3. #include"ctype.h"
  4. int operation(char ch);
  5. int keyword(char c[]);
  6. int symbol(char ch);
  7. spy(char m[],char n[],int i,int j);
  8. char a[200];
  9. char t;
  10. int main()
  11. {
  12.  char sourcename[10];
  13.  char objectname[10];
  14.  FILE *fp;
  15.  char ch;
  16.  int i=0,j;
  17.  char store[30];
  18.  printf("please input sourcefile:");
  19.  scanf("%s",sourcename);
  20.  if((fp=fopen(sourcename,"r"))==NULL)
  21.  {
  22.   printf("file error! please input again.n");
  23.   exit(0);
  24.  }
  25.  printf("n");
  26.  do
  27.  {
  28.   ch=fgetc(fp);
  29.   if(ch=='n') continue;
  30.   a[i]=ch;
  31.   i++;
  32. }while(!feof(fp));
  33. fclose(fp);
  34. printf("Please input objectfile name:");
  35. scanf("%s",objectname);
  36. printf("n");
  37. if((fp=fopen(objectname,"w"))==NULL)
  38. {
  39.  printf("Open Objectfile failure!n");
  40.  exit(0);
  41. }
  42. j=0;
  43. for(i=0;i<(strlen(a)-1);i++)
  44. {
  45.    if(a[i]==' ') continue;
  46.    if(isdigit(a[i]))   {t='5';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
  47.    if(operation(a[i])) {t='3';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
  48.    if(symbol(a[i]))    {t='2';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
  49.    if(isalpha(a[i]))
  50.    {
  51.      j=i+1;
  52.      while(1)
  53.      {
  54.  if(isalpha(a[j])) {j++;continue;}
  55.      break;
  56.  }
  57.      spy(store,a,i,j);
  58.  if(keyword(store))   {t='1';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputs(store,fp);fputc('"',fp);fputc('n',fp);i=j-1;continue;}
  59.  {t='6';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputs(store,fp);fputc('"',fp);fputc('n',fp);i=j-1;continue;}
  60.    }
  61.    if((a[i]=='<')&&(a[i+1]!='=')||(a[i]=='>'&&a[i+1]!='=')) {t='4';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);continue;}
  62.    if((a[i]=='<'&&a[i+1]=='=')||(a[i]=='>'&&a[i+1]=='=')||(a[i]=='='&&a[i+1]=='=')) {t='4';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc(a[i+1],fp);fputc('"',fp);fputc('n',fp);i++;continue;}
  63.    {t='6';fputc('"',fp);fputc(t,fp);fputc('"',fp);fputc(',',fp);fputc('"',fp);fputc(a[i],fp);fputc('"',fp);fputc('n',fp);}
  64. }
  65. }
  66. int operation(char ch)
  67. {
  68. if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
  69.  return 1;
  70.  else return 0;
  71. }
  72. int keyword(char c[])
  73. {
  74.  char  key[][8]={"int","float","char","double","long","if","else","while","for"};
  75.  int j=0,k=0;
  76.  for(j=0;j<9;j++)
  77.  {
  78.   if(!strcmp(c,key[j])) {k=1;break;}
  79.  }
  80.  if(k==1) return 1;
  81.  else return 0;
  82. }
  83. int symbol(char ch)
  84. {
  85.  if(ch=='{'||ch=='}'||ch==','||ch==';'||ch=='('||ch==')'||ch=='"'||ch=='=')
  86.   return 1;
  87.   else return 0;
  88. }
  89. spy(char m[],char n[],int i,int j)
  90. {
  91.  int z=0,k=0;
  92.  for(z=i;z<j;z++)
  93.  {m[k]=n[z];k++;}
  94.  m[k]='';
  95. }