📄 action.cpp
字号:
#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.
{
return;
}
case 1://S1 → S18 S3
/* 定义部分结束,语句部分开始 */
{
_global.clear();
_global.sentence_num = 100;
_global.temp_num = 0;
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 ]
{
return;
}
case 15://S4 → $ID
/* 定义一个变量 */
{
virable_item *tempVirable = new virable_item();
attribute v, type;
_global.popTop(v);
_global.getTop(type);
if( v.att_name.compare("newID") == 0 )
{
tempVirable->name = v.content;
tempVirable->type = type.att_value;
if( tempVirable->type == real)
{
_global.offset += 8;
tempVirable->address = _global.offset;
}
else
{
_global.offset += 4;
tempVirable->address = _global.offset;
}
_global.v_table.push_back(*tempVirable);
}
return;
}
case 16://S10 → $INT
{
return;
}
case 17://S10 → $INT , S10
{
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
{
return;
}
case 23://S11 → S4 := S12 ;
{
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, itos(0), "-", 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, 0);//判断地址进栈
_global.sentence_num++;
return;
}
case 25://S12 → S13 <= S13
{
return;
}
case 26://S12 → S13 > S13
{
return;
}
case 27://S12 → S13 >= S13
{
return;
}
case 28://S12 → S13 = S13
{
return;
}
case 29://S12 → S13 <> S13
{
return;
}
case 30://S12 → S13
{
return;
}
case 31://S13 → S14 OR S14
{
return;
}
case 32://S13 → S14 AND S14
{
return;
}
case 33://S13 → S14
{
return;
}
case 34://S14 → S15 + S15
{
return;
}
case 35://S14 → S15 - S15
{
return;
}
case 36://S14 → S15
{
return;
}
case 37://S15 → S16 * S16
{
return;
}
case 38://S15 → S16 / S16
{
return;
}
case 39://S15 → S16
{
return;
}
case 40://S16 → NOT S17
{
return;
}
case 41://S16 → S17
{
return;
}
case 42://S16 → ( S12 )
{
return;
}
case 43://S17 → $ID
/* 引用一个变量 */
{
return;
}
case 44://S17 → $INT
{
return;
}
case 45://S17 → $REAL
{
return;
}
case 46://S11 → IF S12 THEN S11
{
attribute dst;
_global.popTop(dst);//S11语句的位置
attribute skip;
_global.popTop(skip);//S12语句的位置
attribute checkID;
_global.popTop(checkID);//S12的id
_code.addCode(skip.att_value, itos(dst.att_value), "jnz", checkID.content, "-");
_code.addCode(skip.att_value+1, itos(_global.sentence_num), "j", "-", "-");
return;
}
case 47://S11 → IF S12 THEN S11 ELSE S11
{
return;
}
case 48://S11 → WHILE S12 DO S11
{
return;
}
case 49://S11 → REPEAT S2 UNTIL S12 ;
{
return;
}
case 50://S11 → FOR $ID := S12 TO S12 DO S11
{
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.clear();
return ;
}
default:return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -