资源描述:
《编译原理语义分析实验报告——免费!》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、语义分析实验报告一、实验目的:通过上机实习,加深对语法制导翻译原理的理解,掌握将语法分析所识别的语法成分变换为中间代码的语义翻译方法。二、实验要求:采用递归下降语法制导翻译法,对算术表达式、赋值语句进行语义分析并生成四元式序列。三、算法思想:1、设置语义过程。(1)emit(char*result,char*ag1,char*op,char*ag2)该函数的功能是生成一个三地址语句送到四元式表中。四元式表的结构如下:struct{charresult[8];charag1[8];charop[8];charag2[8];}quad[20];
2、(2)char*newtemp()该函数回送一个新的临时变量名,临时变量名产生的顺序为T1,T2,…char*newtemp(void){char*p;charm[8];p=(char*)malloc(8);k++;itoa(k,m,10);strcpy(p+1,m);p[0]=’t’;return(p);}2、函数lrparser在原来语法分析的基础上插入相应的语义动作:将输入串翻译成四元式序列。在实验中我们只对表达式、赋值语句进行翻译。otherstaffoftheCentre.Duringthewar,Zhuwastransferre
3、dbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,Shenmufu
4、guSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand一、源程序代码:#include#include#include#includestruct{charresult[12];charag1[12];charop[12];charag2[12];}quad;charprog[80
5、],token[12];charch;intsyn,p,m=0,n,sum=0,kk;//p是缓冲区prog的指针,m是token的指针char*rwtab[6]={"begin","if","then","while","do","end"};voidscaner();char*factor(void);char*term(void);char*expression(void);intyucu();voidemit(char*result,char*ag1,char*op,char*ag2);char*newtemp();intstate
6、ment();intk=0;voidemit(char*result,char*ag1,char*op,char*ag2){strcpy(quad.result,result);strcpy(quad.ag1,ag1);strcpy(quad.op,op);strcpy(quad.ag2,ag2);otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSec
7、retary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecordero
8、ftheCountypartyCommitteeSecretary,Ministersandcout<