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

📄 布尔运算.cpp

📁 八、 设计SAMPLE语言的语法、语义分析器
💻 CPP
字号:
#include"头文件.h"
extern int nowaddress;//表示下一个地址
extern bianYi parse;            //词法分析
extern item sign;            //
extern suanfu_stack STACK;     //布朗运算的栈
extern fouryuan FOMO;         //四元组表
int suanfubiao[numsuanfu+1][numsuanfu+1]={-2, 1,1, 1, 1 ,1 ,1, 1,1, 1,1 ,1,
                                          -1,1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,
										  -1,1,1,-1,-1,-1,-1,1, 1,-1,-1,-1,
										  -1,1,1 ,1,-1,-1,-1,1 ,1,-1,-1,-1,
										  -1,1,1 ,1,-2,-2 ,-1,1,1,-2,-2,-2,
										  -1,1,1, 1,-2 ,-2,-1,1,1,-2,-2,-2,
										  -1,-1,-1,-1,-1,-1,-1,0,1,-1,-1,-1,
										  1,1, 1 ,1, 1, 1, 1, 1, 1,1 , 1,1,
										  -1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1,
										  -1,1,1,1, -2, -2,-1,1 ,1,-2,-2,-2,
										  -1,1,1,1, -2,-2,-1,1, 1,- 2,-2,-2,
										  -1,1,1,1, -2,-2,-1, 1,1 ,-2,-2,-2
};
int findfuhaobiao(int k){
         switch(k){
            case 20:return suanfu_or;
            case 18:return suanfu_not;
            case 1:return suanfu_and;
            case 53:return suanfu_low;
            case 57:return suanfu_above;
            case 39:return suanfu_leftkuohao;
            case 40:return suanfu_rightkuohao;
			case 56:return suanfu_equ;
			case 54:return suanfu_lowequ;
			case 58:return suanfu_aboveequ;
            case 36:
            case 31:
            case 13:
            case 37:return suanfu_bctf;
            case suanfu_end: return suanfu_jing;
            default :return -1;
         }
}
void superpop(int i){
      item temp[5];
      int tempt=STACK.applyfortruelist();
      int tempf=STACK.applyforfalselist();
//      char *op1,*op2;
      switch(i){
	        case suanfu_above:
            case suanfu_low:
   		    case suanfu_equ:
			case suanfu_aboveequ:
			case suanfu_lowequ:{
				for(int j=0;j<3;j++)     
                        STACK.suanfu_pop(temp[j]);
                    STACK.optop++;
                    /////////truelist的2位放codebegin,3位放长度
                    FOMO.load(nowaddress,temp[1],temp[2],temp[0],0);
				//	cout<<nowaddress<<"  if>"<<" "<<op1<<" "<<op2<<endl;
                    STACK.truelist[tempt][1]=nowaddress;
                    STACK.falselist[tempf][1]=nowaddress;
                    STACK.truelist[tempt][2]=1;
                    STACK.falselist[tempf][2]=1;
                    STACK.truelist[tempt][3]=nowaddress;
                    STACK.falselist[tempf][3]=++nowaddress;
                    FOMO.load(nowaddress,0);
				//	cout<<nowaddress<<"  goto"<<"    ,      "<<endl;
                    ++nowaddress;
					item tempitem(-1,tempt);
                    STACK.suanfu_push(tempitem);
					tempitem.x=-2;
					tempitem.y=tempf;
                    STACK.suanfu_push(tempitem);
                    break;                 
            }
					
            case suanfu_not :{
                    for(int j=0;j<3;j++)     
                        STACK.suanfu_pop(temp[j]);
					STACK.optop+=2;
                    STACK.truelist[tempt][0]=_available;
                    STACK.falselist[tempf][0]=_available;
                    temp[0].x=-1;
                    temp[1].x=-2;
                    STACK.truelist[temp[1].y][0]=_unavailable;
                    STACK.falselist[temp[0].y][0]=_unavailable;
					////////////////////真假连互换
					for(int zz=0,ww;zz<3+(STACK.truelist[temp[1].y][2])?(STACK.falselist[temp[0].y][2]):(STACK.truelist[temp[1].y][2])>(STACK.falselist[temp[0].y][2]);zz++){
						ww=STACK.truelist[temp[1].y][zz];
						STACK.truelist[temp[1].y][zz]=STACK.falselist[temp[0].y][zz];
					    STACK.falselist[temp[0].y][zz]=ww;
					}
                    STACK.suanfu_push(temp[1]);
                    STACK.suanfu_push(temp[0]);
                    break;
             }
             case suanfu_and :{
                    for(int j=0;j<5;j++)     
                        STACK.suanfu_pop(temp[j]);  
					STACK.optop+=2;
                    ///前真接后开始
                    FOMO.writeback(STACK.truelist[temp[4].y]+2,STACK.truelist[temp[1].y][1]);
                    ///前假后假合并为总假
                    STACK.falselist_merge(temp[3].y,temp[0].y,tempf);
                    ///将后真做总真
                    STACK.truelist[temp[1].y][0]=_unavailable;
                    STACK.truelist[tempt][0]=_available;
                    STACK.suanfu_push(temp[1]);
					item tempitem(-2,tempf);
                    STACK.suanfu_push(tempitem);
                    break;
              }
				 
             case suanfu_or :{
                    for(int j=0;j<5;j++)     
                        STACK.suanfu_pop(temp[j]);  
					STACK.optop+=2;
                    ///前假接后开始
                    FOMO.writeback(STACK.falselist[temp[3].y]+2,STACK.truelist[temp[1].y][1]);
                    ///前真后真合并为总真
                    STACK.truelist_merge(temp[4].y,temp[1].y,tempt);
                    ///将后假做总 假
                    STACK.falselist[tempf][0]=_available;
                    STACK.falselist[temp[0].y][0]=_unavailable;
					item tempitem(-1,tempt);
                    STACK.suanfu_push(tempitem);
                    STACK.suanfu_push(temp[0]);
                    break;     
              }
				
             case suanfu_bctf :{
                    STACK.truelist[tempt][0]=_available;
                    STACK.falselist[tempf][0]=_available;
                    STACK.optop--;
                    break;
             }   
				 }///////
			 
}       
TFL boolean_E(){
      item signtemp;
      TFL temptfl;
      if(findfuhaobiao(sign.x)==-1)
         error();
	  item tempitem(100,0);
      STACK.suanfu_push(tempitem);
   //   STACK.suanfutop++;
     // STACK.optop++;
     // int i=findfuhaobiao(STACK.sfs[STACK.optop].x);
	  int i;
      int j=findfuhaobiao(sign.x);
      for(;STACK.suanfutop!=3||sign.x!=100;){
         i=findfuhaobiao(STACK.sfs[STACK.optop].x);
/////////////////////////
         if(i==-1||j==-1)error();
         int k=suanfubiao[i][j];

         switch(k){
             case 0:
             case -1:{
                       STACK.suanfu_push(sign);
                       getnextsign();
                       if((j=findfuhaobiao(sign.x))==-1){
                           signtemp=sign;
						   sign.x=100;
						   sign.y=0;
						   j=suanfu_jing;
                        //   sign=new jianzhi(100,0);
                       }
                       
                       break;
              }
             case 1:{
                       superpop(i);
					   break;
                    }
             default:error();
          }//switch
      }
      sign=signtemp;
      item temp2;
      STACK.suanfu_pop(temp2); 
      for(int z=0;z<STACK.falselist[temp2.y][2]+1;z++)
         temptfl.headf[z]=STACK.falselist[temp2.y][z+2]; 
      STACK.suanfu_pop(temp2);
      for(z=0;z<STACK.truelist[temp2.y][2]+1;z++)
         temptfl.headt[z]=STACK.truelist[temp2.y][z+2];
      temptfl.codebegin=STACK.truelist[temp2.y][1];  
      return temptfl;
}
suanfu_stack::suanfu_stack(){
		suanfutop=0;
		optop=0;
        for(int i=0;i<nummaxstack;i++)
			truelist[i][0]=_available;
		for(i=0;i<nummaxstack;i++)
			falselist[i][0]=_available;
     }
void suanfu_stack::suanfu_clear(){
		suanfutop=0;
		optop=0;
        for(int i=0;i<nummaxstack;i++)
			truelist[i][0]=_available;
		for(i=0;i<nummaxstack;i++)
			falselist[i][0]=_available;
	 }
int suanfu_stack::applyfortruelist(){
		for(int i=0;i<nummaxstack;i++)
			if(truelist[i][0]==_available){
				truelist[i][0]=_unavailable;
				 break;
			}
		if(i==nummaxstack)error();
    /*	item tempitem(-1,i);
		suanfu_push(tempitem);
		*/
		return i;
	 }
int suanfu_stack::applyforfalselist(){
       for(int i=0;i<nummaxstack;i++)
         if(falselist[i][0]==_available){
              falselist[i][0]=_unavailable;
              break;
         }
    /*   if(i==nummaxstack)error();
	   item tempitem(-2,i);
       suanfu_push(tempitem);
	   */
       return i;
     /////////////////
     }
void suanfu_stack::suanfu_push(item temp){
           sfs[suanfutop++]=temp;
           if(temp.x!=-1&&temp.x!=-2)
            optop=suanfutop-1;
     }
void suanfu_stack::suanfu_pop(item &temp){
           --suanfutop;
           --optop;
           if(sfs[suanfutop].x==-1){
                truelist[sfs[suanfutop].y][0]=_available;
                temp=sfs[suanfutop];
           }
           else if(sfs[suanfutop].x==-2){
                falselist[sfs[suanfutop].y][0]=_available;
                temp=sfs[suanfutop];
           }
           else {
                 temp=sfs[suanfutop];
                }
           
     }
void suanfu_stack::truelist_merge(int first,int second,int end){
           for(int k=3;k<truelist[first][2]+3;k++)
               truelist[end][k]=truelist[first][k];
		   int t=k;
		   k=0;
           for(;k<truelist[second][2];k++)
               truelist[end][k+t]=truelist[second][k+3];
           truelist[end][2]=k-2;
           truelist[end][1]=truelist[first][1];
     }
void suanfu_stack::falselist_merge(int first,int second,int end){
           for(int k=3;k<falselist[first][2]+3;k++)
               falselist[end][k]=falselist[first][k];
		   int t=k;
		   k=0;
           for(;k<falselist[second][2];k++)
               falselist[end][k+t]=falselist[second][k+3];
           falselist[end][2]=falselist[first][2]+falselist[second][2];
           falselist[end][1]=falselist[first][1];
     }

⌨️ 快捷键说明

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