77范文网 - 专业文章范例文档资料分享平台

北京工业大学 编译原理 实验报告(4)

来源:网络收集 时间:2019-03-27 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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,\); }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库北京工业大学 编译原理 实验报告(4)在线全文阅读。

北京工业大学 编译原理 实验报告(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/545957.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: