编译原算符优先分析实验报告Word格式文档下载.docx
《编译原算符优先分析实验报告Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《编译原算符优先分析实验报告Word格式文档下载.docx(26页珍藏版)》请在冰豆网上搜索。
【源代码】
#include<
stdio.h>
stdlib.h>
string.h>
charGrammar[20][10];
charVN[10],VT[10];
charBoolArray[10][10];
charFirstBoolArray[10][10];
charLastBoolArray[10][10];
charRelationShip[10][10];
#defineStackSize100;
intvtNum,vnNum;
intgrammarNum;
intscount=0;
intVNum[20];
intGF[2][10];
typedefstruct{
charvt;
charvn;
}array;
array*base;
array*top;
intstacksize;
}SqStack;
typedefstruct{
chars[20];
intstep;
charcurInVt;
}CharType;
CharType*base;
CharType*top;
}Stack;
typedefstruct
{
intx;
inty;
}Position;
SqStackS;
StackCS;
SqStackInitStack()
inti,j;
S.base=(array*)malloc(100*sizeof(array));
if(!
S.base)exit
(1);
S.top=S.base;
S.stacksize=StackSize;
arraytemp;
printf("
初始化栈:
\n"
);
for(i=1;
i<
=vnNum;
i++)
{
for(j=1;
j<
=vtNum;
j++)
{
if(BoolArray[i][j]=='
1'
)
{
temp.vt=BoolArray[0][j];
temp.vn=BoolArray[i][0];
*S.top=temp;
S.top++;
printf("
%c,%c\n"
temp.vn,temp.vt);
}
}
}
returnS;
}
intvNumCount()
for(inti=0;
=grammarNum;
intj=1;
while(Grammar[i][j]!
='
\0'
j++;
VNum[i]=j;
printf("
%d"
VNum[i]);
return0;
intScanGrammar()
FILE*fp=fopen("
算符优先文法.txt"
"
r"
FILE*tp;
charsingleChar,nextChar;
inti=0,j=0,k,count;
while(!
feof(fp))
fscanf(fp,"
%c"
&
singleChar);
if(singleChar=='
?
'
Grammar[i][j]='
;
break;
\n'
i++;
j=0;
continue;
-'
tp=fp;
fscanf(tp,"
nextChar);
if(nextChar=='
>
fp=tp;
continue;
|'
Grammar[i+1][0]=Grammar[i][0];
i++;
j=1;
Grammar[i][j]=singleChar;
if(singleChar>
A'
&
singleChar<
Z'
count=0;
while(VN[count]!
=singleChar&
VN[count]!
count++;
if(VN[count]=='
VN[count]=singleChar;
vnNum=count+1;
else
count=0;
while(VT[count]!
VT[count]!
if(VT[count]=='
VT[count]=singleChar;
vtNum=count+1;
j++;
输入的文法:
for(k=0;
k<
=i;
k++)
j=0;
while(Grammar[k][j]!
if(j==1)
->
"
printf("
Grammar[k][j]);
vnNum:
%d\n"
vnNum);
vtNum:
vtNum);
%d\nVN:
i);
count=0;
while(VN[count]!
BoolArray[count+1][0]=VN[count];
LastBoolArray[count+1][0]=VN[count];
%d%c"
count+1,BoolArray[count+1][0]);
count++;
}
\nVT:
while(VT[count]!
BoolArray[0][count+1]=VT[count];
LastBoolArray[0][count+1]=VT[count];
count+1,BoolArray[0][count+1]);
fclose(fp);
returni;
intprint()
for(inti=1;
for(intj=1;
%c%c\n"
BoolArray[i][0],BoolArray[0][j]);
intprintlast()
if(LastBoolArray[i][j]=='
LastBoolArray[i][0],LastBoolArray[0][j]);
intBoolArrayInitial(charvn,charvt)
introw=1,column=1;
while(BoolArray[row][0]!
=vn)
row++;
while(BoolArray[0][column]!
=vt)
column++;
if(BoolArray[row][column]=='
return0;
BoolArray[row][column]='
%d%d\n"
row,column);
intLastBoolArrayInitial(charvn,charvt)
while(LastBoolArray[row][0]!
while(LastBoolArray[0][column]!
if(LastBoolArray[row][column]=='
LastBoolArray[row][column]='
intFirstInitial(intgrammarNum)
inti;
for(i=0;
if(Grammar[i][1]<
||Grammar[i][1]>
BoolArrayInitial(Grammar[i][0],Grammar[i][1]);
if(Grammar[i][1]>
Grammar[i][1]<
Grammar[i][2]!
if(Grammar[i][2]<
||Grammar[i][2]>
BoolArrayInitial(Grammar[i][0],Grammar[i][2]);
文法表中各行文法字符数:
vNumCount();
intLastInitial(intgrammarNum)
inti,count;
count=VNum[i]-1;
if(Grammar[i][count]<
||Grammar[i][count]>
LastBoolArrayInitial(Grammar[i][0],Grammar[i][count]);
if(count<
=1)continue;
if(Grammar[i][count]>
Grammar[i][count]<
Grammar[i][count-1]!
if(Grammar[i][count-1]<
||Grammar[i][count-1]>
LastBoolArrayInitial(Grammar[i][0],Grammar[i][count-1]);
printlast();
PositionGetPos(arraytemp)
if(BoolArray[i][0]==temp.vn)
for(intj=1;
if(BoolArray[0][j]==temp.vt)
Positionpos;
pos.x=i;
pos.y=j;
returnpos;
intPrintStack()
array*p=S.top,scan;
scount++;
scount%d:
scount);
while(p!
=S.base)
scan=*p;
scan.vn,scan.vt);
p--;
intInsert(arraypushTemp)
arrayscan;
pos=GetPos(pushTemp);
if(BoolArray[pos.x][pos.y]=='
BoolArray[pos.x][pos.y]='
array*p=S.base;
while(p!
=S.top)
scan=*p;
if(scan.vn==pushTemp.vn&
scan.vt==pushTemp.vt)
break;
p++;
if(p==S.top)
S.top++;
*S.top=pushTemp;
PrintStack();
return1;
intPushPop()
arraytemp,pushTemp;
while(S.top!
temp=*S.top;
S.top--;
for(i=0;
if(Grammar[i][1]==temp.vn)
pushTemp.vn=Grammar[i][0];
pushTemp.vt=temp.vt;
Insert(pushTemp);
print();
intLastPushPop()
count=VNum[i]-1;
if(Grammar[i][count]==temp.vn)
intResetStack()
intJudge(inti,intj)
if(Grammar[i][j+1]=='
)return-1;
if(Grammar[i][j]>
Grammar[i][j]<
(Grammar[i][j+1]<
||Grammar[i][j+1]>
))
elsereturn1;
PositionGetOpPos(charvni,charvnj)
if(RelationShip[i][0]==vni)
if(RelationShip[0][j]==vnj)
intGetVtPos(charvt)
if(LastBoolArray[i][0]==vt)
intRelEqual()
intj;
j=1;
if(Grammar[i][j+2]=='
)break;
if((Grammar[i][j]<
||Grammar[i][j]>
)&
(Grammar[i][j+2]<
||Grammar[i][j+2]>
pos=GetOpPos(Grammar[i][j],Grammar[i][j+2]);
RelationShip[pos.x][pos.y]='
//2表示=
intRelShBlanket()
introw;
RelationShip[i][0]=BoolArray[0][i];
RelationShip[0][i]=BoolArray[0][i];
if(Judge(i,j)==-1)break;
if(Judge(i,j)==0)
row=GetVtPos(Grammar[i][j]);
for(intii=1;
ii<
=9;
ii++)
{
if(LastBoolArray[row][ii]=='
{
pos=GetOpPos(LastBoolArray[0][ii],Grammar[i][j+1]);
RelationShip[pos.x][pos.y]='
//3表示大于
}
}
if(Judge(i,j)==1)
row=GetVtPos(Grammar[i][j+1]);
if(FirstBoolArray[row][ii]=='
pos=GetOpPos(Grammar[i][j],FirstBoolArray[0][ii]);
<
//1表示小于
RelEqual();
表达式文法算符优先关系表:
RelationShip[0][0]='
'
for(intj=0;
if(RelationShip[i][j]=='
RelationShip[i][j]='
0'
%4c"
RelationShip[i][j]);
intChangeValue()
intchange=0;
if(GF[0][i]<
=GF[1][j])
GF[0][i]=GF[1][j]+1;
change=1;
if(GF[0][i]>
GF[1][j]=GF[0][i]+1;
if(GF[0][i]!
if(GF[0][i]>
GF[1][j])
GF[1][j]=GF[0][i];
change=1;
if(GF[0][i]<
GF[0][i]=GF[1][j];
if(change==1)