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

📄 unit1.cpp

📁 此为词法分析程序中的程序清单
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      Form1->Animate3->Play(1,24,1);
     }
      k=2;
    }
 switch(k)
 {
  case 0:
          if(b[0]!='\0')                  //当B数组非空时将B数组中数字进行处理
          {
           n=and(j,b);
	   i=0;                            //数组A下标清零  
            add=ad;                       //分配内存单元
            add=add+"H";
            ad=ad+2;
           Form1->StringGrid1->Cells[5][x]=n;    //输出数字到表格
           Form1->StringGrid1->Cells[4][x]=add;
           Form1->StringGrid1->Cells[3][x]="常数数字";
           Form1->StringGrid1->Cells[2][x]="c";
           Form1->StringGrid1->Cells[1][x]='1';
           Form1->StringGrid1->Cells[0][x]=x;
           if(ssj==0)
           fprintf(fp2,"%10d,         1,         c,    常数数字,%10s,%10d\n",x,add,n);
           x++ ;
          }
          if(i>9)                       //当A数组中字符数量多于10个出错处理
          {
           ShowMessage("此字符串中字母数目多于10个");
           Form1->StringGrid1->Cells[5][x]=a;
           Form1->StringGrid1->Cells[4][x]="error";
           Form1->StringGrid1->Cells[3][x]="error";
           Form1->StringGrid1->Cells[2][x]="error";
           Form1->StringGrid1->Cells[1][x]="error";
           Form1->StringGrid1->Cells[0][x]=x;
           if(ssj==0)
           fprintf(fp2,"%10d,     error,       error,     error,     error,%10d\n",x,n);
           x++;
           a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
           while(s[p+1]>=65&&s[p+1]<=90||s[p+1]>=97&&s[p+1]<=122)
           {
            p++;                       //将多余的即10个以后的字符忽略不参与词法分析  
           }
           return p;
          }
          a[i]=ch;                    //将字母CH存入暂存数组A中
          Form1->Edit4->Text=a;
          i=i+1;                      //A数组下标加一 
	  a[i]='\0';
	  break;
  case 1:
          if(a[0]!='\0')             //当A数组非空时将A数组中字母进行处理
          {
           if(check(a)==1)           //检验保留字,用户自定义字符串
           {
            j=0;                     //数组B下标清零  
	   }
           else
	   {
	    j=0;
	   }
   	  }
          b[j]=ch;                   //将数字CH存入暂存数组A中
          Form1->Edit5->Text=b;
	  j=j+1;                      //B数组下标加一 
	  b[j]='\0';
	  break;

  case 2:
	 i=0;
         if(b[0]!='\0')             //同CASE0
         {
          n=and(j,b);
	  i=0;
          add=ad;
            add=add+"H";
            ad=ad+2;
          Form1->StringGrid1->Cells[5][x]=n;
          Form1->StringGrid1->Cells[4][x]=add;
          Form1->StringGrid1->Cells[3][x]="常数数字";
          Form1->StringGrid1->Cells[2][x]="c";
          Form1->StringGrid1->Cells[1][x]='1';
          Form1->StringGrid1->Cells[0][x]=x;
          if(ssj==0)
          fprintf(fp2,"%10d,         1,         c,    常数数字,%10s,%10d\n",x,add,n);
          x++;
         }
          j=0;
         if(a[0]!='\0')           //同CASE1
         {
          if(check(a)==1)
	  {
	   j=0;
	  }
	  else
	  {
	   j=0;
	  }
         }
	 switch(ch)               //将CH为字符的情况细分
               {
	          case'<':        
			  if(s[p+1]=='='||s[p+1]=='<')        //若“<“符号后为“=”或“〈”,调用双字符符号函数处理
			  {
                                   o[0]=ch;o[1]=s[p+1];o[2]='\0';   //暂存欲匹配的双字符符号
                                   fuyu(o);                         //调用FUYU()函数处理
                                   p=p+1;
				   return p;
			  }
                          goto l;
		  case'>':
			  {
				  if(s[p+1]=='='||s[p+1]=='>')   //双字符符号处理
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
                                   goto l;
			  }
		  case'=':
			  {
				  if(s[p+1]=='=')              //双字符符号处理
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
                                   goto l;
			  }
		  case'!':
			  {
				  if(s[p+1]=='=')            //双字符符号处理
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
                                   goto l;
			  }
                 case'%':                              //双字符符号处理
			  {
				  if(s[p+1]=='='||s[p+1]=='d'||s[p+1]=='f'||s[p+1]=='s'||s[p+1]=='c')
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
                                  
			  }
                 case'&':                         //双字符符号处理
			  {
				  if(s[p+1]=='&'||s[p+1]=='=')
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
			  }
                 case'|':                      //双字符符号处理
			  {
				  if(s[p+1]=='|'||s[p+1]=='=')
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
			  }
                 case'+':                    //双字符符号处理
			  {
				  if(s[p+1]=='+'||s[p+1]=='=')
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
			  }
                 case'-':                  //双字符符号处理
			  {
				  if(s[p+1]=='-'||s[p+1]=='=')
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
			  }
    	         case'/':                     //考虑“/”符号是否为说明部分标记           
			  {
				  if(s[p+1]=='*')             //当“/”符号后是“*”时,找是否有“*/”与之匹配
				  {
					  p++;
					  while(s[p]!='\0')
					  {
					  p=p+1;
					  if(s[p]=='*'&&s[p+1]=='/')
					  {
						  p++;
						  return p;
						  break;
					  }
					  }
                                          if(s[p]=='\0')     //若无,则出错处理
                                          {
                                           ShowMessage("无 \"*/\" 字符串与 \"/*\" 字符串匹配");
                                           Form1->StringGrid1->Cells[5][x]="error";
                                           Form1->StringGrid1->Cells[4][x]="error";
                                           Form1->StringGrid1->Cells[3][x]="error";
                                           Form1->StringGrid1->Cells[2][x]="error";
                                           Form1->StringGrid1->Cells[1][x]="error";
                                           Form1->StringGrid1->Cells[0][x]=x;
                                           if(ssj==0)
                                           fprintf(fp2,"%10d,     error,     error,       error,     error,     error\n",x);
                                           x++;
                                           return p;
                                          }
			          break;
				  }
                                  if(s[p+1]=='/')       //当“/”符号后为“/”时,在换行之前的字符串作为说明部分
                                  {
                                   p++;
                                   while(s[p]!='\n')
                                   {
                                    if(s[p]=='\0')
                                    {
                                     Form1->StringGrid1->Cells[5][x]="error";
                                     Form1->StringGrid1->Cells[4][x]="0000";
                                     Form1->StringGrid1->Cells[3][x]="标识符";
                                     Form1->StringGrid1->Cells[2][x]="";
                                     Form1->StringGrid1->Cells[1][x]='0';
                                     Form1->StringGrid1->Cells[0][x]=x;
                                    }
                                    p++;
                                    if(s[p]=='\n')
                                    return p;
                                   }
                                  }
			  }	  
		  }
          if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')             //双字符符号处理
          {
           if(s[p+1]=='=')
				  {
                                   o[0]=ch;o[1]=s[p+1];
                                   fuyu(o);
				   p=p+1;
				   return p;
				  }
          }
          if(ch=='(')
          {
                                          if(find1(p)==0)          //检验"("是否匹配
                                          {
                                           Form1->Edit6->Text=ch;
                                           Form1->StringGrid1->Cells[5][x]="( ";
                                           Form1->StringGrid1->Cells[4][x]="error";
                                           Form1->StringGrid1->Cells[3][x]="字符串匹配";
                                           Form1->StringGrid1->Cells[2][x]="符串与 \"(\"";
                                           Form1->StringGrid1->Cells[1][x]="无 \")\" 字";
                                           Form1->StringGrid1->Cells[0][x]=x;
                                           Form1->Edit6->Text=ch;
                                           if(ssj==0)
                                           fprintf(fp2,"%10d,     error,     error,       error,     error,%10c\n",x,ch);
                                           x++;
                                           return p;
                                          }
                                          else
                                          {
                                           Form1->StringGrid1->Cells[5][x]=ch;
                                           Form1->StringGrid1->Cells[4][x]="10001110";
                                           Form1->StringGrid1->Cells[3][x]="界符";
                                           Form1->StringGrid1->Cells[2][x]="";
                                           Form1->StringGrid1->Cells[1][x]='0';
                                           Form1->StringGrid1->Cells[0][x]=x;
                                           if(ssj==0)
                                           fprintf(fp2,"%10d,         0,          ,        界符,  10001110,%10c\n",x,ch);
                                           x++;
                                           break;
                                          }
          }
          if(ch==')')                   
          {
                                          if(find2(p)==0)          //检验" )"是否匹配
                                          {
                                           Form1->Edit6->Text=ch;
                                           Form1->StringGrid1->Cells[5][x]=")";
                                           Form1->StringGrid1->Cells[4][x]="error";
                                           Form1->StringGrid1->Cells[3][x]="字符串匹配";
                                           Form1->StringGrid1->Cells[2][x]="符串与 \")\"";
                                           Form1->StringGrid1->Cells[1][x]="无 \"(\" 字";
                                           Form1->StringGrid1->Cells[0][x]=x;
                                           if(ssj==0)
                                           fprintf(fp2,"%10d,     error,     error,       error,     error,%10c\n",x,ch);
                                           x++;
                                           return p;
                                          }
                                          else
                                          {
                                           Form1->StringGrid1->Cells[5][x]=ch;
                                           Form1->StringGrid1->Cells[4][x]=10001111;
                                           Form1->StringGrid1->Cells[3][x]="界符";
                                           Form1->StringGrid1->Cells[2][x]="";
                                           Form1->StringGrid1->Cells[1][x]='0';
                                           Form1->StringGrid1->Cells[0][x]=x;
                                           Form1->Edit6->Text=ch;
                                           if(ssj==0)
                                           fprintf(fp2,"%10d,         0,          ,        界符,  10010000,%10c\n",x,ch);
                                           x++;
                                           break;
                                          }
          }
          if(ch=='{')
          {
                                          if(find3(p)==0)            //检验"{ "是否匹配
                                          {
                                           Form1->Edit6->Text=ch;
                                           Form1->StringGrid1->Cells[5][x]="{";
                                           Form1->StringGrid1->Cells[4][x]="error";
                                           Form1->StringGrid1->Cells[3][x]="字符串匹配";
                                           Form1->StringGrid1->Cells[2][x]="符串与 \"{\" ";

⌨️ 快捷键说明

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