📄 cifafenxi.cpp
字号:
CurrentToken.addr=-1;
CurrentToken.label=label_count++;
OutPut();
ch=fgetc(SourceFin);
break;
case '}': CurrentToken.name[0]='}';
CurrentToken.code=50;
CurrentToken.addr=-1;
CurrentToken.label=label_count++;
OutPut();
ch=fgetc(SourceFin);
break;
case '@': CurrentToken.name[0]='@';
CurrentToken.code=51;
CurrentToken.addr=-1;
CurrentToken.label=label_count++;
OutPut();
ch=fgetc(SourceFin);
break;
case 10: LineOfPro++;
ch=fgetc(SourceFin);
break;
case 13: LineOfPro++;
ch=fgetc(SourceFin);
break;
case' ':ch=fgetc(SourceFin);
break;
case EOF:Error(4);
break;
default:
ch=fgetc(SourceFin);
break;
}
}
/**************注释处理*****************************/
void IsAnotation()
{
char ch1;
ch1=ch;
ch=fgetc(SourceFin);
if(ch=='/')
{ ch=fgetc(SourceFin);
if(ch=='*')
{
for(;;)
{
ch=fgetc(SourceFin);
if(ch==EOF)//如果是终结符就停止
{
Error(3);
break;
}
if(ch=='*')
{
ch1=ch;
ch=fgetc(SourceFin);
}
if(ch=='/')
{
ch=fgetc(SourceFin);
break;
}
}
}
else
{
CurrentToken.name[0]='/';
CurrentToken.code=39;
CurrentToken.addr=-1;
CurrentToken.label=label_count++;
OutPut();
}
}
}
/******************输出*************************************/
void OutPut()
{
int flag=0;
int i=0;
int k;
/*查填符号表*/
if((CurrentToken.code==27)||(CurrentToken.code==28)||(CurrentToken.code==29)||(CurrentToken.code==30))
{
CurrentSymble.number=CurrentToken.addr;
CurrentSymble.type=CurrentToken.code;
strcmp(CurrentSymble.name,CurrentToken.name);
flag=WordHave();
if(((CurrentToken.code==27)&&(flag==1))||(CurrentToken.code==28)||(CurrentToken.code==29)||(CurrentToken.code==30))
fprintf(SymbleFout,"%3d %3d %s\n",CurrentSymble.number,CurrentSymble.type,CurrentSymble.name);
}
/*输出到Token表*/
for(;;)if(CurrentToken.name[i++]=='\0') break;
fprintf(TokenFout,"%3d %s",CurrentToken.label,CurrentToken.name);//控制格式
printf("%3d %s",CurrentToken.label,CurrentToken.name);
for(k=20-i;k>0;k--)
{
fprintf(TokenFout," ");
printf(" ");
}
fprintf(TokenFout,"%3d %3d\n",CurrentToken.code,CurrentToken.addr);
printf("%3d %3d\n",CurrentToken.code,CurrentToken.addr);
s1.push(CurrentToken);
}
int WordHave()
{
int flag,i=0;
for(i=0;i<var_count;i++)
{
flag=strcmp(CurrentSymble.name,SymbleList[i].name);
if(flag==0)
{
CurrentToken.addr=SymbleList[i].number;
return 0;
}
}
SymbleList[var_count].number=CurrentToken.addr ;
SymbleList[var_count].type=CurrentToken.code;
strcmp(SymbleList[var_count].name,CurrentToken.name);
var_count++;
return 1;
}
/**************错误提示****************************/
void Error(int a)
{
error_count++;
switch(a)
{
case 1: printf("error %2d 非法字符于 %3d 行.\n",error_count,LineOfPro+1);break;
case 2: printf("error %2d 实常数出错于 %3d 行.\n",error_count,LineOfPro+1);break;
case 3: printf("error %2d 没有匹配的注释符 '*/' \n",error_count);break;
case 4: printf("error %2d 非正常结束!\n",error_count);break;
default:break;
}
return;
}
/*************************语法分析****************************************/
void Analys()
{ int j=0;
int i=0;
printf("语法分析\n");
AnalysInit();
stack<int>StateStack;//状态栈
stack<string>SymbolStack;//符号栈
StateStack.push(i);
StateStack.top();
//cout<<StateStack.top()<<endl;
SymbolStack.push("@");
token t;
token s;
t.name[0]='@';
t.code=51;
t.addr=-1;
t.label=label_count++;
s1.push(t);
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
//cout<<s2.top().code<<endl;
while(!s2.empty())
{
s=s2.top();
//cout<<s2.top().code<<endl;
j=Fuzhi(s.code);
char c=action[i][j].cha;
if(c=='#') break;
else if(c=='s')//移进
{
StateStack.push(action[i][j].state);
SymbolStack.push(s.name);
//cout<<s.name<<"---->"<<"移进"<<endl;
//cout<<SymbolStack.top()<<endl;
i=action[i][j].state;
s2.pop();
}
else if(c=='r')//归约
{
int t=action[i][j].state;
string n=css[t].left;
string m=css[t].right;
q=css[t].len;
int p=BianGoto1(n);
for(int k=0;k<q;k++)
{
StateStack.pop();
string u=SymbolStack.top();
cout<<u<<"----->"<<"出栈"<<endl;
if((t==3)||(t==4)||(t==5)||(t==6)||(t==7)||(t==8)||(t==9)||(t==14)||(t==15)||(t==16)||(t==17))
{
YuyiStack.push(u);
}
SymbolStack.pop();
}
cout<<"按照第"<<t<<"个产生式"<<n<<"-->"<<m<<"---->"<<"归约"<<endl;
int gu=0;
gu=StateStack.top();
int b=GoTo[gu][p];
StateStack.push(b);
string x=css[t].left;
cout<<x<<"---->"<<"进栈"<<endl;
if((t==4)||(t==5)||(t==6)||(t==7)||(t==8)||(t==9))
{
ttttt();
}
if(t==3)
{
ttt();
}
if((t==14)||(t==15)||(t==16)||(t==17))
{
tt();
}
SymbolStack.push(x);
i=b;
}
else if(c=='@')
{cout<<"语法分析成功!"<<endl;
cout<<endl;
cout<<"输出三地址:"<<endl;
/* for(int w=0;w<fu_count;w++)
{
cout<<w<<" "<<l[w]<<endl;
}
cout<<w<<endl;*/
kbb=fu_count;
break;
}
else
cout<<"语法分析出错"<<endl;
}
/*while(!s2.empty())
{
s=s2.top();
cout<<s.code<<endl;
s2.pop();
}*/
}
void ttttt()//算数表达式语义分析
{
string t="t1:=";
for(int i=0;i<q;i++)
{ string c=YuyiStack.top();
t=t+c;
YuyiStack.pop();
}
YuyiStack.push("t1");
l[fu_count]=t;
st.push_back(t);
fu_count++;
}
void ttt()//赋值表达式分析
{
string t;
string c=YuyiStack.top();
t=t+c+":=";
for(int i=0;i<q;i++)
YuyiStack.pop();
t=t+YuyiStack.top();
l[fu_count]=t;
st.push_back(t);
fu_count++;
}
void tt()//布尔表达式分析
{
string t,m,n;
for(int i=0;i<q;i++)
{ string c=YuyiStack.top();
t=t+c;
YuyiStack.pop();
}
m=m+"if"+" "+t+" "+"goto";
l[fu_count]=m;
if(too1==0)
{
to2=fu_count;
to3=fu_count+1;
too1++;
}
else
{
to4=fu_count;
to5=fu_count+1;
}
st.push_back(m);
fu_count++;
n="goto";
l[fu_count]=n;
st.push_back(n);
fu_count++;
}
void print(vector<string> a)//打印
{
vector<string>::iterator it=a.begin();
int i=0;
int len=a.size();
while(i<len)
{
cout<<i<<" "<<*it<<endl;
it++;
i++;
}
cout<<len<<endl;
}
void huitian(vector<string> & a,int t1,int t2)//回填
{
vector<string>::iterator it=a.begin();
int i=0;
while(i<t1)
{
it++;
i++;
}
string kbuf=*it;
char * kkk=new char[10];
char *kkk1=itoa(t2,kkk,10);
string kbuf1=kkk1;
*it=*it+" "+kbuf1;
}
void AnalysInit()
{
//产生式初始化
css[0].left="M";
css[0].right="S";
css[1].left="S";
css[1].right="do A while B;";
css[2].left="A";
css[2].right="{C;}";
css[3].left="C";
css[3].right="a=D;";
css[4].left="D";
css[4].right="a+a";
css[5].left="D";
css[5].right="a-a";
css[6].left="D";
css[6].right="a+i";
css[7].left="D";
css[7].right="a-i";
css[8].left="D";
css[8].right="E*E";
css[9].left="D";
css[9].right="E/E";
css[10].left="D";
css[10].right="i";
css[11].left="E";
css[11].right="(D)";
css[12].left="B";
css[12].right="(F)";
css[13].left="F";
css[13].right="G and G";
css[14].left="F";
css[14].right="a>=a";
css[15].left="F";
css[15].right="a<=a";
css[16].left="F";
css[16].right="a>=i";
css[17].left="F";
css[17].right="a<=i";
css[18].left="G";
css[18].right="(F)";
/**************产生式长度******************/
css[0].len=1;
css[10].len=1;
css[1].len=5;
css[3].len=4;
for(int k=4;k<=9;k++)
{
css[k].len=3;
}
css[2].len=3;
for(int l=11;l<=18;l++)
{
css[l].len=3;
}
/*****************************************/
int i,j;
for(i=0;i<46;i++)
{
for(j=0;j<18;j++)
action[i][j].cha='#';
}
for(i=0;i<46;i++)
{
for(j=0;j<9;j++)
GoTo[i][j]=-1;
}
/*****************************action表****************************/
action[0][0].cha='s';action[0][0].state=2;
action[1][17].cha='@';//结束
action[2][1].cha='s';action[2][1].state=4;
action[3][12].cha='s';action[3][12].state=5;
action[4][2].cha='s';action[4][2].state=7;
action[5][9].cha='s';action[5][9].state=9;
action[6][11].cha='s';action[6][11].state=10;
action[7][3].cha='s';action[7][3].state=11;
action[8][16].cha='s';action[8][16].state=12;
action[9][2].cha='s';action[9][2].state=15;
action[9][9].cha='s';action[9][9].state=16;
action[11][2].cha='s';action[11][2].state=18;
action[11][8].cha='s';action[11][8].state=20;
action[11][9].cha='s';action[11][9].state=21;
for( i=0;i<18;i++)
{action[10][i].cha='r';action[10][i].state=2;
action[12][i].cha='r';action[12][i].state=1;
action[20][i].cha='r';action[20][i].state=10;
action[22][i].cha='r';action[22][i].state=12;
action[32][i].cha='r';action[32][i].state=13;
action[33][i].cha='r';action[33][i].state=14;
action[34][i].cha='r';action[34][i].state=16;
action[35][i].cha='r';action[35][i].state=15;
action[36][i].cha='r';action[36][i].state=17;
action[37][i].cha='r';action[37][i].state=18;
action[38][i].cha='r';action[38][i].state=4;
action[39][i].cha='r';action[39][i].state=6;
action[40][i].cha='r';action[40][i].state=5;
action[41][i].cha='r';action[41][i].state=7;
action[42][i].cha='r';action[42][i].state=8;
action[43][i].cha='r';action[43][i].state=9;
action[44][i].cha='r';action[44][i].state=11;
action[45][i].cha='r';action[45][i].state=3;
}
action[13][10].cha='s';action[13][10].state=22;
action[14][13].cha='s';action[14][13].state=23;
action[15][14].cha='s';action[15][14].state=25;
action[15][15].cha='s';action[15][15].state=24;
action[16][2].cha='s';action[16][2].state=15;
action[17][16].cha='s';action[17][16].state=45;
action[18][4].cha='s';action[18][4].state=27;
action[18][5].cha='s';action[18][5].state=28;
action[19][6].cha='s';action[19][6].state=29;
action[19][7].cha='s';action[19][13].state=30;
action[21][2].cha='s';action[21][2].state=18;
action[23][9].cha='s';action[23][9].state=16;
action[24][2].cha='s';action[24][2].state=33;
action[24][8].cha='s';action[24][14].state=34;
action[25][2].cha='s';action[25][2].state=35;
action[25][8].cha='s';action[25][8].state=31;
action[26][10].cha='s';action[26][10].state=37;
action[27][2].cha='s';action[27][2].state=38;
action[27][8].cha='s';action[27][8].state=39;
action[28][2].cha='s';action[28][2].state=40;
action[28][8].cha='s';action[28][8].state=41;
action[29][8].cha='s';action[29][8].state=21;
action[30][8].cha='s';action[30][8].state=21;
action[31][10].cha='s';action[31][7].state=44;
/*********************goTo表***********************************/
GoTo[0][0]=1;GoTo[2][1]=3;
GoTo[4][3]=6;GoTo[5][2]=8;
GoTo[9][6]=13;GoTo[9][7]=14;
GoTo[11][4]=17;GoTo[11][5]=19;
GoTo[16][6]=26;GoTo[16][7]=14;
GoTo[21][4]=31;GoTo[21][5]=9;
GoTo[23][7]=32;GoTo[30][5]=43;
GoTo[29][5]=42;
}
/******************按action表给输入符赋值***********************************************/
int Fuzhi(int i)
{
int j=-1;
if(i==8) j=0;
if(i==49)j=1;
if(i==27)j=2;
if(i==46)j=3;
if(i==35)j=4;
if(i==36)j=5;
if(i==34)j=6;
if(i==39)j=7;
if(i==28)j=8;
if(i==32)j=9;
if(i==33)j=10;
if(i==50)j=11;
if(i==26)j=12;
if(i==1)j=13;
if(i==44)j=14;
if(i==48)j=15;
if(i==42)j=16;
if(i==51)j=17;
return(j);
}
int BianGoto1(string p)//找到GoTo表的行数
{
int j=-1;
if(p=="S") j=0;
if(p=="A") j=1;
if(p=="B") j=2;
if(p=="C") j=3;
if(p=="D") j=4;
if(p=="E") j=5;
if(p=="F") j=6;
if(p=="G") j=7;
return(j);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -