📄 bianyiyuanli.txt
字号:
#include"stdio.h"
#include"string.h"
char prog[80],token[8];
char ch;
int syn,p,m,n,sum,kk,q;
char *rwtab[6]={"begin","if","then","while","do","end"};
struct { char result[8];
char ag1[8];
char op[8];
char ag2[8];
} quad[20];
scaner();
int lrparser();
int statement();
char *expression();
char *tern();
char *factor();
char *newtemp();
emit(char *result,char *ag1,char *op,char *ag2);
main()
{ int j;
p=0;
printf("\n please input string : \n");
do{ scanf("%c",&ch);
prog[p++]=ch;
}while(ch!='#');
p=0;
kk=0;
q=0;
scaner();
lrparser();
if(q>19)
printf(" to long sentense!\n");
else
for(j=0;j<q;j++)
printf(" %s = %s %s %s \n\n",quad[j].result,quad[j].ag1,quad[j].op,quad[j].ag2);
getch();
}
scaner()
{
m=0;
sum=0;
for(n=0;n<8;n++)token[n]=NULL;
ch=prog[p++];
while(ch==' ') ch=prog[p++];
if(ch>='a'&&ch<='z' || ch>='A'&&ch<='Z')
{while(ch>='a'&&ch<='z' || ch>='A'&&ch<='Z' || ch>='0'&&ch<='9')
{ token[m++]=ch;
ch=prog[p++];
}
token[m++]='\0';p--;syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0)
{ syn=n+1;
break;
}
}
else
if(ch>='0'&&ch<='9')
{ while(ch>='0'&&ch<='9')
{ sum=sum*10+ch-'0';
ch=prog[p++];
}
p--;syn=11;
}
else
switch(ch)
{
case'<':token[m++]=ch;
ch=prog[p++];
if(ch=='>')
{ syn=21;
token[m++]=ch;
}
else if(ch=='=')
{ syn=22;
token[m++]=ch;
}
else
{ syn=20;p--;}
break;
case'>':token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{ syn=24;
token[m++]=ch;
}
else{syn=23;p--;}
break;
case':':token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{ syn=18;
token[m++]=ch;
}
else
{ syn=17;
p--;
}
break;
case'+':syn=13;token[m]=ch;break;
case'-':syn=14;token[m]=ch;break;
case'*':syn=15;token[m]=ch;break;
case'/':syn=16;token[m]=ch;break;
case';':syn=26;token[m]=ch;break;
case'(':syn=27;token[m]=ch;break;
case')':syn=28;token[m]=ch;break;
case'#':syn=0;token[m]=ch;break;
default:syn=-1;
}
}
int lrparser()
{ int schain=0;
kk=0;
if(syn==5)
{ scaner();
statement();
if(syn==4)
{ scaner();
expression();
if(syn!=0 && kk!=0)
printf("success");
}
else if(kk!=1)
{ printf(" 'while' error ");
kk=1;}
}
else{ printf(" 'do' error ");
kk=1;}
return(schain);
}
int statement()
{ char tt[8],eplace[8];
int schain=0;
if(syn==10)
{ strcpy(tt,token);
scaner();
if(syn==18)
{ scaner();
strcpy(eplace,expression());
emit(tt,eplace,"","");
schain=0;
}
else{ printf(" ':=' error ");
kk=1;}
}
else{ printf("ID error");
kk=1;}
return(schain);
}
char *expression()
{ char *tp,*ep2,*eplace,*tt;
tp=(char *)malloc(12);
ep2=(char *)malloc(12);
eplace=(char *)malloc(12);
tt=(char *)malloc(12);
strcpy(eplace,tern());
while(syn==13 || syn==14)
{ if (syn==13)strcpy(tt,"+");
else strcpy(tt,"-");
scaner();
strcpy(ep2,tern());
strcpy(tp,newtemp());
emit(tp,eplace,tt,ep2);
strcpy(eplace,tp);
}
return(eplace);
}
char *tern()
{ char *tp,*ep2,*eplace,*tt;
tp=(char *)malloc(12);
ep2=(char *)malloc(12);
eplace=(char *)malloc(12);
tt=(char *)malloc(12);
strcpy(eplace,factor());
while(syn==15 || syn==16)
{ if (syn==15)strcpy(tt,"*");
else strcpy(tt,"/");
scaner();
strcpy(ep2,factor());
strcpy(tp,newtemp());
emit(tp,eplace,tt,ep2);
strcpy(eplace,tp);
}
return(eplace);
}
char *factor()
{ char *fplace;
fplace=(char *)malloc(12);
strcpy(fplace," ");
if(syn==10)
{ strcpy(fplace,token);
scaner();}
else if(syn==11)
{ itoa(sum,fplace,10);
scaner();}
else if(syn==27)
{ scaner();
fplace=expression();
if(syn==28)
scaner();
else{ printf(" ')' error ");
kk=1;}
}
else{ printf(" '(' error");
kk=1;}
return(fplace);
}
char *newtemp()
{ char *p;
char m[8];
p=(char *)malloc(8);
kk++;
itoa(kk,m,10);
strcpy(p+1,m);
p[0]='t';
return(p);
}
emit(char *result,char *ag1,char *op,char *ag2)
{
strcpy(quad[q].result,result);
strcpy(quad[q].ag1,ag1);
strcpy(quad[q].op,op);
strcpy(quad[q].ag2,ag2);
q++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -