资源描述:
《北邮-语法分析实验报告》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、编译原理语法分析实验报告by坏学长otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,
2、hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand一、实验题目和要求题目:语法分析程序的设计与实现。实验内容:编写语法分析程序,实现对算术表达式的语法分析。要求所分析算术表达式由如下的文法
3、产生。实验要求:在对输入表达式进行分析的过程中,输出所采用的产生式。方法1:编写递归调用程序实现自顶向下的分析。方法2:编写LL(1)语法分析程序,要求如下。(1)编程实现算法4.2,为给定文法自动构造预测分析表。(2)编程实现算法4.1,构造LL(1)预测分析程序。方法3:编写语法分析程序实现自底向上的分析,要求如下。(1)构造识别所有活前缀的DFA。(2)构造LR分析表。(3)编程实现算法4.3,构造LR分析程序。方法4:利用YACC自动生成语法分析程序,调用LEX自动生成的词法分析程序。二、程序设计及步骤:此次实验使
4、用第一种方法:递归调用预测分析1.提取左公因子及消除左递归:E->TE'';E''->E'E''E''->εE'->+TE'->-TT->FT''T''->T'T''otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganiz
5、ation,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,MinistersandT''
6、->ε''T'->*FT'->/F''F->idE->(E)E->num1.做状态转换图并化简:otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitte
7、eAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommitteeSecretary,Ministersand1.程序实现分析根据上图产生式,读入字符串并以$结尾,根据字符串情况分别采取
8、不同的过程,为每个过程编写子程序,并返回是否成功,1代表成功,-1代表失败,当字符串格式错误时,最终E过程返回-1,不能被接收。读取一个字符,根据状态转换图的自动机进行转换状态,并判断是否正确并读取下一个字符。直到读取$,过程未产生错误(返回值为1),则表达式被接收,语法分析结束。2.源代码:#incl