T –> F1 (* F2)* T.place = newtemp; (T.code = F1.code || F2.code || gen(T.place ‘:=’ F1.place ‘*’ F2.place); F1.place = T.place; F1.code = T.code;)+ T.place = newtemp; (T.code = F1.code || F2.code || gen(T.place ‘:=’ F1.place ‘/’ F2.place); F1.place = T.place; F1.code = T.code;)+ F.place = E.place; F.code = E.code F.place = id.name; F.code = ‘’ F.place = int8.value; F.code = ‘’ F.place = int10.value; F.code = ‘’ F.place = int16.value; F.code = ‘’ T –> F1 (/ F2)* F –> ( E ) F –> id F –> int8 F –> int10 F –> int16 三地址代码生成器的数据结构
typedef struct { /*S的属性定义*/ char code[CODESIZE]; int begin; int next; }AttrS;
typedef struct { /*E的属性定义*/ char code[CODESIZE];//CodeSize = 500 char place[BUFSIZE];//BufSize = 200 }AttrE;
typedef struct { /*C的属性定义*/ char code[CODESIZE]; int c_false;//用来标记入口 int c_true;//用来标记入口 }AttrC;
typedef struct { /*T的属性定义*/ char code[CODESIZE];//CodeSize = 500 char place[BUFSIZE];//BufSize = 200 }AttrT;
typedef struct { /*F的属性定义*/ char code[CODESIZE];//CodeSize = 500 char place[BUFSIZE];//BufSize = 200 }AttrF;
typedef struct { /*IDN的属性定义*/ char idname[BUFSIZE]; int entry; }AttrIDN;
三地址生成器算法:
int ProcedureS(ifstream& from_file, AttrS &s) {
AttrC c;//C的属性 AttrS s1;//s1的属性 AttrE e;//e的属性
char temp_idn_name[50];//用来暂存当下一个是IDN时,s->id:=E 的 id的name
Token* token = TokenScan(from_file);
//////////////////////////////////////////s-> if C then S1///////////////////////////////////////////// if (token->type == IF) {
c.c_true = NewLabel();//c.c_true出口有了新标签 s1.begin = c.c_true;// C真 则往 S1走
s1.next = c.c_false = s.next; // c假 则 走s的下一步 为L0标签,在前面预置了
ProcedureC(from_file, c);
token = TokenScan(from_file); if (token->type == THEN) { ProcedureS(from_file, s1);
sprintf_s(s.code, \, c.code, c.c_true, s1.code);//将中间代码输出到s.code中 } else { exit(-1); }
//////////////////////////////////////////s-> while C do S1///////////////////////////////////////////// } else if (token->type == WHILE) { s1.next = s.begin = NewLabel();
c.c_true = s1.begin = NewLabel();//C真 则往 S1走
c.c_false = s.next;// c假 则 走s的下一步 为L0标签,在前面预置了
ProcedureC(from_file, c);
token = TokenScan(from_file); if (token->type == DO) {
ProcedureS(from_file, s1);
sprintf_s(s.code,\,s.begin,c.code,c.c_true,s1.code,s.begin); } else { exit(-1); }
//////////////////////////////////////////s-> id := E///////////////////////////////////////////// } else if (token->type == IDN) {
strcpy_s(temp_idn_name, token->name.c_str()); token = TokenScan(from_file); if (token->type == EQU) { ProcedureE(from_file, e);
sprintf_s(s.code,\,e.code, temp_idn_name, e.place);
} else { exit(-1); } }
return 0; }
int ProcedureC(ifstream& from_file, AttrC &c) {
AttrE e1;//e1的属性 AttrE e2;//e2的属性 Token* token;
ProcedureE(from_file, e1); token = TokenScan(from_file); if (token->type == MORE) { ProcedureE(from_file, e2);
sprintf_s(c.code, \,e1.code,e2.code,e1.place,e2.place,c.c_true,c.c_false); } else if (token->type == LESS) { ProcedureE(from_file, e2);
sprintf_s(c.code, \,e1.code,e2.code,e1.place,e2.place,c.c_true,c.c_false); } else { exit(-1); }
return 0; }
int ProcedureE(ifstream& from_file, AttrE &e) {
AttrT t1; AttrT t2;
Token* token;
ProcedureT(from_file, t1); while (true) {
token = TokenScan(from_file); if (token->type == ADD) { ProcedureT(from_file, t2); strcpy_s(e.place,NewTemp());
sprintf_s(e.code,\,t1.code,t2.code, e.place,t1.place,t2.place);
//这里是关键,用t1.code和t1.place临时记录了上一次while的e.code 和e.place,随着while的不断加深,t1的代码会不断长长 strcpy_s(t1.code,e.code); strcpy_s(t1.place,e.place); } else if (token->type == MINUS) { ProcedureT(from_file, t2); strcpy_s(e.place,NewTemp());
sprintf_s(e.code,\,t1.code,t2.code, e.place,t1.place,t2.place);
strcpy_s(t1.code,e.code); strcpy_s(t1.place,e.place); } else {
for (int i = 0; i < (int)token->name.length(); i++) { from_file.unget();//回退 }
//////////////////////////////////////////E->T///////////////////////////////////////////// strcpy_s(e.place,t1.place);
sprintf_s(e.code,\,t1.code); break; } }
return 0; }
int ProcedureT(ifstream& from_file, AttrT &t) { AttrF f1; AttrF f2;
Token* token;
ProcedureF(from_file ,f1); while (true) {
token = TokenScan(from_file); if (token->type == MUL) {
ProcedureF(from_file, f2); strcpy_s(t.place,NewTemp());
sprintf_s(t.code,\,f1.code,f2.code,t.place,f1.place,f2.place);
strcpy_s(f1.code,t.code);
strcpy_s(f1.place,t.place); } else if (token->type == DIC) { ProcedureF(from_file, f2); strcpy_s(t.place,NewTemp());
sprintf_s(t.code,\,f1.code,f2.code,t.place,f1.place,f2.place);
strcpy_s(f1.code,t.code);
strcpy_s(f1.place,t.place); } else {
for (int i = 0; i < (int)token->name.length(); i++) { from_file.unget();//回退 }
strcpy_s(t.place,f1.place);
sprintf_s(t.code,\,f1.code); break; } }
return 0; }
int ProcedureF(ifstream& from_file, AttrF &f) { AttrE e;
Token* token;
char temp_value[50];
token = TokenScan(from_file); if (token->type == LBRAC) { ProcedureE(from_file, e);
strcpy_s(f.place,e.place);//f.place = e.place sprintf_s(f.code,\,e.code);
token = TokenScan(from_file);//匹配右括号 }
if (token->type == IDN) {
strcpy_s(f.place,token->name.c_str()); sprintf_s(f.code,\); }
if (token->type == INT8) {
sprintf_s(temp_value, \, ValueOfINT8(token->value)); strcpy_s(f.place, temp_value); sprintf_s(f.code,\); }