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

📄 expression.cpp

📁 PL语言到中间代码的编译程序
💻 CPP
字号:

void Factor()
{	//因子
	NametabItem *const_or_var;

	switch(CurSymbol->Type)
	{
	case IDENT:
		const_or_var=FindLowLevelNametabItem(CurSymbol->Value.lpValue,VARIABLE);
		if(const_or_var)	//变量
		{
			if(const_or_var->Type==TARRAY)
			{
				ArrayItemAddress(const_or_var);
				GenerateCode(LODT,0,0);
			}
			else
			{
				if(const_or_var->Normal>=0)
					GenerateCode(LOD,const_or_var->Level,const_or_var->Value);
				else
					GenerateCode(ILOD,const_or_var->Level,const_or_var->Value);  
				GotoNextSymbol();
			}
			break;
		}
		const_or_var=FindLowLevelNametabItem(CurSymbol->Value.lpValue,CONSTANT);
		if(const_or_var)	//常量 
		{
			GenerateCode(LIT,0,const_or_var->Value);
			GotoNextSymbol();
			break;
		}
		Error(24);
		break;
	case NOTSYM:
		GotoNextSymbol();
		Factor();
		GenerateCode(NOTS,0,0);
		break;
	case LPAREN:
		GotoNextSymbol();
		Expression();
		if(CurSymbol->Type!=RPAREN)
			Error(22);
		GotoNextSymbol();
		break;
	case CHARCON:
	case INTCON:
		GenerateCode(LIT,0,CurSymbol->Value.iValue);
		GotoNextSymbol();
		break;
	default:
		Error(24);
	}
}

void Term()
{	//项
	int mul_sym;	//乘法运算符

	Factor();
	while(CurSymbol->Type==TIMES || CurSymbol->Type==DIVSYM || CurSymbol->Type==MODSYM ||CurSymbol->Type==ANDSYM)
	{
		mul_sym=CurSymbol->Type;
		GotoNextSymbol();
		Factor();
		switch(mul_sym)
		{
		case TIMES:
			GenerateCode(MULT,0,0);
			break;
		case DIVSYM:
			GenerateCode(IDIV,0,0);
			break;
		case MODSYM:
			GenerateCode(IMOD,0,0);
			break;
		case ANDSYM:
			GenerateCode(ANDS,0,0);
			break;
		}
	}
}

void SimpleExpression()
{	//简单表达式
	int first_neg=0;	//开始的负号
	int plus_sym;		//加法运算符
	if(CurSymbol->Type==MINUS)
	{
		GotoNextSymbol();
		first_neg=1;
	}
	else if(CurSymbol->Type==PLUS)
		GotoNextSymbol();
	Term();
	if(first_neg)
		GenerateCode(MUS,0,0);
	while(CurSymbol->Type==PLUS || CurSymbol->Type==MINUS || CurSymbol->Type==ORSYM)
	{
		plus_sym=CurSymbol->Type; 
		GotoNextSymbol();
		Term();
		switch(plus_sym)
		{
		case PLUS:
			GenerateCode(ADD,0,0);
			break;
		case MINUS:
			GenerateCode(SUB,0,0);
			break;
		case ORSYM:
			GenerateCode(ORS,0,0);
			break;
		}
	}
}

void Expression()
{	//表达式处理
	int relative_sym;	//关系运算符

	SimpleExpression();
	while(CurSymbol->Type==EQL || CurSymbol->Type==LSS || CurSymbol->Type==GTR || CurSymbol->Type==LEQ || CurSymbol->Type==GEQ || CurSymbol->Type==NEQ)
	{
		relative_sym=CurSymbol->Type;
		GotoNextSymbol();
		SimpleExpression();
		switch(relative_sym)
		{
		case EQL:
			GenerateCode(EQ,0,0);
			break;
		case LSS:
			GenerateCode(LS,0,0);
			break;
		case GTR:
			GenerateCode(GT,0,0);
			break;
		case LEQ:
			GenerateCode(LE,0,0);
			break;
		case GEQ:
			GenerateCode(GE,0,0);
			break;
		case NEQ:
			GenerateCode(NE,0,0);
			break;
		}
	}
}

⌨️ 快捷键说明

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