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

📄 bool.cpp

📁 一个简单的语法分析器,实现对布尔表达式,算术表达式的分析,以及if语句,FOR语句,WHILE语句,DO_WHILE语句的分析
💻 CPP
字号:
//布尔表达式的文法
//B--->B or L|L
//L--->L and M | M 
//M --->not M | k
//K--->(B) | i | true | false
//i ---> iSi
//S ---> <> | < | > | <= | >= | =
//改为无左递归的文法为:
//B  ---> LB1 | L
//B1 --->or L B1 |e
//L ---> M L1 | M
//L1 ---> and M L1 | e
//M ---> not M | K
//K--->(B) | i | true | false
//i ---> iSi
//S ---> <> | < | > | <= | >= | =
//把布尔表达式的各个元素入栈,以待分析
#include "Parser.h"
void Bool_Init()
{
	pos = 0;
	expr_length = 0;
	for(int i = 0; i  < MAX_EXPR_SIZE; i++)
	{
		expr[i].addr = 0;
		expr[i].code = 0;
	}
	GetNextSymble();
	for(;;)
	{
		switch(curr_code)
		{
		case 1:              //and
		case 15:             //not
		case 17:             //or
		case 11:             //false
		case 23:             //true
		case 27:             //symbol
		case 28:             //integer
		case 29:             //real
		case 32:              //(
		case 33:               //)
		case 43:             //<
		case 44:             //<=
		case 45:             // <>
		case 46:             //=
		case 47:             //>
		case 48:             //>=
			expr[expr_length].addr = curr_addr;
			expr[expr_length].code = curr_code;
			expr_length ++;
			GetNextSymble();
			break;
		default:
			return;
		}
	}

}
//布尔表达式分析开始
int Bool_Analysis()
{
	int rtn = 0;
	Bool_Init();

	for(int i = 0; i < expr_length; i++)
	{
		cout << expr[i].code << endl;
		cout << expr[i].addr << endl;
	}
	rtn = B_OR();
	//cout << "pos:: " << pos << endl;
	if(pos < expr_length)
		cout << "error" << endl;
	return rtn;
}

//分析B-->LB1|L
int B_OR()
{
	cout << "B_OR()" << endl;
	return B1_OR(L_AND());
}

//分析B1-->or L B1| e
int B1_OR(int temp)
{
	cout << "B1_OR()" << endl;
	int rtn = temp;
	if(expr[pos].code == 17) // or
	{
		pos ++;
		int temp_addr = L_AND();
		int tt = New_Temp();
		EquPush(2, temp, temp_addr, tt);
		rtn = B1_OR(tt);
	}
	return rtn;
}

//分析L ---> M L1 | M
int L_AND()
{
	cout << "L_AND()" << endl;
	return L1_AND(M_NOT());
}
//分析L1 ---> and M L1 | e
int L1_AND(int temp)
{
	cout << "L1_AND()" << endl;
	int rtn = temp;
	if(expr[pos].code == 1)           //and
	{
		pos++;
		int temp_addr = M_NOT();
		int tt = New_Temp();
		EquPush(4, temp_addr, temp, tt);
		rtn = L1_AND(tt);
	}
	return rtn;
}
//分析M ---> not M | K

int M_NOT()
{
	cout << "M_NOT()" << endl;
	int rtn = 0;
	if(expr[pos].code == 15)     //not
	{
		pos++;
		int temp_addr = M_NOT();
		int tt = New_Temp();
		EquPush(3, temp_addr, ONE, tt);
		rtn = tt;
	}
	else
		rtn = K_END();
	return rtn;
}

//分析K--->(B) | i | true | false
int K_END()
{
	cout << "K_END()" << endl;
	cout << "pos: " << pos << endl;
	cout << expr[pos].code << endl;
	int temp_addr = 0;;
	int tt = 0;
	int rtn = 0;
	switch(expr[pos].code)
	{
	case 32:
		pos++;
		rtn = B_OR();
		if(expr[pos].code != 33)    //)
			cout << "Error" <<endl;
		else
			pos ++;
		break;
	case 28:
	case 27:
	case 29:
		temp_addr = K_CMP();
		tt = New_Temp();
		if(temp_addr == 0)
		{
			//cout << "here" << endl;
			EquPush(1, expr[pos].addr, 0, tt);
			rtn = tt;
		}
		else
			rtn = temp_addr;
		break;
	case 11:
		tt = New_Temp();
		EquPush(1, ZERO, 0, tt);
		rtn = tt;
		pos++;
		break;
	case 23:
		tt = New_Temp();
		EquPush(1, ONE, 0, tt);
		rtn = tt;
		pos++;
		break;
	default:
		break;
	}
	return rtn;
}
//分析K--->iSi
//S ---> <> | < | > | <= | >= | =
int K_CMP()
{
	cout << "K_CMP()" << endl;
	int rtn = 0;
	if(expr[pos+1].code > 42 && expr[pos+1].code < 49)
	{
		int t1 = New_Temp();
		int t2 = New_Temp();
		EquPush(3, expr[pos].addr, expr[pos+2].addr, t1);
		switch(expr[pos+1].code)
		{
		case 43:
			EquPush(7, t1, ZERO, lineOfEqu + 2);
			break;
		case 44:
			EquPush(10, t1, ZERO, lineOfEqu + 2);
			break;
		case 45:
			EquPush(12, t1, ZERO, lineOfEqu + 2);
			break;
		case 46:
			EquPush(9, t1, ZERO, lineOfEqu + 2);
			break;
		case 47:
			EquPush(8, t1, ZERO, lineOfEqu + 2);
			break;
		case 48:
			EquPush(11, t1, ZERO, lineOfEqu + 2);
			break;
		default:
			break;
		}
		EquPush(1, ONE, 0, t2);
		EquPush(6, 0, 0, lineOfEqu + 3);
		EquPush(1, ZERO, 0, t2);
		rtn = t2;
		pos ++;
		pos++;
		pos++;
	}
	return rtn;
}

⌨️ 快捷键说明

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