📄 yufa.c
字号:
#include <stdio.h>
#include <dir.h>
#include <string.h>
int graph[17][17];
int p_exp[30][10];
char exp[35][22];
int getwords(int* words);
int findword(char* words);
int getlength(int pnum);
void init();
main(){
char dstFilename[MAXPATH];
int words[500];
int i,tempi,tempj,j,temp;
int stack[40];
int top,ptr,plenth;
FILE* fpDst=NULL;
init();
j=getwords(words);
printf("please enter the output produce expression filename : ");
scanf("%s",dstFilename);
fpDst=fopen(dstFilename,"wb");
if(fpDst==NULL)
{
perror("\nOpen token file failed!");
getch();
exit(0);
}
stack[0]=33;stack[1]=0;top=1;ptr=0;
while(stack[top]!=33)
{
if(stack[top]>=17&&stack[top]<=33)
{
if(stack[top]==words[ptr])
{
if( stack[top] != 33 )
{
top--;
ptr++;
}
}
else
{
printf("chongtu error");getch();exit(0);
}
}
else
{
if( graph[stack[top]][words[ptr]-17]!=-1)
{
/*输出产生式左部*/
fputs(exp[stack[top]],fpDst); fputs("—>",fpDst);
printf("%s-->",exp[stack[top]]);
tempi=graph[stack[top]][words[ptr]-17];/*得到产生式的序号*/
tempj=getlength(tempi)-1; /*得到产生式右部单词个数*/
top--;
for(i=tempj;i>=0;i--) /*将产生式右部压入栈*/
{
if(p_exp[tempi][i]==34)
break;
stack[++top]=p_exp[tempi][i];
}
/*输出产生式右部*/
for(i=0;i<=tempj;i++)
{
printf("%s ",exp[p_exp[tempi][i]]);
fputs(exp[p_exp[tempi][i]],fpDst);
fputs(" ",fpDst);
}
fputs("\r\n",fpDst);
/* printf(" ");
for(i=0;i<=top;i++)
printf("%s ",exp[stack[i]]);
printf(" buffer : %s",exp[words[ptr]]); */
printf("\n");
/* printf("words[ptr] %d\n",words[ptr]);*/
}
else
{
printf("wu chansheng error");getch();exit(0);
}
}
}
if(fpDst!=NULL)
fclose(fpDst);
getch();
}
int getwords(int* words)
{
char srcFilename[MAXPATH];
char buffer[800],ch;
int i=0,forward,backward,j=0,temp;
FILE* fpSrc=NULL;
char tempword[10];
printf("please enter the token filename : ");
scanf("%s",srcFilename);
fpSrc=fopen(srcFilename,"rb");
if(fpSrc==NULL)
{
perror("\n open the source file failed.");
exit(0);
}
while((ch=fgetc(fpSrc))!=EOF)
{
buffer[i++]=ch;
}
buffer[i]='\0';
for(forward=backward=0;buffer[forward]!='\0';forward++)
{
backward=forward;
while(1)
{
if(buffer[forward]=='<')
{
forward++;
backward++;
continue;
}
else if( buffer[forward]==',')
{
for(temp=0;temp<forward-backward;temp++)
{
tempword[temp]=buffer[backward+temp];
}
tempword[temp]='\0';
*(words+j)=findword(tempword);
j++;
forward++;
}
else
{
forward++;
if(buffer[forward]=='\n')
break;
}
}
}
*(words+j)=33;
j++;
if(fpSrc!=NULL)
fclose(fpSrc);
return j;
}
void init()
{
int i=0,j=0;
/* 所有符号初始化*/
strcpy(exp[0],"Program");strcpy(exp[1],"Slist");strcpy(exp[2],"Dlist");
strcpy(exp[3],"Dlist\'"); strcpy(exp[4],"Olist");strcpy(exp[5],"Olist\'");
strcpy(exp[6],"D");strcpy(exp[7],"O");strcpy(exp[8],"O\'");
strcpy(exp[9],"type");strcpy(exp[10],"exp");strcpy(exp[11],"exp\'");
strcpy(exp[12],"simple_exp");strcpy(exp[13],"simple_exp\'");
strcpy(exp[14],"term");strcpy(exp[15],"term\'");strcpy(exp[16],"factor");
strcpy(exp[17],"id"); strcpy(exp[18],"num"); strcpy(exp[19],"relop");
strcpy(exp[20],"addop"); strcpy(exp[21],"mulop"); strcpy(exp[22],"(");
strcpy(exp[23],")"); strcpy(exp[24],"{"); strcpy(exp[25],"}");
strcpy(exp[26],";"); strcpy(exp[27],"if");strcpy(exp[28],"else");
strcpy(exp[29],"while");strcpy(exp[30],"!"); strcpy(exp[31],"=");
strcpy(exp[32],"int"); strcpy(exp[33],"#"); strcpy(exp[34],"ε");
/*产生式初始化*/
for(i=0;i<=29;i++)
for(j=0;j<10;j++)
p_exp[i][j]=-1;
p_exp[0][0]=17; p_exp[0][1]=22;p_exp[0][2]=23;
p_exp[0][3]=24; p_exp[0][4]=1;p_exp[0][5]=25;
p_exp[1][0]=2; p_exp[1][1]=4;
p_exp[2][0]=4;
p_exp[3][0]=34;
p_exp[4][0]=6; p_exp[4][1]=3;
p_exp[5][0]=6; p_exp[5][1]=3;
p_exp[6][0]=34;
p_exp[7][0]=9; p_exp[7][1]=17;p_exp[7][2]=26;
p_exp[8][0]=7; p_exp[8][1]=5;
p_exp[9][0]=7; p_exp[9][1]=5;
p_exp[10][0]=34;
p_exp[11][0]=17; p_exp[11][1]=31;p_exp[11][2]=10;p_exp[11][3]=26;
p_exp[12][0]=29; p_exp[12][1]=22;p_exp[12][2]=10;
p_exp[12][3]=23; p_exp[12][4]=24;p_exp[12][5]=4; p_exp[12][6]=25;
p_exp[13][0]=27; p_exp[13][1]=22;p_exp[13][2]=10;
p_exp[13][3]=23; p_exp[13][4]=24;p_exp[13][5]=4; p_exp[13][6]=25;
p_exp[13][7]=8;
p_exp[14][0]=28; p_exp[14][1]=24;p_exp[14][2]=4;
p_exp[14][3]=25;
p_exp[15][0]=34;
p_exp[16][0]=12; p_exp[16][1]=11;
p_exp[17][0]=19; p_exp[17][1]=12;
p_exp[18][0]=34;
p_exp[19][0]=14; p_exp[19][1]=13;
p_exp[20][0]=20; p_exp[20][1]=14;p_exp[20][2]=13;
p_exp[21][0]=34;
p_exp[22][0]=16; p_exp[22][1]=15;
p_exp[23][0]=21; p_exp[23][1]=16;p_exp[23][2]=15;
p_exp[24][0]=34;
p_exp[25][0]=17;
p_exp[26][0]=18;
p_exp[27][0]=22; p_exp[27][1]=10;p_exp[27][2]=23;
p_exp[28][0]=30; p_exp[28][1]=16;
p_exp[29][0]=32;
/*预测分析表初始化*/
for(i=0;i<17;i++)
for(j=0;j<17;j++)
graph[i][j]=-1;
graph[0][0]=0;
graph[1][0]=2;graph[1][8]=3;graph[1][10]=2; graph[1][12]=2;graph[1][15]=1;
graph[2][15]=4;
graph[3][0]=6;graph[3][10]=6;graph[3][12]=6;graph[3][15]=5;
graph[4][0]=8;graph[4][10]=8;graph[4][12]=8;
graph[5][0]=9;graph[5][8]=10;graph[5][10]=9;graph[5][12]=9;
graph[6][15]=7;
graph[7][0]=11;graph[7][10]=13;graph[7][12]=12;
graph[8][0]=15;graph[8][8]=15;graph[8][10]=15;graph[8][11]=14;graph[8][12]=15;
graph[9][15]=29;
graph[10][0]=16;graph[10][1]=16; graph[10][5]=16;graph[10][13]=16;
graph[11][2]=17;graph[11][6]=18;graph[11][9]=18;
graph[12][0]=19;graph[12][1]=19;graph[12][5]=19;graph[12][13]=19;
graph[13][2]=21;graph[13][3]=20;graph[13][6]=21;graph[13][9]=21;
graph[14][0]=22;graph[14][1]=22;graph[14][5]=22;graph[14][13]=22;
graph[15][2]=24;graph[15][3]=24;graph[15][4]=23;graph[15][6]=24;graph[15][9]=24;
graph[16][0]=25;graph[16][1]=26;graph[16][5]=27;graph[16][13]=28;
}
int findword(char* words)
{
int i;
for(i=17;i<=34;i++)
if(strcmp(words,exp[i])==0)
return i;
return -1;
}
int getlength(int pnum)
{
int i=0;
for(i=0;i<=10;i++)
if( p_exp[pnum][i]==-1)
break;
return i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -