-
大小: 53KB文件類型: .zip金幣: 2下載: 1 次發(fā)布日期: 2021-08-23
- 語(yǔ)言: C/C++
- 標(biāo)簽: 語(yǔ)義分析??實(shí)驗(yàn)報(bào)告??代碼??
資源簡(jiǎn)介
用c語(yǔ)言實(shí)現(xiàn)的語(yǔ)義分析器,包括實(shí)驗(yàn)報(bào)告和代碼。

代碼片段和文件信息
#include?“stdio.h“
#include?“string.h“
char?prog[100]token[8]ch;
char?*rwtab[6]={“begin““if““then““while““do““end“};
int?synpmnsumq;
int?kk;
struct?{?char?result1[8];
????char?ag11[8];
????char?op1[8];
????char?ag21[8];
??}?quad[20];
char?*factor();
char?*expression();
int?yucu();
char?*term();
int?statement();
int?lrparser();
char?*newtemp();
scaner();
emit(char?*resultchar?*ag1char?*opchar?*ag2);
main()
{?int?j;
?q=p=kk=0;
?printf(“\nplease?input?a?string?(end?with?‘#‘):?“);
?do
???{?scanf(“%c“&ch);
?????prog[p++]=ch;
???}while(ch!=‘#‘);
?p=0;
?scaner();
?lrparser();
if(q>19)printf(“?to?long?sentense!\n“);
else?for?(j=0;jgetch();
}
int?lrparser()
??{?int?schain=0;
????kk=0;
????if?(syn==1)
??????{?scaner();
?schain=yucu();
?if(syn==6)
??{?scaner();
????if((syn==0)&&(kk==0))??printf(“Success!\n“);
??}
?else?{?if(kk!=1)printf(“short?of?‘end‘?!\n“);
????????kk=1;
????????getch();
????????exit(0);
??????}
??????}
????else?{?printf(“short?of?‘begin‘?!\n“);
????kk=1;
????getch();
????exit(0);
??}
????return?(schain);
}
int?yucu()
{?int?schain=0;
??schain=statement();
??while(syn==26)
???{?scaner();
?????schain=statement();
???}
??return?(schain);
}
int?statement()
{?char?tt[8]eplace[8];
??int?schain=0;
??if?(syn==10)
???{?strcpy(tttoken);
?????scaner();
?????if(syn==18)
???????{?scaner();
??strcpy(eplaceexpression());
??emit(tteplace““““);
??schain=0;
?}
?????else?{?printf(“short?of?sign?‘:=‘?!\n“);
?????kk=1;
?????getch();
?????exit(0);
????}
????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(eplaceterm());
??while((syn==13)||(syn==14))
???{?if?(syn==13)strcpy(tt“+“);
?????else?strcpy(tt“-“);
?????scaner();
?????strcpy(ep2term());
?????strcpy(tpnewtemp());
?????emit(tpeplacettep2);
?????strcpy(eplacetp);
???}
??return?(eplace);
}
char?*term()
{?char?*tp*ep2*eplace*tt;
??tp=(char?*)malloc(12);
??ep2=(char?*)malloc(12);
??eplace=(char?*)malloc(12);
??tt=(char?*)malloc(12);
??strcpy(eplacefactor());
??while((syn==15)||(syn==16))
???{?if?(syn==15)strcpy(tt“*“);
?????else?strcpy(tt“/“);
?????scaner();
?????strcpy(ep2factor());
?????strcpy(tpnewtemp());
?????emit(tpeplacettep2);
?????strcpy(eplacetp);
???}
??return?(eplace);
}
char?*factor()
{?char?*fplace;
??fplace=(char?*)malloc(12);
??strcpy(fplace““);
??if(syn==10)
???{?strcpy(fplacetoken);
?????scaner();
???}
??else?if(syn==11)
???{?itoa(sumfplace10);
?????scaner();
???}
??else?if(syn==27)
???{?scaner();
?????fplace=expression();
?????if(syn==28)?scaner();
?????else?{?printf(“error?on?‘)‘?!\n“);
?????kk=1;
?????getch();
?????exit(0);
???}
???}
??else?{?printf(“error?on?‘(‘?!\n“);
??
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????56573??2013-05-29?20:34??語(yǔ)義分析報(bào)告.docx
?????文件????????4785??2012-06-22?17:32??語(yǔ)義分析.c
評(píng)論
共有 條評(píng)論