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

📄 compile.txt

📁 这是我编写的一个词法分析器,有兴趣的人可以看看,里面有详细的说明,我觉得非常适合初学者看
💻 TXT
字号:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>

#define N 256

char str[N];  

char strtoken[20];  

char *key[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" ,"volatile" ,"while"};



int len; 

int count=0; 

void write(char *wstr,char wc,FILE *wout) //将分析结果按照规则写入到文件
{
       fputc('(',wout);
       fputs(wstr,wout);
       fputc(',',wout);
       fputc(wc,wout);
       fputc(')',wout);

}
void write2(char *wstr,char *wc,FILE *wout) //将分析结果按照规则写入到文件
{
       fputc('(',wout);
       fputs(wstr,wout);
       fputc(',',wout);
       fputs(wc,wout);
       fputc(')',wout);

}
int readstr(FILE *fp)
{
char ch;
len=0;
ch=fgetc(fp);
while(!feof(fp) && ch!='\n')
{
str[len]=ch;
ch=fgetc(fp);
len++;
}

len--;

if(feof(fp))
return 0;
else
return 1;

}
void error(int type)
{

 if(type==1)
  printf("is a invalid char,the%d line has a error the id should not begin from a digit\n",count);
 else if(type==2)
      printf("the%d lines has a error,the evaluate should be  <variable>=<expression> \n ",count);

     else printf("is a invalid character,the %d line has a error\n",count);

}

void check(char *str,FILE *out);
void fenxi(char *row,FILE *op)
{

 int k=0;
 int i=0;
 int ferror=0;
 char str0[20]; //临时存放单词的变量
 int j=0;
//printf("this is the %dth line\n",count);
 while(i<=len)
 {
  k=0;
  strcpy(strtoken,"\0");
  if(isspace(row[i]))
  {
   i++;
      continue;
  }

 while(!isalpha(row[i])&&!isdigit(row[i])&&i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]=='{'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]=='}' || row[i]==';' || row[i]=='='))
 {
  putchar(row[i]);
  i++;
  ferror=1;
 }
  if(ferror==1)
  {
   error(3);
   ferror=0;
  }

 if(row[i]=='/') //判断是否为注释
  {
   i++;
   if(row[i]=='/')
   {
    i=len+1;
    continue;
   }
   else
    i--;
  }


     if(isdigit(row[i]))
  {
       while(i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';' || row[i]=='='||row[i]=='{'||row[i]=='}' ))


   {

	      if(isdigit(row[i]))
      {
	    str0[k]=row[i]; //是数字则将字符逐个存入临时数组
		  i++;
       k++;

      }
	 else
      {

       ferror=1;break;

      }
   }
	    if(ferror==1)
   {
    for( j=0;j<k;j++)
     putchar(str0[j]);
     while(i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]==':'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';'||row[i]=='='))
     {
     putchar(row[i]);
      i++;

     }
      error(1);

      ferror=0;
      i--;
      strcpy(strtoken,"");
   }
      else
     {
      str0[k]='\0';
      strcpy(strtoken,str0);
      i--;
      str0[0]='\0';

     }
  }

     if(isalpha(row[i]))
  {

   k=0;
      while(i<=len&&row[i]!=32&&!(row[i]=='\0'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')' || row[i]==';'||  row[i]=='='))//关键字和标志符由数字和字母组成
   {
    if(isalpha(row[i])||isdigit(row[i]))
    {
	    str0[k]=row[i];
      i++;
      k++;
    }
    else
    {


     ferror=1;
     break;
    }

   }
   if(ferror)
   {
    for( j=0;j<k;j++)
     putchar(str0[j]);
    while(i<=len&&!isspace(row[i])&&!(row[i]=='\0'||row[i]=='>'||row[i]=='<'||row[i]=='+' || row[i]=='-' || row[i]=='*' || row[i]=='/' || row[i]=='(' || row[i]==')'|| row[i]==';'||row[i]=='='))
    {
     putchar(row[i]);
     i++;

    }
    ferror=0;
    error(3);
    i--;
   }
   else
   {
	str0[k]='\0';
		 strcpy(strtoken,str0);
	str0[0]='\0';
	   i--;
   }
	}

	 if(row[i]=='/' || row[i]=='(' || row[i]==')'   || row[i]==';'||row[i]=='{'||row[i]=='}' || row[i]=='=')
   {
    str0[0]=row[i];
    str0[1]='\0';
    strcpy(strtoken,str0);
    str0[0]='\0';
  }

       if(row[i]=='=')
  {
   i++;
   if(row[i]=='=')
   {
    strtoken[0]='=';
    strtoken[1]='=';
    strtoken[2]='\0';
    strcpy(strtoken,"==");

    }
	      else
     {
      strcpy(strtoken,"=");
      i--;
     }
  }


       if(row[i]=='+')
  {
   i++;
   if(row[i]=='+')
   {
    strtoken[0]='+';
    strtoken[1]='+';
    strtoken[2]='\0';
    strcpy(strtoken,"++");

    }
	      else
     {
      strcpy(strtoken,"+");
      i--;
     }
  }


       if(row[i]=='-')
  {
   i++;
   if(row[i]=='-')
   {
    strtoken[0]='-';
    strtoken[1]='-';
    strtoken[2]='\0';
    strcpy(strtoken,"--");

    }
	      else
     {
      strcpy(strtoken,"-");
      i--;
     }
  }

       if(row[i]=='*')
  {
   i++;
   if(row[i]=='*')
   {
    strtoken[0]='*';
    strtoken[1]='*';
    strtoken[2]='\0';
    strcpy(strtoken,"**");

    }
	      else
     {
      strcpy(strtoken,"*");
      i--;
     }
  }


  if(row[i]=='>')
  {
   i++;
   if(row[i]=='=')
   {

    strcpy(strtoken,">=");
   }
	      else
     {
      strcpy(strtoken,">");
      i--;
     }
  }
  if(row[i]=='<')
  {
   i++;
   if(row[i]=='=')
   {
    strcpy(strtoken,"<=");


   }
else
     { strcpy(strtoken,"<");i--;}
  }

   puts(strtoken);


    check(strtoken,op);

    strtoken[0]='\0';
     i++;

 }
 }

void check(char *str,FILE *out)
{      int fyiyong=0;
       int ct;
       if(isdigit(str[0]))
    {
     write2(str,"digit",out);
    }

	   if(isalpha(str[0]))
     {



      for( ct=0;ct<32;ct++)
      {
	if(!strcmp(str,key[ct]))
     {
      write2(str,"key",out);
      fyiyong=1;
     }
      }

    if(fyiyong!=1)
    {
	       write2(str,"identifier",out);
    }
	}

     if(str[0]=='>')
     {
      if(str[1]=='=')
      {
		   write2(str,">=logic expression",out);
      }
      else
      {
		   write2(str,">logic expression",out);
      }

     }

     if(str[0]=='<')
     {
      if(str[1]=='=')
      {
		 write2(str,"<=logic expression",out);
      }
      else
      {
		     write2(str,"<logic expression",out);
      }

     }


	if(!strcmp(str,"=="))
     {
	       write2(str,"==logic expression",out);
     }
     

    if(str[0]=='+')
     {
      if(str[1]=='+')
      {
		 write2(str,"++selfplus",out);
      }
      else
      {
		     write2(str,"+plus",out);
      }

     }

     
    if(str[0]=='-')
     {
      if(str[1]=='-')
      {
		 write2(str,"--selfsub",out);
      }
      else
      {
		     write2(str,"+sub",out);
      }

     }
 
    if(str[0]=='*')
     {
      if(str[1]=='*')
      {
		 write2(str,"power",out);
      }
      else
      {
		     write2(str,"mul",out);
      }

     }
 if(str[0]=='/' ){  write2(str,"div",out);}
 if(str[0]=='=') {  write2(str,"=assign",out);}   
 if(str[0]=='(' ){  write2(str,"lpar",out);}
 if(str[0]==')' ){  write2(str,"rpar",out);}
 if(str[0]==';' ){  write2(str,"semicolon",out);}
 if(str[0]=='{' ){  write2(str,"lbrace",out);}
 if(str[0]=='}' ){  write2(str,"rbrace",out);}


}

void main()
{
 int count=1;
 char scfilename[20],rsfilename[20];
 FILE *fp,*op;
 printf("Please input your source file name:");
 gets(scfilename);
    printf("Please input your result file name:");
 gets(rsfilename);

 fp=fopen(scfilename,"r");
    op=fopen(rsfilename,"w");
 if(fp)
 {
      while(readstr(fp))
   {
      fenxi(str,op);
   count++;
	}
 }
 else
 {
  printf("Your source file not exist!!!\n");
  exit(0);
 }

 fclose(fp);
 fclose(op);
    printf("ok!");

}

⌨️ 快捷键说明

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