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

📄 lexer.cpp

📁 自己用C语言编写的一个关于词法分析器的源代码
💻 CPP
字号:
#include"init.h"

void parse() 
{
    
	char ch, *arr="";

	char filenamein[20],filenameout[20];
    int i=0,j=0,count=0;
	FILE *fpin , *output;
	int type[]={0,1,2,3}; /* 关键字,标示符,数字,符号*/

	printf(" Input the name of your input file: ");
     scanf("%s",filenamein);
     if((fpin=fopen(filenamein, "r"))==NULL)
     {
         printf(" cannot open the file\n");
         exit(0);
     }

     printf(" Input the name of your output file: ");
     scanf("%s",filenameout);
     if((output=fopen(filenameout,"w"))==NULL)
     {
         printf(" cannot open the file\n");
         exit(0);
     }

    ch=fgetc(fpin);

    while(ch!=EOF)
    {

        i=0;
        if( ch==' '|| ch =='\t')          /* 去除空格*/
            ch=fgetc(fpin);
        else if( ch=='\n')              /* 去除回车符*/
		{
			lineno++;
			 ch=fgetc(fpin);
			  
		}
        else
        if( isalpha(ch)||ch=='_')  /* 开头是字母或下划线*/
        {
            while(isalpha(ch)||isdigit(ch)||ch=='_')
            {

                arr[i++] = ch;
              
                ch=fgetc(fpin);
            }
            arr[i]='\0';

            if( Find(arr))
            {
                fprintf(output,"%-10s\t%5d\t%-5s \n",arr,type[1],"id");  /* 标示符*/
            }
            else
            {
                 fprintf(output,"%-10s\t%5d\t%-5s\n",arr,type[0],"kword"); /*  是关键字*/
             }
             continue;
         }


         else
         if(isdigit(ch))    /* 分析数字的情况*/
         {
             while(isdigit(ch))
             {
                 arr[i]=ch;
                 i++;
                 ch=fgetc(fpin);
             }
        
             if (ch=='.')   /*出现非点时*/
             {
                 arr[i]=ch; /*将点加入*/
                 i++;
                 ch=fgetc(fpin);/*继续读取*/
                 if (isdigit(ch))
                 {
                     while(isdigit(ch)) /*读取的是数字时,加入*/
                     {
                         arr[i]=ch;
                         i++;
                         ch=fgetc(fpin);
                     }
    

                 /*如果是数字加点,以后再加非数字的情况出现时,就是错误*/
                    if(isalpha(ch))
                    {
                        while(isdigit(ch)||isalpha(ch)||ch=='_')   
                        {
                            arr[i++]=ch;
                            ch=fgetc(fpin);
                        }
                        arr[i]='\0';

                        fprintf(output,"%-10s\t unexpect in %-5d line ",arr,lineno);
                        continue;   /* 退出当前循环 if */
                    }

                /*当出现结束符时,就收入为实数内*/
                  else
                  {
                     arr[i]='\0';
                     fprintf(output,"%-10s\t%5d\t%-5s\n",arr,type[2],"num");/* 实数 */
                     continue;
                  }  
            }   /*  157 line if */
        }  /*  152 line if */
   
      /*如果是字符,则判断为标识错误*/
       else if(isalpha(ch))
       {
          while(isdigit(ch)||isalpha(ch)||ch=='.')      
          {
              arr[i++]=ch;
              ch=fgetc(fpin);
          }
          arr[i]='\0';

          fprintf(output,"%-10s\t unexpect in %-5d line ",arr,lineno);
          continue; 
      }
   /*如果读取的为结束时,就判断为常数*/  
      else
      {
          arr[i]='\0';

          fprintf(output,"%-10s\t%5d\t%-5s\n",arr,type[2],"num");
          continue;
      }
  } /* 142  else if */


  else if( ch=='>')            
  {
      arr[i++]=ch;
	  ch=fgetc(fpin);
      if( ch=='=')
	  {
		  fprintf(output,"%-10s\t%5d\t%-5d\n",">=",type[3],25);     /* 为运算符 ">="*/
		  ch=fgetc(fpin);
		  continue;
	  }
      else
	  {
		  fprintf(output,"%-10s\t%5d\t%-5d\n",">",type[3],24);      /* 为运算符 ">"*/
		  ch=fgetc(fpin);
		  continue;
	  }

  }


  else if (ch=='<')
  {
      arr[i++]=ch;
	  ch=fgetc(fpin);
      if( ch=='=')
	  {
		  fprintf(output,"%-10s\t%5d\t%-5d\n","<=",type[3],23);   /* 为运算符 "<="*/
		  ch=fgetc(fpin);
		  continue;
	  }

     if( ch=='<')
	 {
             fprintf(output,"%-10s\t%5d\t%-5d\n","<<",type[3],20);    /* 为运算符 "<<"*/
			 ch=fgetc(fpin);
		     continue;
	 }
     fprintf(output,"%-10s\t%5d\t%-5d\n","<",type[3],22);      /* 为运算符 "<"*/
	 continue;
	}
   
  else if(ch=='"')   /* 处理字符串的问题 */
  { 
	  arr[i++]=ch;
      ch=fgetc(fpin);
      while(ch!='"'&&ch!=EOF)
	  {
		  arr[i++]=ch;
          ch=fgetc(fpin);    
	  }
      if(ch=='"')
	  {
		  arr[i++]=ch;
          ch=fgetc(fpin);
          arr[i]='\0';
          fprintf(output,"%-10s\t\t\t字符串\n",arr);
		  
          continue;
	  }
  }

  else
	  if(ch=='-') 
	  {
		  arr[i++]=ch;
		  ch=fgetc(fpin);
		  if( ch=='-')
		  {
			  fprintf(output,"%-10s\t%5d\t%-5d\n","--",type[3],12);    /* 为运算符 "--"*/
			  ch=fgetc(fpin);
			  continue;
		  }
		  if(ch=='>')
		  {

			  fprintf(output,"%-10s\t%5d\t%-5d\n","->",type[3],8);    /* 为运算符 "->"*/
			  ch=fgetc(fpin);
			  continue;
		  }
		  arr[i]='\0';
		  fprintf(output, "%-10s\t%5d\t%-5d\n",arr,type[3],19);      /* 为运算符 "-"*/
		  ch=fgetc(fpin);
		  continue;
	  }
  else 
	  if(ch=='+') 
	  {
		  arr[i++]=ch;
		  ch=fgetc(fpin);
		  if(ch=='+')
		  {
			  fprintf(output, "%-10s\t%5d\t%-5d\n","++",type[3],11);   /* 为运算符 "++" */
			  ch=fgetc(fpin);
			  continue;
		  }
		  arr[i]='\0';
		  fprintf(output, "%-10s\t%5d\t%-5d\n",arr,type[3],18);      /* 为运算符 "+" */
		  ch=fgetc(fpin);
		  continue;
	  }
  else
	  if(ch=='*')
	  {
          fprintf(output,"%-10s\t%5d\t%-5d\n","*",type[3],15);    /* 为运算符 "*" */
		  ch=fgetc(fpin);
		  continue;
	  }
   else
	   if(ch=='/')
	   {
		  arr[i++]=ch;
		  ch=fgetc(fpin);
		  if(ch=='*')         /*若为注释的开始,标示出包含在里面的所有字符*/
		  {
			  arr[i++]=ch;  
			  count=0;
				ch=fgetc(fpin);
				while(count!=2)
                {          /*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/
					count=0;
					while(ch!='*')
					{
						arr[i++]=ch;
						ch=fgetc(fpin);
					}
					count++;
					arr[i++]=ch;
					ch=fgetc(fpin);
					if(ch=='/')
						count++;
					else
					{
						
						ch=fgetc(fpin);
					}
				}
				arr[i++]=ch;
				 arr[i]='\0';
				 fprintf(output,"%s\t 注释\n",arr);
				continue;
		  }
		 else
		  if(ch=='=')
		  {
			  fprintf(output,"%-10s\t%5d\t%-5d\n","/=",type[3],34);  /* 为运算符 "/=" */
			  continue;
		  }
		   else
		   {
			   fprintf(output, "%-10s\t%5d\t%-5d\n","/",type[3],16);  /* 为运算符 "/" */
			   ch=fgetc(fpin);
			   continue;
		   }
	   }
	
	   
	   else
		if(ch=='%')
		{
			arr[i++]=ch;
		    ch=fgetc(fpin);
		    if(ch=='=')
			{
			  fprintf(output,"%-10s\t%5d\t%-5d\n","%=",type[3],42);  /* 为运算符 "%=" */
			  continue;
			}
			else
			{
				fprintf(output, "%-10s\t%5d\t%-5d\n","%",type[3],17);  /* 为运算符 "%" */
				ch=fgetc(fpin);
			    continue;
			}
		}
	else
		if(ch=='&')
		{
			arr[i++]=ch;
			ch=fgetc(fpin);
			if(ch=='&')
			{
				arr[i++]=ch;
				arr[i]='\0';
				fprintf(output,"%-10s\t%5d\t%-5d\n",arr,type[3],28);  /* 为运算符 "&&" */
				ch=fgetc(fpin);
				continue;
			}
			arr[i]='\0';
			fprintf(output,"%-10s\t%5d\t%-5d\n",arr,type[3],13);   /* 为运算符 "&" */
		    ch=fgetc(fpin);
			continue;
			
		}
	else 
	  if(ch==';') 
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n",";",type[3],36);  /* 为限界符 ";" */
		  ch=fgetc(fpin);
		  continue;
	  }
  else 
	  if(ch=='(') 
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n","(",type[3],4);  /* 为限界符 "(" */
		  ch=fgetc(fpin);
		  continue;
	  }
  else 
	  if(ch==')')
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n",")",type[3],5);   /* 为限界符 ")" */
		  ch=fgetc(fpin);
		  continue;
	  }
  else 
	  if(ch=='[') 
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n","[",type[3],6);   /* 为限界符 "[" */
		  ch=fgetc(fpin);
		  continue;
	  }
  else 
	  if(ch==']') 
	  {

          fprintf(output,"%-10s\t%5d\t%-5d\n","]",type[3],7);  /* 为限界符 "]" */
		  ch=fgetc(fpin);
		  continue;
	  }
   else 
	  if(ch=='{') 
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n","{",type[3],37);  /* 为限界符 "{" */
		  ch=fgetc(fpin);
		  continue;
	  }
	else 
	  if(ch=='}') 
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n","}",type[3],38);  /* 为限界符 "}" */
		  ch=fgetc(fpin);
		  continue;
	  }
    else 
	  if(ch=='.') 
	  {

          fprintf(output, "%-10s\t%5d\t%-5d\n",".",type[3],9);  /* 为限界符 "." */
		  ch=fgetc(fpin);
		  continue;
	  }
    else 
	  if(ch==',') 
	  {
          fprintf(output, "%-10s\t%5d\t%-5d\n",",",type[3],35);  /* 为限界符 "," */
		  ch=fgetc(fpin);
		  continue;
	  }
    else
	  if(ch=='=')
	  {
		  arr[i++]=ch;
		  ch=fgetc(fpin);
		  if(ch=='=')
		  {
			  fprintf(output, "%-10s\t%5d\t%-5d\n","==",type[3],26);  /* 为运算符 "==" */
		      ch=fgetc(fpin);
		      continue;
		  }
		  fprintf(output, "%-10s\t%5d\t%-5d\n","=",type[3],30);  /* 为运算符 "=" */
		  continue;
		 
	  }
     else 
	   if(ch=='!')
	   {
		   arr[i++]=ch;
	       ch=fgetc(fpin);
	       if(ch=='=')
		   {
			   fprintf(output, "%-10s\t%5d\t%-5d\n","!=",type[3],27);  /* 为运算符 "!=" */
		       ch=fgetc(fpin);
		       continue;
		   }
	       fprintf(output, "%-10s\t%5d\t%-5d\n","!",type[3],10);  /* 为运算符 "!" */
	       continue;
	   }
     else 
	   if(ch=='|')
	   {
		   arr[i++]=ch;
	       ch=fgetc(fpin);
	       if(ch=='|')
		   {
			   fprintf(output, "%-10s\t%5d\t%-5d\n","|",type[3],43);  /* 为运算符 "|" */
		       ch=fgetc(fpin);
		       continue;
		   }
	       fprintf(output, "%-10s\t%5d\t%-5d\n","||",type[3],29);   /* 为运算符 "||" */
	       continue;
	   }
   
else 
		{
			fprintf(output,"error in  %d lines unknown character %c \n",lineno,ch);
			ch=fgetc(fpin);
		    continue;
		}
  
 }        
 
 fclose(fpin);
 fclose(output);
}



int main()

{
 
	printf("****** you can look up  help.txt  for reading the result ******\n");
	printf("****** the result will be saved in the file you named it ******\n\n\n");
	init();
	Help();
    parse();

}

⌨️ 快捷键说明

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