📄 function&struct.h
字号:
#include"iostream.h"
#include"string.h"
class table{
public:
char name[11];
int cat,val,level,addr;
table * next;
table(char* Name,int Cat)
{
if(Name)
{
strcpy(name,Name);
}
cat=Cat;
}
};
class quad{
public:
char OP[4];
int arg1,arg2,result;
int num;
quad * next;
quad(char *string,int Arg1,int Arg2,int Result,int Num=1)
{
strcpy(OP,string);
arg1=Arg1;
arg2=Arg2;
result=Result;
num=Num;
}
};
extern table* t_first; //符号表的首指针
extern quad* q_first,*q_last; //四元式的首指针,末指针
int vary_addr=5; //局部变量的起始地址
extern int const_tab; //常量表当前大小,在scanner.h中定义
void Gen(char* OP,int result,int arg1,int arg2) //四元式生成函数
{
quad* newone;
if(q_last!=NULL)
{
newone=new quad(OP,arg1,arg2,result,q_last->num+1);
q_last->next=newone;
newone->next=NULL;
q_last=q_last->next;
}else{ //第一个四元式
newone=new quad(OP,arg1,arg2,result);
q_first=q_last=newone;
newone->next=NULL;
}
}
quad* find(int num) //跟据四元式的序号,查找相应的四元式指针
{
quad* temp=q_first;
while(temp!=NULL)
{
if(temp->num==num)
return temp;
else
temp=temp->next;
}
return NULL;
}
void backpatch(int q,int t)
{
int temp;
quad* current;
if(q==0)
{
return;
}else{
current=find(q);
while(current->result!=0)
{
temp=current->result;
current->result=t;
current=find(temp);
}
current->result=t;
}
}
int merg(int p1,int p2)
{
quad* current=find(p2);
if(p2==0)
return p1;
else{
if(current->result==0)
{
current->result=p1;
return p1;
}
else{
while((current=find(current->result))->result!=0);//该循环没有语句!
current->result=p1;
return p2;
}
}
}
void fill(char* id,int k)
{
table* current=t_first->next;
table* newone;
while(current!=NULL)
{
if(strcmp(current->name,id)==0)
{
cout<<id<<"重定义!"<<endl;
return;
}
if(current->next==NULL)
break;
else
current=current->next;
}
switch(k)
{
case 0:
newone=new table(id,k);
newone->level=0; //实验三中level均为0;
newone->addr=vary_addr++;
if(current!=NULL)
{
current->next=newone;
newone->next=NULL;
}else{ //处理首结点
t_first->next=newone;
newone->next=NULL;
}
break;
case 1:
newone=new table(id,k);
newone->val=const_tab-1; //常量在常量表中的位置
if(current!=NULL)
{
current->next=newone;
newone->next=NULL;
}else{ //处理首结点
t_first->next=newone;
newone->next=NULL;
}
break;
case 2:;//实验三不处理;
}
}
int entry(char *id)
{
table* current=t_first;
while(current!=NULL)
{
if(strcmp(current->name,id)==0)
{
if(current->cat==1)
return 1000+current->val;
else
return current->addr;
}else
current=current->next;
}
return -1; //查不到刚反回-1
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -