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

📄 action.cpp

📁 (1) 给定一段符合Pascal子集语法的语言
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "StdAfx.h"
#include ".\action.h"

action::action(void)
{
}

action::~action(void)
{
}

string action::itos(int i)
{
	stringstream   s;   
	s   <<   i;   
	return   s.str();   
}
void action::take_action(int creation_num)
{
	cout<<"take action after creation_num : "<<creation_num<<endl;
	switch(creation_num)
	{
	case 0://S0 → PROGRAM $ID ; S1 BEGIN S2 END.
		{
			_global.global_type = finish;
			return;
		}
	case 1://S1 → S18 S3
		/* 定义部分结束,语句部分开始 */	
		{
			_global.clearAtt();
			_global.clearWait();
			_global.sentence_num = 100;
			_global.temp_num = 0;
			_global.global_type = program;
			return;
		}
	case 2://S18 → e
		/* 地址初始化 */
		{
			_global.offset = 0;
			return;
		}
	case 3://S3 → S3 S3
		{
			return;
		}
	case 4://S3 → e
		{
			return;
		}
	case 5://S3 → S6 : S9 ;
		/* 结束一个变量定义,去除变量类型 */
		{
			attribute temp;
			_global.popTop(temp);//移出type属性
			return;
		}
	case 6://S3 → CONST S6 : S9 ;
		{
			return;
		}
	case 7://S6 → INTEGER
		/* 定义整数变量类型 */
		{
			_global.push("type", "", integer, 0);
			return;
		}
	case 8 ://S6 → REAL
		/* 定义实数变量类型 */
		{
			_global.push("type", "", real, 0);
			return;
		}
	case 9://S6 → BOOLEAN
		/* 定义布尔变量类型 */
		{
			_global.push("type", "", boolean, 0);
			return;
		}
	case 10://S8 → TRUE
		{
			return;
		}
	case 11://S8 → FALSE
		{
			return;
		}
	case 12://S9 → S4 , S9
		{
			return;
		}
	case 13://S9 → S4
		{
			return;
		}
	case 14://S4 → ARRAY $ID [ S10 ]
		{
			virable_item *tempVirable = new virable_item();
			attribute op1, op2, type, name;
			_global.getTop(op1);						//先获得维数
			vector<attribute> _vectorType;
			for( int i = 0 ; i < op1.symbol_name ; i++)
			{
				attribute temp;
				_global.popTop(temp);
				_vectorType.push_back(temp);
			}
			_global.popWaitingTop(name);
			if( _global.global_type == definition )		//定义部分
			{
				int size;
				_global.getTop(type);
				if( type.att_value == real )
					size = 8;
				else 
					size = 4;
				tempVirable->name = name.content;
				tempVirable->type = vector_type;
				tempVirable->vector_size = _vectorType.size();
				tempVirable->vectorType = type.att_value;
				for( int i = 0 ; i < _vectorType.size() ; i++)
				{
					tempVirable->_size.push_back(_vectorType[i].att_value);
					size *= _vectorType[i].att_value;
				}
				tempVirable->address = _global.offset;
				_global.offset += size;
				_global.v_table.push_back(*tempVirable);

			}
			else if( _global.global_type == program )	//主程序部分
			{
				tempVirable = _global.getVirable(name.content);
				int size;
				if( tempVirable->vectorType == real )
					size = 8;
				else 
					size = 4;
				int address = 0;
				for( int i = 0 ; i < tempVirable->vector_size ; i++)
				{
					address += _vectorType[i].att_value*tempVirable->_size[i];
				}
				address *= size;
				_code.addCode(_global.sentence_num, "temp"+itos(_global.temp_num), ":=", tempVirable->name + "[" + itos(address) + "]", "-");//合为一个S12
				_global.push("opt", "temp"+itos(_global.temp_num), tempVirable->vectorType, 0);
				_global.temp_num++;
			}
			return;
		}
	case 15://S4 → $ID
		/* 定义一个变量 */
		{
			virable_item *tempVirable = new virable_item();
			attribute v, type;
			_global.popWaitingTop(v);
			if( v.att_name.compare("newID") == 0 )
			{
				_global.getTop(type);
				tempVirable->name = v.content;
				tempVirable->type = type.att_value;
				if( tempVirable->type == real)
				{
					tempVirable->address = _global.offset;
					_global.offset += 8;
				}
				else
				{
					tempVirable->address = _global.offset;
					_global.offset += 4;
				}
				_global.v_table.push_back(*tempVirable);
			}
			else if( v.att_name.compare("checkID") == 0 )
			{
				_global.push("opt", v.content, v.att_value, 0);
			}
			return;
		}
	case 16://S10 → $INT
		{
			attribute op1;
			_global.popWaitingTop(op1);
			_global.push("vector", "", atol(op1.content.c_str()), 1);//1维数组
			return;
		}
	case 17://S10 → $INT , S10
		{
			attribute op1, temp;
			_global.popWaitingTop(op1);
			_global.getTop(temp);
			if( temp.att_name.compare("vector") == 0 )//多维数组
			_global.push("vector", "", atol(op1.content.c_str()), temp.symbol_name+1);//维数加1
			return;
		}
	case 18://S3 → FUNCTION $ID ( S5 ) : S17 ;
		{
			return;
		}
	case 19://S5 → S6 $ID , S5
		{
			return;
		}
	case 20://S5 → e
		{
			return;
		}
	case 21://S2 → S11 S2
		{
			return;
		}
	case 22://S2 → e
		{
			_global.clearAtt();
			_global.clearWait();
			_global.global_type = finish;
			return;
		}
	case 23://S11 → S4 := S12 ;
		{
			attribute op1, op2;
			_global.popTop(op1);
			_global.popTop(op1);		//前面两个没有用
			_global.popTop(op1);
			_global.popTop(op2);
			int type = op1.att_value;//类型检查
			_code.addCode(_global.sentence_num, op2.content, ":=", op1.content, "-");//合为一个S12
			_global.push("next", "" ,_global.sentence_num, 0);
			return;
		}
	case 24://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", "", above, 0);//操作码
			return;
		}
	case 25://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", "", aboveequal, 0);//操作码
			return;
		}
	case 26://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), "-", op2.content, op1.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", "", above, 0);//操作码
			return;
		}
	case 27://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), "-", op2.content, op1.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", "", aboveequal, 0);//操作码
			return;
		}
	case 28://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", "",	equal, 0);//操作码
			return;
		}

⌨️ 快捷键说明

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