📄 编译原理dlg.cpp
字号:
for(p=i+1;p<word2_len;p++)
strcat(process,word2[p]);
strcat(process," ");
//---------------
row=stk.top();
prior=getPrior(row,current);
switch(prior)
{
case 1:
strcat(process,"归约");
time=0;
do{
Q=row;
wd.push(row);
stk.pop();
len_sb--;
row=stk.top();
prior=getPrior(row,Q);
//要特别讨论i=E的情况
if((prior==-1)&&(Q=="E")&&(row=="=")){
if(time==0)
prior=0;
}
if((prior==0)&&(time>1)&&(Q=="B")&&(row=="("))
prior=-1;
time++;
}while(prior!=-1);
if(wd.top()){
strcpy(Temp,wd.top());
wd.pop();
while(!wd.empty())
{
strcat(Temp,wd.top());
wd.pop();
}
}
else{
// MessageBox("error1");
isTrue=false;
i=word2_len+1;
}
product_p=0;
for(m=0;m<27;m++)
{
if(product_r[m]==Temp){
stk.push(product_l[m]);
stk_sb[len_sb]=product_l[m];
len_sb++;
//translate
if(m==8)//i=E
{
x=oprend.top();
oprend.pop();
nextstart++;
mySYS.NO=nextstart;
mySYS.a=x.place;
mySYS.b="@";
mySYS.r=oprend.top().place;
mySYS.opt="=";
SYS.push_back(mySYS);
}
else if((m>8)&&(m<13))//+,-,*,/
{
if(!oprend.empty())
{
y=oprend.top();
oprend.pop();
if(!oprend.empty())
{
x=oprend.top();
oprend.pop();
}
}
rt++;
RT.Format("%d",rt);
pt.place=RT;
oprend.push(pt);
char *k=new char[2];
k[0]=Temp[1];
k[1]='\0';
opt.Format("%s",k);//operator
nextstart++;
mySYS.NO=nextstart;
mySYS.r=RT;
mySYS.a=x.place;
mySYS.b=y.place;
mySYS.opt=opt;
SYS.push_back(mySYS);
}
else if((m==15)||(m==16))//&&
{
if(!bol.empty())
{
E2=bol.top();
bol.pop();
if(!bol.empty())
{
E1=bol.top();
bol.pop();
if(m==15)//&&
{
backpatch(E1.True,E2.codebegin);
e.codebegin=E1.codebegin;
Merge(E1.False,E2.False,e.False);
for(int II=0;II<10;II++)
{
if(E2.True[II]>0)
e.True[II]=E2.True[II];
}
}
else
{
backpatch(E1.False,E2.codebegin);
e.codebegin=E1.codebegin;
Merge(E1.True,E2.True,e.True);
for(int ii=0;ii<10;ii++)
{
if(E2.True[ii]>0)
e.False[ii]=E2.False[ii];
}
}
bol.push(e);
}
}
else
{
isTrue=false;
break;
}
}
else if(m==17)//idRid
{
y=oprend.top();
oprend.pop();
logical=oprend.top();
oprend.pop();
x=oprend.top();
oprend.pop();
nextstart++;
e.True[0]=nextstart;
e.codebegin=nextstart;
e.False[0]=nextstart+1;
bol.push(e);
mySYS.opt="if";
mySYS.b="goto";
mySYS.r="--";
log[0]='\0';
strcpy(log,x.place);
strcat(log,logical.place);
strcat(log,y.place);
str.Format("%s",log);
mySYS.a=str;
mySYS.NO=nextstart;
SYS.push_back(mySYS);
nextstart++;
mySYS.NO=nextstart;
mySYS.opt="";
mySYS.a="";
SYS.push_back(mySYS);
}
else if((m>17)&&(m<20))//true,false
{
pt.place=Temp;
oprend.push(pt);
nextstart++;
if(m==18)//true
{
e.True[0]=nextstart;
e.codebegin=nextstart;
}
else
{
e.False[0]=nextstart;
e.codebegin=nextstart;
}
bol.push(e);
mySYS.NO=nextstart;
mySYS.opt="";
mySYS.a="";
mySYS.r="--";
SYS.push_back(mySYS);
}
else if((m>20)&&(m<27))
{
pt.place=Temp;
oprend.push(pt);
}
break;
}
}
if(m==27)
{
isTrue=false;
//MessageBox("error4");
i=word2_len+1;
}
break;
case 0:
if(row=="G"){
if(current=="#"){
strcat(process,"分析成功");}
else{
// MessageBox("error2");
isTrue=false;
}
i=word2_len+1;
}
else
{
if((current=="id")||(current=="i")){
pt.place=word2[i];
oprend.push(pt);
}
stk.push(current);
stk_sb[len_sb]=current;
len_sb++;
i++;
current=word2[i];
}
break;
case -1:
if((current=="id")||(current=="i")){
pt.place=word2[i];
oprend.push(pt);
}
stk.push(current);
stk_sb[len_sb]=current;
len_sb++;
i++;
current=word2[i];
break;
case 2:
if((row=="K1")&&(current=="}")){
stk.push("K");
stk_sb[len_sb]="K";
len_sb++;
}
else
{
// MessageBox("error3");
i=word2_len+1;
isTrue=false;
}
break;
}
OUT_file<<process;
OUT_file<<nextline;
}
OUT_file.close();
if(!bol.empty())
{
e=bol.top();
backpatch(e.True,1);
backpatch(e.False,nextstart+1);
}
}
void CMyDlg::emit(CString opt, CString a, CString b, CString R)
{
char *TEMP=new char[40];
int t=2;
if(out_type==2)//四元式形式
{
if((b=="goto")&&(a!=""))
{
TEMP[0]='(';
TEMP[1]='j';
for(int i=1;i<50;i++)
{
if((a.GetAt(i)=='<')||(a.GetAt(i)=='>')||(a.GetAt(i)=='=')||(a.GetAt(i)=='!'))
{
TEMP[t]=a.GetAt(i);
i++;
if(a.GetAt(i)=='=')
{
t++;
TEMP[t]='=';
i++;
}
t++;
TEMP[t]=',';
int j=0;
while(!((a.GetAt(j)=='<')||(a.GetAt(j)=='>')||(a.GetAt(j)=='=')||(a.GetAt(j)=='!')))
{
t++;
TEMP[t]=a.GetAt(j);
j++;
}
t++;
TEMP[t]=',';
while(a.GetAt(i)!=' ')
{
t++;
TEMP[t]=a.GetAt(i);
if(i<a.GetLength()-1)
i++;
else
break;
}
t++; TEMP[t]=',';
t++; TEMP[t]=R.GetAt(0);
t++;TEMP[t]=')';
TEMP[t+1]='\0';
break;
}
}
}
else
{
strcpy(TEMP,"(");
if(a=="")
{
strcat(TEMP,"jump,--,--");
}
else
{
strcat(TEMP,opt); strcat(TEMP,","); strcat(TEMP,a);
strcat(TEMP,","); strcat(TEMP,b);
}
strcat(TEMP,","); strcat(TEMP,R); strcat(TEMP,")");
}
}
in_tran<<TEMP;//写入文件中
in_tran<<nextline;
}
void CMyDlg::backpatch(int a[], int b)//回填
{
for(int i=0;i<10;i++)
{
if(a[i]>0)//大于0就要回填
{
for(iter=SYS.begin();iter<SYS.end();iter++)
{
if(iter->NO==a[i])
{
str.Format("%d",b);
iter->r=str;
}
}
}
}
}
void CMyDlg::Merge(int a[], int b[],int R[])//合并
{
int r=0;
for(int i=0;i<10;i++)
{
if(a[i]>0)
{
R[r]=a[i];
r++;
}
if(b[i]>0)
{
R[r]=b[i];
r++;
}
if(r>10)
{
MessageBox("合并越界!");
break;
}
}
}
void CMyDlg::Printsiyushi()
{
in_tran.open("tran.txt",ios::out);
char *con=new char[100];
for(iter=SYS.begin();iter<SYS.end();iter++)
{
strcpy(con,"(");
str.Format("%d",iter->NO);
strcat(con,str);
strcat(con,") ");
in_tran<<con;
emit(iter->opt,iter->a,iter->b,iter->r);
}
in_tran.close();
}
void CMyDlg::PrintInWindow()
{
char temp;
int j=0;
fstream infile1,infile2,infile3;
m_result.ReplaceSel("----------------词法分析阶段------------------");
m_result.ReplaceSel(nextline);
infile1.open("cifafenxi.txt",ios::in);
j=0;
infile1.get(temp);
while(!infile1.eof())//注意了 有时候如果我们引入的文件是fstream.h 则要循环条件是output!=EOF
{
content[j]=temp;
j++;
if(j==10000)
{
MessageBox("content数组越界");
}
infile1.get(temp);
}
content[j]='\0';
infile1.close();
m_result.ReplaceSel(content);//添加内容
m_result.ReplaceSel(nextline);
m_result.ReplaceSel("--------翻译结果-------");
m_result.ReplaceSel(nextline);
infile3.open("tran.txt",ios::in);
j=0;
infile3.get(temp);
while(!infile3.eof())
{
content[j]=temp;
j++;
if(j==10000)
{
MessageBox("content数组越界");
}
infile3.get(temp);
}
content[j]='\0';
infile3.close();
m_result.ReplaceSel(content);
m_result.ReplaceSel(nextline);
}
void CMyDlg::OnAnalyse_Button()
{
OnAnalyse();
}
void CMyDlg::ClearVar()//清空一些变量的值
{
while(!SYS.empty())//每次分析的使用要将向量清空,防止tran.txt中有重叠的四元式
SYS.pop_back();
for(int i=0;i<10;i++)
{
e.True[i]=0;
e.False[i]=0;
E1.True[i]=0;
E1.False[i]=0;
E2.True[i]=0;
E2.False[i]=0;
}
}
int CMyDlg::getPrior(CString a, CString b)//2
{
int table[37][37]={
{2,2,-1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,-1,2,2,2,2,2,2,2,2,2},
{2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,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,0,2,1,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,2,2,2,-1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,-1,2,2,2,-1,2,2},
{2,2,2,2,2,-1,1,2,2,2,2,2,2,-1,-1,-1,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,0,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,1,-1,2,2,2,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{-1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2,2,2,2,2,2,2}
};
int row=0,column=0;
CString row_str[37]={"do","while","{","}",";","(",")","i","=","+","-","*","/","id",
"true","false","&&","||","<","<=",">",">=","==","!=","ε",
"G","A","A1","N","K","K1","B","B1","E","E1","R","#"};
for(int i=0;i<37;i++)
{
if(a==row_str[i])
row=i;
if(b==row_str[i])
column=i;
}
return table[row][column];
}
int CMyDlg::Compare2(CString str)
{
CString row_str[37]={"do","while","{","}",";","(",")","=","+","-","*","/",
"true","false","&&","||","<","<=",">",">=","==","!=","ε",
"G","A","A1","N","K","K1","B","B1","E","E1","R","#","i","id"};
for(int i=0;i<37;i++)
{
if(str==row_str[i])
{
return 0;
break;
}
}
return 1;
}
void CMyDlg::OnButton1()
{
Onsave();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -