⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ly.c

📁 字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符
💻 C
字号:
/*词法分析*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>

/*关键字*/
char *keyword[32]={"auto","break","case","char","const","continue","default","do",
             "double","else","enum","extern","float","for","goto","if",
             "int","long","register","return","short","signed","sizeof","static",
            "struct","switch","typedef","union","unsigned","void","while","volatile"};
/*操作符*/
char *relop[11]={"&&","||","++","--",">=","<=","==","!=",">","<","<>"};
char *addop[3]={"+","-"};
char *mulop[3]={"*","/"};

/*终结符*/
char *terminate[10]={"[","]","{","}","(",")",",",";","'"};
/*等号*/
char *equal[2]={"="};
int number;

/*判断函数是字符串,关键字或者数字*/
char out(char*str)
{
 int i;

 /*数字*/
 if(isdigit(str[0]))
   return '2';

 /*字符串or关键字*/
 for(i=0;i<32;i++)
   {
      if(strcmp(str,keyword[i])==0)
      return '1';
   }
 if(!isalnum(str[0]))
   return '1';
 else return '0';
}

/*判断单词类型,确定输出结果*/
char *token(char *str)
{
   int i;
   char flag;

   flag=out(str);

   /*操作符*/
   for(i=0;i<11;i++)
   {
      if(strcmp(str,relop[i])==0)
      return "relop ";
   }

   for(i=0;i<3;i++)
   {
      if(strcmp(str,addop[i])==0)
      return "addop ";
   }

   for(i=0;i<3;i++)
   {
      if(strcmp(str,mulop[i])==0)
      return "mulop ";
   }

   /*终结符*/
   for(i=0;i<10;i++)
   {
      if(strcmp(str,terminate[i])==0)
      return (terminate[i]);
   }

   /*=*/
   for(i=0;i<2;i++)
   {
      if(strcmp(str,equal[i])==0)
      return "= ";
   }

   /*字符串,关键字或者数字*/
   if(isalnum(str[0]))
   {
      /*变量输出*/
      if(flag=='0')
      return "id ";
      /*关键字输出*/
      else if(flag=='1')
      {
        return str;
      }
      /*字母输出*/
      else
      return "num ";
    }
}

/*判断单词类型,确定输出结果*/
char *attr(char *str)
{
     return " ";
}

/*判断单词类型,确定输出结果*/
char *convert(char *str)
{
   int i;
   char *c;

   /*字符串,关键字或者数字*/
   if(isalnum(str[0]))
   {
      i++;
      itoa(i,c,10);
      return c;
    }
    else
    {
      i++;
      return "";
    }
}


/*主函数*/
main()
{
   FILE *in,*out,*table,*two,*fp;
   char ch,dualop,buf[30],*programme[1000],*xulie,*att,*n;
   int i=0,j=0,number=0;
   /*从文件中读取数据*/
   if((in=fopen("ly.txt","r+"))==NULL)
   {
      printf("ly.txt open error...\n");
      getch();
      exit(0);
   }

   /*写入文件*/
   if((out=fopen("token.txt","w+"))==NULL)
   {
      printf("token.txt open error\n");
      getch();
      exit(0);
   }

   /*写入文件*/
   if((table=fopen("table.txt","w+"))==NULL)
   {
      printf("table.txt open error\n");
      getch();
      exit(0);
   }

   /*写入文件*/
   if((two=fopen("two.txt","w+"))==NULL)
   {
      printf("two.txt open error\n");
      getch();
      exit(0);
   }

   while(!feof(in))
   {
      ch=fgetc(in);

      /*如果首字符是字母*/
      if(isalpha(ch))
      {
         while(isalnum(ch)&&(i<30))
         {
            buf[i++]=ch;
            ch=fgetc(in);
         }

         if(i>=30)
         {
            printf("the string ");
            printf(buf);
            printf("  is too long!\n");
            getch();
            exit(0);
         }

         buf[i]='\0';
         programme[j++]=(char *)malloc(sizeof(char)*(strlen(buf)+1));
         strcpy(programme[j-1],buf);
         i=0;
         fseek(in,-1L,1);
      }

      /*如果首字符是数字*/
      else if(isdigit(ch))
      {
         while(isdigit(ch)&&(i<30))
         {
            buf[i++]=ch;
            ch=fgetc(in);
         }

         if(i>=30)
         {
            printf("the string ");
            printf(buf);
            printf("  is too long!\n");
            getch();
            exit(0);
         }

         if(isalpha(ch))
         {
            printf("The string ");
            printf(buf);
            printf("%c",ch);
            printf(" is wrong!");
            getch();
            exit(0);
         }
         else
         {
            buf[i]='\0';
            programme[j++]=(char *)malloc(sizeof(char)*(strlen(buf)+1));
            strcpy(programme[j-1],buf);
            i=0;
            fseek(in,-1L,1);
         }
      }
      /*如果首字符既不是数字也不是字母的话*/
      else if(!isalnum(ch))
      {
         /*不是换行和回车*/
         if(ch!='\n'&&ch!=' ')
         {
            /*">=","<=","!="运算符*/
            if(ch=='>'||ch=='<'||ch=='!')
            {
               if((dualop=fgetc(in))=='=')
               {
                  buf[i++]=ch;
                  buf[i++]=dualop;
                  buf[i]='\0';
                  programme[j++]=(char *)malloc(sizeof(char)*3);
                  strcpy(programme[j-1],buf);
                  i=0;
               }
               else
               {
                  buf[i++]=ch;
                  buf[i]='\0';
                  programme[j++]=(char *)malloc(sizeof(char)*2);
                  strcpy(programme[j-1],buf);
                  i=0;
                  fseek(in,-1L,1);
               }
            }
            /*"++","--,"==","||","&&"运算符*/
            else if(ch=='+'||ch=='-'||ch=='&'||ch=='|'||ch=='=')
            {
               if((dualop=fgetc(in))==ch)
               {
                 buf[i++]=ch;
                 buf[i++]=dualop;
                 buf[i]='\0';
                 programme[j++]=(char *)malloc(sizeof(char)*3);
                 strcpy(programme[j-1],buf);
                 i=0;
               }
               else
               {
                  buf[i++]=ch;
                  buf[i]='\0';
                  programme[j++]=(char *)malloc(sizeof(char)*2);
                  strcpy(programme[j-1],buf);
                  i=0;
                  fseek(in,-1L,1);
               }
            }
            else
            {
               buf[i++]=ch;
               buf[i]='\0';
               programme[j++]=(char *)malloc(sizeof(char)*2);
               strcpy(programme[j-1],buf);
               i=0;
            }
         }
      }
   }

   /*输出结果*/
  for(i=0;i<j-1;i++)
  {
    /*token序列*/
    xulie=token(programme[i]);
    fputs(xulie,out);

    /*符号表*/
    att=attr(programme[i]);

    fputc('(',table);
    fputs(programme[i],table);
    fputc(',',table);
    fputs(xulie,table);
    fputc(',',table);
    fputs(att,table);
    fputc(')',table);
    fputc('\n',table);

    /*2元组*/
    n=convert(programme[i]);

    fputc('(',two);
    fputs(xulie,two);
    fputc(',',two);
    fputs(n,two);
    fputc(')',two);
    fputc('\n',two);

  }

  getch();
  return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -