📄 test.cpp
字号:
temp1.offset=level-ProcTable.table[num].plevel; //常数值:相对层差
temp3.type=1; //常数
temp3.offset=ProcTable.table[num].quaterposition; //常数值:该过程四元式首址
GEN(6,temp1,temp2,temp3);
return true;
case 11:
value=signCache[0]; //取过程名
num=CHECK(value); //检查过程调用是否合理
if(num==-1) return false;
temp2.type=-1;
temp1.type=1; //常数
temp1.offset=level-ProcTable.table[num].plevel; //常数值:相对层差
temp3.type=1; //常数
temp3.offset=ProcTable.table[num].quaterposition; //常数值:该过程四元式首址
GEN(6,temp1,temp2,temp3);
return true;
case 12:
temp1.type=temp2.type=-1;
while(part.size()!=0)
{
temp3=part.front();
part.pop();
GEN(18,temp1,temp2,temp3);
}
return true;
case 13:
temp1.type=temp2.type=-1;
while(part.size()!=0)
{
temp3=part.front();
part.pop();
GEN(20,temp1,temp2,temp3);
}
return true;
case 14: return true;
case 15:
BACKPATCH(chain.top(),PC+1);
chain.pop();
chain.push(PC);
temp1.type=temp2.type=temp3.type=-1;
GEN(11,temp1,temp2,temp3);
return true;
case 16: return true;
case 17: return true;
case 18:
temp2=place.top();
place.pop();
temp3=place.top();
place.pop();
temp1=place.top();
place.pop();
place.push(temp3);
temp3.type=-1;
chain.push(PC);
GEN(17,temp1,temp2,temp3);
temp2=place.top();
place.pop();
GEN(1,temp1,temp2,temp1);
return true;
case 19: return true;
case 20:
value=signCache[2]; //取ident
num=searchVTable(value);
if(num==-1) return false; //变量未定义
temp3.name=value;
temp3.offset=VariableTable.table[num].relposition;
temp3.rlevel=level-VariableTable.table[num].plevel;
temp3.type=0;
temp1=place.top(); //取第一操作数
place.pop();
temp2.type=-1; //第二操作数不使用
GEN(5,temp1,temp2,temp3); //四元式:赋值
place.push(temp3);
return true;
case 21:
temp1=place.top();
place.pop();
part.push(temp1);
return true;
case 22:
temp1=place.top();
place.pop();
part.push(temp1);
return true;
case 23: return true;
case 24: return true;
case 25: return true;
case 26:
value=signCache[0]; //取变量名
signCache[0]=NULL; //清空signCache
num=searchVTable(value); //查变量表
if(num==-1) return false; //变量未定义
temp1.name=value;
temp1.offset=VariableTable.table[num].relposition;
temp1.rlevel=level-VariableTable.table[num].plevel;
temp1.type=0;
part.push(temp1);
return true;
case 27: return true;
case 28:
case 29:
case 30:
case 31:
temp2=place.top(); //取第二操作数
place.pop();
temp1=place.top(); //取第一操作数
place.pop();
K++; //申请临时变量
temp3.type=2; //临时变量
if(K<10) //0-9号临时变量
{
value=(char*)malloc(3*sizeof(char));
c[0]=K+48;
c[1]='\0';
strcpy(value,"T");
strcat(value,c);
}
else
if(K<100) //10-99号临时变量
{
int i,j;
value=(char*)malloc(4*sizeof(char));
i=K/10;
j=K-10*i;
strcpy(value,"T");
c[0]=i+48;
c[1]=j+48;
c[2]='\0';
strcat(value,c);
}
else
if(K<1000) //100-999号临时变量
{
int i,j,l;
value=(char*)malloc(5*sizeof(char));
i=K/100;
j=(K-100*i)/10;
l=K-100*i-10*j;
strcpy(value,"T");
c[0]=i+48;
c[1]=j+48;
c[2]=l+48;
c[3]='\0';
strcat(value,c);
}
temp3.name=value;
temp3.offset=K;
temp3.rlevel=0;
place.push(temp3); //临时变量入栈
GEN(productor-27,temp1,temp2,temp3); //生成相应的四元式
return true;
case 32:
temp1=place.top(); //取第一操作数
place.pop();
temp2.type=-1; //第二操作数不使用
K++; //申请临时变量
temp3.type=2; //临时变量
if(K<10) //0-9号临时变量
{
value=(char*)malloc(3*sizeof(char));
c[0]=K+48;
c[1]='\0';
strcpy(value,"T");
strcat(value,c);
}
else
if(K<100) //10-99号临时变量
{
int i,j;
value=(char*)malloc(4*sizeof(char));
i=K/10;
j=K-10*i;
strcpy(value,"T");
c[0]=i+48;
c[1]=j+48;
c[2]='\0';
strcat(value,c);
}
else
if(K<1000) //100-999号临时变量
{
int i,j,l;
value=(char*)malloc(5*sizeof(char));
i=K/100;
j=(K-100*i)/10;
l=K-100*i-10*j;
strcpy(value,"T");
c[0]=i+48;
c[1]=j+48;
c[2]=l+48;
c[3]='\0';
strcat(value,c);
}
temp3.name=value;
temp3.offset=K;
temp3.rlevel=0;
place.push(temp3); //临时变量入栈
GEN(0,temp1,temp2,temp3); //生成相应的四元式
return true;
case 33: return true;
case 34:
value=signCache[0]; //取变量名
num=searchVTable(value); //查变量表
if(num==-1) return false; //变量未定义
temp1.name=value;
temp1.offset=VariableTable.table[num].relposition;
temp1.rlevel=level-VariableTable.table[num].plevel;
temp1.type=0;
place.push(temp1); //操作数入栈
return true;
case 35:
int mid; //存放转换中的整形数
mid=0;
temp1.type=1; //常数:type=1
value=signCache[0]; //取“常数串”
while((*value)!='\0') //将常数串转换成整形
{
mid=10*mid+(*value)-48;
value++;
}
temp1.offset=mid; //常数:offset=常数值
place.push(temp1); //操作数入栈
return true;
case 36:
int jump;
chain.push(PC); //建立回填链
temp2=place.top(); //取第二操作数
place.pop();
temp1=place.top(); //取第一操作数
place.pop();
jump=quad.top(); //取比较运算符
quad.pop();
temp3.type=-1; //目的操作数:暂时不用,等待回填。
GEN(jump,temp1,temp2,temp3);
return true;
case 37:
quad.push(16);
return true;
case 38:
quad.push(17);
return true;
case 39:
quad.push(15);
return true;
case 40:
quad.push(13);
return true;
case 41:
quad.push(14);
return true;
case 42:
quad.push(12);
return true;
default: return false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数完成查GOTO表的功能
bool checkGoto(int productor)
{
int cluster;
int start;
char *temp;
temp=(char*)malloc((strlen(AL[productor])+1)*sizeof(char));
strcpy(temp,AL[productor]);
signSta.push(temp);
cluster=clusterSta.top(); //取当前状态
start=NL[productor];
do //查找相应状态
{
if(GL[start][0]==cluster||GL[start][0]==-1) break;
start++;
}while(start<41);
if(start==41)
{
isTrue=false;
return false;
}
clusterSta.push(GL[start][1]);
if(isTrue==true)
LRSemanticor(productor);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数完成查ACTION表的功能
int checkAction()
{
int cluster;
int start;
int act;
int plen;
int i;
char *temp;
cluster=clusterSta.top(); //取当前状态
while(WORD.index==0||WORD.index==35) //去掉换行符
{
if(WORD.index==35) linenum++;
getWord();
}
start=SL[cluster]; //取查SA数组的起始行
do //查找相应单词(编码)
{
if(SA[start][0]==WORD.index||SA[start][0]==-1) break;
start++;
}while(SA[start][0]!=0);
if(SA[start][0]==0)
{
isTrue=false;
return 0; //出错
}
act=SA[start][1]; //取相应动作
if(act==999)
{
while(signSta.size()!=0)
{
signSta.pop(); //清空符号栈
}
while(clusterSta.size()!=0)
{
clusterSta.pop(); //清空状态栈
}
return 2; //LR分析结束
}
if(act>0) //移进操作
{
temp=(char*)malloc((strlen(WORD.value)+1)*sizeof(char));
strcpy(temp,WORD.value);
clusterSta.push(act);
WORD.index=0; //清空WORD,准备再次读入
signSta.push(temp);
return 1;
}
if(act<0) //归约操作
{
act=-act; //取正数:产生式编号
plen=PL[act]; //取产生式右部长度
for(i=0;i<plen;i++)
{
clusterSta.pop();
temp=signSta.top();
signSta.pop();
signCache[i]=temp; //保留规约式
}
if(checkGoto(act)==false)
{
isTrue=false;
return 0;
}
return 1;
}
isTrue=false;
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数初始化全局变量
void initialize()
{
int i;
// linenum=1; //初始化行计数器
// WORD.index=0; //没有单词读入(去掉begin)
// WORD.next=NULL;
clusterSta.push(0); //状态0入栈
for(i=0;i<5;i++)
signCache[i]=NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数显示状态栈的内容(用于调试)
void showclusterStack()
{
int k;
FILE *fp;
stack<int> temp;
fp=fopen("showclusterStack.txt","a");
while(clusterSta.size()!=0)
{
k=clusterSta.top();
temp.push(k);
clusterSta.pop();
}
while(temp.size()!=0)
{
k=temp.top();
clusterSta.push(k);
// printf("%d ",k);
fprintf(fp,"%d ",k);
temp.pop();
}
// printf("\n");
fprintf(fp,"\n");
fclose(fp);
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数显示符号栈的内容(用于调试)
void showsignStack()
{
char *k;
FILE *fp;
stack<char*> temp;
fp=fopen("showsignStack.txt","a");
while(signSta.size()!=0)
{
k=signSta.top();
temp.push(k);
signSta.pop();
}
while(temp.size()!=0)
{
k=temp.top();
signSta.push(k);
fprintf(fp,"%s ",k);
temp.pop();
}
fprintf(fp,"\n");
fclose(fp);
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数完成LR分析中的出错处理
void errorDeal()
{
int back;
char *temp;
printf("Error!行数:%d,字符:%s\n",linenum,WORD.value);
getWord();
while(WORD.index!=0) //去掉换行符
{
if(WORD.index==35) linenum++;
if(WORD.index==4||WORD.index==5||WORD.index==17||WORD.index==14||WORD.index==15||WORD.index==16||WORD.index==6||WORD.index==9||WORD.index==11)
break;
getWord();
}
back=clusterSta.top();
if(WORD.index==5) //如果是“end”前的最后一条语句出错,则不跳到下一条语句
{
while(back!=21&&back!=2) //回退到可以接受“end”的地方
{
clusterSta.pop();
temp=signSta.top();
signSta.pop();
free(temp);
back=clusterSta.top();
}
if(back==2) //更改栈顶状态,以便可以接受“end”,顺利完成后续的翻译。
{
temp=signSta.top();
signSta.pop();
free(temp);
temp=(char*)malloc(2*sizeof(char));
strcpy(temp,"L");
signSta.push(temp);
clusterSta.pop();
clusterSta.push(21);
}
}
else //如果不是“end”前的最后一条语句出错,则跳到下一条语句继续执行。
{
while(back!=0&&back!=2&&back!=6&&back!=8&&back!=9&&back!=10&&back!=29)
{
clusterSta.pop();
temp=signSta.top();
signSta.pop();
free(temp);
back=clusterSta.top();
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//此函数为LR总控程序
void LRAnalyser()
{
int i=0;
// WordAnalyser();
initialize();
while(i!=2)
{
showclusterStack();
showsignStack();
i=checkAction();
if(i==0) errorDeal();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//此函数初始化全局变量
void init(){
FILE* perrfile;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -