编译原理-中间代码优化
break;
case1:
tmp1->bsf=fhead->arg1;
tmp1->var= "";
tmp1->flag = 0;
tmp1->lchild= NULL;
tmp1->rchild= NULL;
tmp1->next = NULL;
tmpResult->bsf=fhead->result;
tmpResult->var=fhead->op;
/* Deceleration of structures */
/********************************************************************/
structFour{
string op;//操作符
string arg1;//第一个操作数
string arg2;//第二个操作数
*/
/*第四个测试用例
input_source.push_back("*,7,4,T1");
input_source.push_back("+,5,T1,T2");
input_source.push_back(" ,T2, ,a");
input_source.push_back("+,a,9,T3");
/* Global variables */
/********************************************************************/
Four *fhead;
DagNode*nhead;
std::list<DagNode*>list_Dag;
vector<string>input_source;
tmp1->lchild= NULL;
tmp1->rchild= NULL;
tmp1->next = tmp2;
tmpResult->bsf=fhead->result;
tmpResult->var=fhead->op;
tmpResult->flag = 0;
tmpResult->lchild= tmp1;
*/
/*第五个测试用例结合实验3的输出结果
input_source.push_back("/,3,2,t1");
input_source.push_back(" ,t1, ,a");
input_source.push_back("+,s,8,t2");
input_source.push_back(" ,t2, ,b");
while(iter3 != iter2)//从链表下一个结点开始扫描
{
nheadUp= (*iter2)->next;
while(nheadUp!= NULL)
{
if(nheadUp->bsf==nheadDown->bsf||nheadUp->var==nheadDown->bsf)
{
nheadUp->var=nheadDown->var;
nheadDown->flag = 1;
nheadUp->lchild=nheadDown->lchild;
nheadUp->rchild=nheadDown->rchild;
}
nheadUp=nheadUp->next;
}
++iter2;
}
nheadDown=nheadDown->next;
}
}
}
string result;//结果
intstylenum;//结点类型
structFour *next;//指向下一条语句的起始位置
structFour *last;//指向上一条语句的起始位置
};
structDagNode{
stringbsf;//标识符
stringvar;//副标识符
intflag;//标记位
#include <string>
#include <vector>
#include <iostream>
#include <stdio.h>
usingnamespace std;
/********************************************************************/
else
//----------3补充语句//第二个操作数为空,并且操作符不空,则为1型四元式
pp->stylenum=1;
else
//----------4补充语句//第二个操作数不为空,则为2型四元式
pp->stylenum=2;
fourTmp= pp;
}
fhead=fourTmp;
}
voidListBuild()//建立链表
}
}
}
voidNodeBuild()//建立DAG结点
{
DagNode*nheadUp= newDagNode;
nheadUp->next = NULL;
nheadUp->lchild= NULL;
nheadUp->rchild= NULL;
DagNode*nheadDown= newDagNode;
input_source.push_back("*,2,T0,T1");
input_source.push_back("+,R,r,T2");
input_source.push_back("+,T1,T2,A");
input_source.push_back(" ,A, ,B");
*/
/*第三个测试用例
tmp1->lchild= NULL;
tmp1->rchild= NULL;
tmp1->next = NULL;
tmp2->lchild= NULL;
tmp2->rchild= NULL;
tmp2->next = NULL;
tmpResult->lcrchild= NULL;
{
for(iter1 =list_Dag.begin(),iter3 = (--list_Dag.end()); iter1 != iter3; --iter3)//从链表头部开始扫描
{
nheadDown= (*iter3)->next;
while(nheadDown!= NULL)
{
iter2 =list_Dag.begin();
tmpResult->rchild= tmp2;
tmpResult->next = tmp1;
nhead->next =tmpResult;
list_Dag.push_back(nhead);
break;
default:
cout<<"您输入的四元式不是0型、1型或者2型的,请输入这三种类型的四元式:"<<endl;
}
voidPaintFunction(DagNode*node)//画出DAG结点
编译原理-实验4.中间代码优化实验报告
一、实验目的
理解中间代码优化的过程和基本方法,掌握0型四元式、1型四元式和2型四元式的基本块构造。
二、实验要求
编制程序,完成局部优化过程中的基本块划分。给定一段代码,判定程序的入口语句,划分基本块,删除无用产生式和冗余节点。
三、补充完整的源程序代码
#include <list>
input_source.push_back("*,2,T0,T3");
input_source.push_back("+,R,r,T4");
input_source.push_back("*,T3,T4,T5");
input_source.push_back("-,R,r,T6");
input_source.push_back("*,T5,T6,B");
tmpResult->flag = 0;
tmpResult->lchild= tmp1;
tmpResult->rchild= NULL;
tmpResult->next = tmp1;
nhead->next =tmpResult;
list_Dag.push_back(nhead);
break;
case2:
chararg1[10]={0};
chararg2[10]={0};
charresult[10]={0};
sscanf(input_source[i].c_str(),"%[^,],%[^,],%[^,],%[^\n]", op,arg1,arg2,result);//sscanf() -从一个字符串中读进与指定格式相符的数据
/********************************************************************/
/* implementation of functions */
/********************************************************************/