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

📄 parsell1.cpp

📁 SNL语言的编译器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
   /*当前指针指向赋值左部*/
   currentP=currentP->child[0];

   TreeNode *t=newExpNode(OpK);
   t->attr.ExpAttr.op = END ;
   PushOp(t);  //操作符栈的栈底存入一个特殊的操作符作为标志
}
void CparseLL1::process70()
{
   Push(2,FI);
   Push(1,StmList);
   Push(2,ELSE);
   Push(1,StmList);
   Push(2,THEN);
   Push(1,RelExp);
   Push(2,IF);
   
   PushPA(&(currentP->child[2]));
   PushPA(&(currentP->child[1]));
   PushPA(&(currentP->child[0]));

}
 
void CparseLL1::process71()
{
   Push(2,ENDWH);
   Push(1,StmList);
   Push(2,DO);
   Push(1,RelExp);
   Push(2,WHILE);

   PushPA(&(currentP->child[1]));
   PushPA(&(currentP->child[0]));

}

void CparseLL1::process72()
{
   Push(2,RPAREN);
   Push(1,InVar);
   Push(2,LPAREN);
   Push(2,READ);
}
void CparseLL1::process73()
{
   Push(2,ID);

   strcpy( currentP->name[0], currentToken.Sem);	
   currentP->idnum++;
}
void CparseLL1::process74()
{
   Push(2,RPAREN);
   Push(1,Exp);
   Push(2,LPAREN);
   Push(2,WRITE);
   
   PushPA(&(currentP->child[0]));

   TreeNode *t=newExpNode(OpK);
   t->attr.ExpAttr.op = END ;
   PushOp(t);  //操作符栈的栈底存入一个特殊的操作符作为标志
}
void CparseLL1::process75()
{
   Push(2,RETURN);
}

void CparseLL1::process76()
{ 
   Push(2,RPAREN);
   Push(1,ActParamList);
   Push(2,LPAREN);	

   PushPA(&(currentP->child[1]));
 
}
void CparseLL1::process77()
{
   PopPA();
}
void CparseLL1::process78()
{
   Push(1,ActParamMore);
   Push(1,Exp);

   TreeNode *t=newExpNode(OpK);
   t->attr.ExpAttr.op = END ;
   PushOp(t);  //操作符栈的栈底存入一个特殊的操作符作为标志
}

void CparseLL1::process79()
{
}
void CparseLL1::process80()
{ 
   Push(1,ActParamList);
   Push(2,COMMA);
   
   PushPA(&(currentP->sibling));
}


/*设定操作符的优先级,值越大优先级越高*/
int  CparseLL1::Priosity( LexType  op)
{ int  pri=0;
  switch(op)
  { case END:
			pri = -1; break;//栈底标识,优先级最低
    case LPAREN: 
		    pri = 0;
    case LT:
    case EQ:
		    pri = 1; break;
	case PLUS:
	case MINUS:
		pri = 2; break;
	case TIMES:
	case OVER:
			pri = 3; break;
    default:  {fprintf(listing ,"no such  operator !");
		       fprintf(listing,"%d\n",op);
			   pri= -1;
			  }  
  }
  return  pri;
}  

/*********************表达式部分************************/

void CparseLL1::process81()
{
   Push(1,OtherRelE);
   Push(1,Exp);
   
   TreeNode *t=newExpNode(OpK);
   t->attr.ExpAttr.op = END ;
   PushOp(t);  //操作符栈的栈底存入一个特殊的操作符作为标志	

   getExpResult=FALSE;
}

void CparseLL1::process82()
{   
   Push(1,Exp);
   Push(1,CmpOp);

   TreeNode  *currentP=newExpNode(OpK);
   currentP->attr.ExpAttr.op = currentToken.Lex; 
 
   LexType  sTop=ReadOpStack();
   while ( Priosity(sTop) >= Priosity(currentToken.Lex) )
   /*如果操作符栈顶运算符的优先级高于或等于当前读到的操作符*/
   {   TreeNode *t= PopOp();
	   TreeNode *Rnum= PopNum();
	   TreeNode *Lnum= PopNum();
       t->child[1]=Rnum;
	   t->child[0]=Lnum;
       PushNum(t);

       sTop=ReadOpStack();
   }  
   
   PushOp(currentP);
   /*处理完关系操作符右部的表达式时,要弹语法树栈,故
     设置getExpResult为真*/
   getExpResult	= TRUE;
}

void CparseLL1::process83()
{ 
   Push(1,OtherTerm);
   Push(1,Term);
}

void CparseLL1::process84()
{
  if ((currentToken.Lex==RPAREN)&&(expflag!=0))
     //说明当前右括号是表达式中的一部分
    { while (ReadOpStack()!=LPAREN)
          {TreeNode   *t=PopOp();
	        TreeNode  *Rnum=PopNum();
	        TreeNode  *Lnum=PopNum();

            t->child[1] = Rnum;
			t->child[0] = Lnum;
			PushNum(t);
         }
		PopOp(); //弹出左括号
        expflag--; 
     }
  else
  { if ((getExpResult)||(getExpResult2))
    {  while  (ReadOpStack()!=END)
	  {TreeNode  *t=PopOp();
	   TreeNode  *Rnum=PopNum();
	   TreeNode  *Lnum=PopNum();

       t->child[1] = Rnum;
	   t->child[0] = Lnum;
	   PushNum(t);  
	  }
      PopOp();//弹出栈底标志
	  currentP=PopNum();
    
	  TreeNode  **t=PopPA();
	  (*t)=currentP;
      
	  /*处理完数组变量,标志恢复初始值假,
	    遇到下一个数组下标表达式时,再将其设置为真值*/
	  if (getExpResult2==TRUE)
	      getExpResult2 = FALSE;
	}
 }
}

void CparseLL1::process85()
{ 
   Push(1,Exp);
   Push(1,AddOp);
   
   TreeNode  *currentP=newExpNode(OpK);
   currentP->attr.ExpAttr.op = currentToken.Lex; 
   LexType  sTop=ReadOpStack();
   while ( Priosity(sTop) >= Priosity(currentToken.Lex) )
   /*如果操作符栈顶运算符的优先级高于或等于当前读到的操作符*/
   {   TreeNode *t=PopOp();
	   TreeNode *Rnum=PopNum();
	   TreeNode *Lnum=PopNum();
       t->child[1]=Rnum;
	   t->child[0]=Lnum;
       PushNum(t);
       sTop=ReadOpStack();
   }  
   PushOp(currentP);
}

void CparseLL1::process86()
{ 
   Push(1,OtherFactor);
   Push(1,Factor);
}
void CparseLL1::process87()
{
}
void CparseLL1::process88()
{
   Push(1,Term);
   Push(1,MultOp);

   TreeNode  *currentP=newExpNode(OpK);
   currentP->attr.ExpAttr.op = currentToken.Lex; 
   
   LexType  sTop=ReadOpStack();
   while ( Priosity(sTop) >= Priosity(currentToken.Lex) )
   /*如果操作符栈顶运算符的优先级高于或等于当前读到的操作符*/
   {   TreeNode *t=PopOp();
	   TreeNode *Rnum=PopNum();
	   TreeNode *Lnum=PopNum();
       t->child[1]=Rnum;
	   t->child[0]=Lnum;
       PushNum(t);

       sTop=ReadOpStack();
   }  
   PushOp(currentP);
}

void CparseLL1::process89()
{
   Push(2,RPAREN);
   Push(1,Exp);
   Push(2,LPAREN);

   TreeNode *t=newExpNode(OpK);
   t->attr.ExpAttr.op = currentToken.Lex; /*把左括号也压入栈中*/
   PushOp(t);
   expflag++;
}

void CparseLL1::process90()
{
  Push(2,INTC);
	
  TreeNode *t=newExpNode(ConstK);
  t->attr.ExpAttr.val = atoi(currentToken.Sem);
  /*常数节点入操作数栈*/
  PushNum(t);

}

void CparseLL1::process91()
{
 Push(1,Variable);
}

void CparseLL1::process92()
{
	Push(1,VariMore);
	Push(2,ID);
	
	currentP=newExpNode(VariK);
	strcpy( currentP->name[0] , currentToken.Sem );
	currentP->idnum++;
	/*变量节点入操作数栈*/
	PushNum(currentP);

}

void CparseLL1::process93()
{
 /*标识符变量*/
 currentP->attr.ExpAttr.varkind=IdV;
}

void CparseLL1::process94()
{  
	Push(2, RMIDPAREN);
	Push(1, Exp);
	Push(2, LMIDPAREN);
    /*数组成员变量*/
    currentP->attr.ExpAttr.varkind=ArrayMembV;
	PushPA(&currentP->child[0]);

	/*要进入表达式处理,初始化操作符栈*/
	//操作符栈的栈底存入一个特殊的操作符作为标志
    TreeNode *t=newExpNode(OpK);
    t->attr.ExpAttr.op = END ;
    PushOp(t);  	

   	/*要进入数组下标表达式处理,在函数CparseLL1::process84处理中,要
	  操作语法树栈,故将标志getExpResult2设置为真值*/
    getExpResult2 = TRUE;


}

void CparseLL1::process95()
{
	Push(1, FieldVar);
	Push(2, DOT);
    /*域成员变量*/
	currentP->attr.ExpAttr.varkind=FieldMembV;
	PushPA(&currentP->child[0]);
}

void CparseLL1::process96()
{
	Push(1,FieldVarMore);
	Push(2,ID);
    
	/*纪录域的成员*/
	currentP=newExpNode(VariK);
	strcpy( currentP->name[0] , currentToken.Sem );
	currentP->idnum++;

	TreeNode **t=PopPA( );
	(*t)=currentP;


}

void CparseLL1::process97()
{
  /*域成员是标识符变量*/
  currentP->attr.ExpAttr.varkind=IdV;
}

void CparseLL1::process98()
{
	Push(2,RMIDPAREN);
	Push(1,Exp);
	Push(2,LMIDPAREN);
	/*域成员是数组变量*/
	currentP->attr.ExpAttr.varkind=ArrayMembV;
	/*指向数组成员表达式*/
	PushPA(&currentP->child[0]);

    //操作符栈的栈底存入一个特殊的操作符作为标志
    TreeNode *t=newExpNode(OpK);
    t->attr.ExpAttr.op = END ;
    PushOp(t);  	

	/*要进入数组下标表达式处理,在函数CparseLL1::process84处理中,要
	  操作语法树栈,故将标志getExpResult2设置为真值*/
    getExpResult2 = TRUE;
}
void CparseLL1::process99()
{ 
    Push(2,LT);
}

void CparseLL1::process100()
{  
	Push(2,EQ);
}

void CparseLL1::process101()
{  
	Push(2,PLUS);
}

void CparseLL1::process102()
{ 
	Push(2,MINUS);
}

void CparseLL1::process103()
{
	Push(2,TIMES);
}

void CparseLL1::process104()
{
    Push(2,OVER);
}


/****************************************************/
/* 函数名  predict									*/
/* 功  能  选择产生式函数							*/
/* 说  明  										    */
/****************************************************/
void CparseLL1::predict(int num)
{  switch( num )
    { case 1:     CparseLL1::process1();	break;	  
      case 2:     CparseLL1::process2();	break;
      case 3:     CparseLL1::process3();	break;
      case 4:     CparseLL1::process4();	break;
	  case 5:	  CparseLL1::process5();	break;
      case 6:	  CparseLL1::process6();	break;
      case 7:	  CparseLL1::process7();	break;
      case 8:	  CparseLL1::process8();	break;
	  case 9:	  CparseLL1::process9();	break;
      case 10:	  CparseLL1::process10();	break;
      case 11:	  CparseLL1::process11();	break;
      case 12:	  CparseLL1::process12();	break;
      case 13:	  CparseLL1::process13();	break;
      case 14:	  CparseLL1::process14();	break;
      case 15:	  CparseLL1::process15();	break;
      case 16:	  CparseLL1::process16();	break;
      case 17:	  CparseLL1::process17();	break;
      case 18:	  CparseLL1::process18();	break;
      case 19:	  CparseLL1::process19();	break;
      case 20:	  CparseLL1::process20();	break;
      case 21:	  CparseLL1::process21();	break;
	  case 22:	  CparseLL1::process22();	break;
      case 23:	  CparseLL1::process23();	break;
      case 24:	  CparseLL1::process24();	break;
	  case 25:	  CparseLL1::process25();	break;
      case 26:	  CparseLL1::process26();	break;
      case 27:	  CparseLL1::process27();	break;
      case 28:	  CparseLL1::process28();	break;
      case 29:	  CparseLL1::process29();	break;
      case 30:	  CparseLL1::process30();	break;
	  case 31:	  CparseLL1::process31();	break;
      case 32:	  CparseLL1::process32();	break;
      case 33:	  CparseLL1::process33();	break;
	  case 34:	  CparseLL1::process34();	break;
      case 35:	  CparseLL1::process35();	break;
     
	  case 36:	  CparseLL1::process36();	break;
      case 37:	  CparseLL1::process37();	break;
      case 38:	  CparseLL1::process38();	break;
      case 39:	  CparseLL1::process39();	break;
      case 40:	  CparseLL1::process40();	break;
	  
      case 41:	  CparseLL1::process41();	break;
      case 42:	  CparseLL1::process42();	break;
	  case 43:	  CparseLL1::process43();	break;
      case 44:	  CparseLL1::process44();	break;
      case 45:	  CparseLL1::process45();	break;
	  
	  case 46:    CparseLL1::process46();	break;
      case 47:	  CparseLL1::process47();	break;
      case 48:	  CparseLL1::process48();	break;
      case 49:	  CparseLL1::process49();	break;
      case 50:	  CparseLL1::process50();	break;
  
	  case 51:	  CparseLL1::process51();	break;
      case 52:	  CparseLL1::process52();	break;
      case 53:	  CparseLL1::process53();	break;
      case 54:	  CparseLL1::process54();	break;
      case 55:	  CparseLL1::process55();	break;
      case 56:	  CparseLL1::process56();	break;

	  case 57:	  CparseLL1::process57();   break;
      case 58:	  CparseLL1::process58();	break;
      case 59:	  CparseLL1::process59();	break;
      case 60:	  CparseLL1::process60();	break;
      case 61:	  CparseLL1::process61();	break;
      case 62:	  CparseLL1::process62();	break;
      case 63:	  CparseLL1::process63();	break;
      case 64:	  CparseLL1::process64();	break;
      case 65:	  CparseLL1::process65();	break;
      case 66:	  CparseLL1::process66();	break;
      case

⌨️ 快捷键说明

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