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

📄 action.cpp

📁 (1) 给定一段符合Pascal子集语法的语言
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	case 29://S12 → S13 <> S13
		{
			attribute op1, op2;
			_global.popTop(op1);//第一个S13
			_global.popTop(op2);//第二个S13
			int type = op1.att_value;//类型检查
			_code.addCode(_global.sentence_num, "temp"+itos(_global.temp_num), "-", op1.content, op2.content);//合为一个S12
			_global.push("opt","temp"+itos(_global.temp_num), type, 0);//S12进栈
			_global.temp_num++;
			_global.push("true", "", _global.sentence_num-1, 0);//判断地址进栈
			_global.push("ope", "",	notequal, 0);//操作码
			return;
		}
	case 30://S12 → S13
		{
			attribute newID;
			_global.popTop(newID);
			_global.push("opt", newID.content, newID.att_value, 0);//S12进栈
			_global.push("true", "", _global.sentence_num, 0);//判断地址进栈
			_global.push("ope", "", above, 0);//操作码
			return;
		}
	case 31://S13 → S14 OR S14
		{
			return;
		}
	case 32://S13 → S14 AND S14
		{
			return;
		}
	case 33://S13 → S14
		{
			attribute newID;
			_global.popTop(newID);
			_global.push("opt", newID.content, newID.att_value, 0);
			return;
		}
	case 34://S14 → S15 + S15
		{
			attribute op1, op2;
			_global.popTop(op1);
			_global.popTop(op2);
			int type = op1.att_value;//类型检查
			_code.addCode(_global.sentence_num, "temp"+itos(_global.temp_num), "+", op2.content, op1.content);//合为一个S12
			_global.push("opt","temp"+itos(_global.temp_num), type, 0);//S12进栈
			_global.temp_num++;
			return;
		}
	case 35://S14 → S15 - S15
		{
			return;
		}
	case 36://S14 → S15
		{
			attribute newID;
			_global.popTop(newID);
			_global.push("opt", newID.content, newID.att_value, 0);
			return;
		}
	case 37://S15 → S16 * S16	
		{
			return;
		}
	case 38://S15 → S16 / S16
		{
			return;
		}
	case 39://S15 → S16
		{
			attribute newID;
			_global.popTop(newID);
			_global.push("opt", newID.content, newID.att_value, 0);
			return;
		}
	case 40://S16 → NOT S17
		{
			return;
		}
	case 41://S16 → S17
		{
			attribute newID;
			_global.popTop(newID);
			_global.push("opt", newID.content, newID.att_value, 0);
			return;
		}
	case 42://S16 → ( S12 )
		{
			return;
		}
	case 43://S17 → $ID
		/* 引用一个变量 */
		{
			attribute newID;
			_global.popWaitingTop(newID);
			_global.push("opt", newID.content, newID.att_value, 0);
			return;
		}
	case 44://S17 → $INT
		{
			attribute newNum;
			_global.popWaitingTop(newNum);
			_global.push("opt", newNum.content, newNum.att_value, 0);
			return;
		}
	case 45://S17 → $REAL
		{
			attribute newNum;
			_global.popWaitingTop(newNum);
			_global.push("opt", newNum.content, newNum.att_value, 0);
			return;
		}
	case 46://S11 → IF S12 THEN S11
		{
			attribute next;
			_global.popTop(next);		//S11之后的语句
			attribute ope;
			_global.popTop(ope);		//操作码
			attribute dst;
			_global.popTop(dst);		//S12语句的位置
			attribute judge_item;		
			_global.popTop(judge_item);	//S12的值
			_code.pushForward(dst.att_value, 2);		//添加S12语句
			if( ope.att_value == 1)//judge_item>0
				_code.addCode(dst.att_value+1, itos(dst.att_value+3), "j>", judge_item.content, "-");
			else if( ope.att_value == 2 )//judge_item >= 0
				_code.addCode(dst.att_value+1, itos(dst.att_value+3), "j>=", judge_item.content, "-");
			_code.addCode(dst.att_value+2, itos(next.att_value+2), "j", "-", "-");

			_global.push("next", "", _global.sentence_num, 0);	//记录语句结束地址
			return;
		}
	case 47://S11 → IF S12 THEN S11 ELSE S11
		{
			attribute next1, next2;
			_global.popTop(next2);						//ELSE后面的S11
			_global.popTop(next1);						//THEN后面的S11
			attribute ope;
			_global.popTop(ope);						//操作码
			attribute dst;
			_global.popTop(dst);						//S12语句的位置
			attribute judge_item;		
			_global.popTop(judge_item);					//S12的值

			int trueBegin, falseBegin, end_position;
			trueBegin = dst.att_value + 1;
			falseBegin = next1.att_value;
			end_position = next2.att_value;

			_code.pushForward(dst.att_value, 2);		//添加S12语句
			trueBegin += 2;
			falseBegin += 2;
			end_position += 2;
			if( ope.att_value == 1)						//judge_item>0
				_code.addCode(dst.att_value+1, itos(trueBegin), "j>", judge_item.content, "-");
			else if( ope.att_value == 2 )				//judge_item >= 0
				_code.addCode(dst.att_value+1, itos(trueBegin), "j>=", judge_item.content, "-");
			_code.addCode(dst.att_value+2, itos(falseBegin), "j", "-", "-");

			_code.pushForward(falseBegin-1, 1);		//添加true分支执行后的跳转
			falseBegin += 1;
			end_position += 1;
			_code.addCode(falseBegin - 1, itos(end_position), "j", "-", "-");

			_global.push("next", "", _global.sentence_num, 0);	//记录语句结束地址
			return;
		}
	case 48://S11 → WHILE S12 DO S11
		{
			attribute next;
			_global.popTop(next);		//S11之后的语句
			attribute ope;
			_global.popTop(ope);		//操作码
			attribute dst;
			_global.popTop(dst);		//S12语句的位置
			attribute judge_item;		
			_global.popTop(judge_item);	//S12的值
			attribute begin;
			_global.popTop(begin);

			int loop_begin_position, exe_begin, end_position, loop_judge_begin;
			loop_judge_begin = dst.att_value + 1;
			loop_begin_position = begin.att_value;
			exe_begin = dst.att_value + 1;
			end_position = next.att_value;

			_code.pushForward(exe_begin-1, 2);	//插入while跳转
			exe_begin += 2;
			end_position += 2;
			if( ope.att_value == 1)						//judge_item>0
				_code.addCode(loop_judge_begin, itos(exe_begin), "j>", judge_item.content, "-");
			else if( ope.att_value == 2 )				//judge_item >= 0
				_code.addCode(loop_judge_begin, itos(exe_begin), "j>=", judge_item.content, "-");
			_code.addCode(loop_judge_begin+1, itos(end_position), "j", "-", "-");

			_code.pushForward(end_position-1, 1);	//插入while结束跳转
			end_position += 1;
			_code.addCode(end_position-1, itos(loop_begin_position), "j", "-", "-");

			_global.push("next", "", _global.sentence_num, 0);	//记录语句结束地址
			return;
		}
	case 49://S11 → REPEAT S2 UNTIL S12 ;
		{
			return;
		}
	case 50://S11 → FOR $ID := S12 TO S12 DO S11
		{
			attribute next;
			_global.popTop(next);		//S11之后的语句
			attribute ope;
			_global.popTop(ope);		//操作码
			attribute dst;
			_global.popTop(dst);		//S12语句的位置
			attribute judge_item;		
			_global.popTop(judge_item);	//S12的值
			attribute idName;
			_global.popWaitingTop(idName);
			attribute dst1, judge_item1, ope1;
			_global.popTop(ope1);
			_global.popTop(dst1);		//S12的首地址
			_global.popTop(judge_item1);
			attribute begin;
			_global.popTop(begin);		//for 后面的位置
			virable_item *tempVirable = _global.getVirable(idName.content);
			
			int loop_judge_begin, end_position, exe_begin;
			loop_judge_begin = exe_begin = dst.att_value + 1;
			end_position = _global.sentence_num;
			_code.pushForward(begin.att_value-1, 1);	//插入开始赋值
			loop_judge_begin++;
			end_position++;
			exe_begin++;
			_code.addCode(begin.att_value, judge_item1.content, ":=", tempVirable->name, "-");

			_code.pushForward(loop_judge_begin-1, 2);	//插入判断跳转
			end_position += 2;
			exe_begin += 2;
			if( ope.att_value == 1)						//judge_item>0
				_code.addCode(loop_judge_begin, itos(exe_begin), "j>", judge_item.content, "-");
			else if( ope.att_value == 2 )				//judge_item >= 0
				_code.addCode(loop_judge_begin, itos(exe_begin), "j>=", judge_item.content, "-");
			_code.addCode(loop_judge_begin+1, itos(end_position), "j", "-", "-");

			_code.pushForward(end_position-1, 1);	//插入重复跳转
			end_position++;
			_code.addCode(end_position-1, itos(loop_judge_begin), "j", "-", "-");

			_global.push("next", "", _global.sentence_num, 0);	//记录语句结束地址
			return;
		}
	case 51://S11 → BEGIN S2 END	
		{
			return;
		}
	case 52://S11 → S7 FUNCTION $ID ( S5 ) ;
		{
			return;
		}
	case 53://S7 → S4 :=
		{
			return;
		}
	case 54://S7 → e
		{
			return;
		}
	case 55://S5 → S12 , S5
		{
			return;
		}
	case 56://S5 → e
		{
			return;
		}
	case 57://S20 → e
		/* 消去函数名变量 */
		{
			_global.clearAtt();
			_global.clearWait();
			_global.global_type = definition;
			return ;
		}
	case 58://S17 → S8
		{
			attribute newNum;
			_global.popWaitingTop(newNum);
			_global.push("opt", newNum.content, newNum.att_value, 0);
			return;
		}
	case 59://S19 → e
		{
			_global.push("begin", "", _global.sentence_num, 0);
			return;
		}
	default:return;
	}
}

⌨️ 快捷键说明

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