资源描述:
《北邮《语法分析程序》实验报告.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、语法分析程序实验报告需求分析题目:语法分析程序的设计与实现。实验内容:编写语法分析程序,实现对算术表达式的语法分析。要求所分析算术表达式由如下的文法产生。实验要求:在对输入表达式进行分析的过程中,输出所采用的产生式。方法1:编写LL(1)语法分析程序,要求如下。(1)编程实现算法4.2,为给定文法自动构造预测分析表。(2)编程实现算法4.1,构造LL(1)预测分析程序。方法2:编写语法分析程序实现自底向上的分析,要求如下。(1)构造识别所有活前缀的DFA。(2)构造LR分析表。(3)编程实现算法4.3,构造LR分析程序。概要设计1.LL(1)
2、语法分析程序(1).原文法(2).消除左递归和回溯E->TRR->+TRR->-TRR->eT->FWW->*FWW->/FWW->eF->idF->(E)F->num(3).FISRT集和FOLLOW集FIRST(S)={id,num,(}FOLLOW(S)={$}FIRST(E)={id,num,(}FOLLOW(E)={$,+,-,)}FIRST(T)={id,num,(}FOLLOW(T)={$,+,-,*,/,)}FIRST(F)={id,num,(}FOLLOW(F)={$,+,-,*,/,)}(4).LL(1)预测分析表2.LR
3、实现,自底向上分析(1).原文法(2).文法对应的拓广文法为:1E->E+T2E->E-T3E->T4T->T*F5T->T/F6T->F7F->(E)8F->id9F->num(3).FISRT集和FOLLOW集FIRST(S)={id,num,(}FOLLOW(S)={$}FIRST(E)={id,num,(}FOLLOW(E)={$,+,-,)}FIRST(T)={id,num,(}FOLLOW(T)={$,+,-,*,/,)}FIRST(F)={id,num,(}FOLLOW(F)={$,+,-,*,/,)}(4).构造项目集规范族I
4、0=closure({S->·E})={S->·E,E->·E+T,E->·E-T,E->·T,T->·T*F,T->·T/F,T->·F,F->·id,F->·(E),F->·num};从I0出发:I1=go(I0,E)=closure({S->E·,E->E·+T,E->E·-T})={S->E·,E->E·+T,E->E·-T};I2=go(I0,T)=closure({E->T·,T->T·*F,T->T·/F})={E->T·,T->T·*F,T->T·/F};I3=go(I0,F)=closure({T->F·})={T->F·}
5、;I4=go(I0,id)=closure({F->id·})={F->id·};I5=go(I0,()=closure({F->(·E)})={F->(·E),E->·E+T,E->·E-T,E->·T,T->·T*F,T->·T/F,T->·F,F->·id,F->·(E),F->·num};I6=go(I0,num)=closure({F->num·})={F->num·};从I1出发:I7=go(I1,+)=closure({E->E+·T})={E->E+·T,T->·T*F,T->·T/F,T->·F,F->·id,F->·(E)
6、,F->·num};I8=go(I1,-)=closure({E->E-·T})={E->E-·T,T->·T*F,T->·T/F,T->·F,F->·id,F->·(E),F->·num};从I2出发:I9=go(I2,*)=closure({T->T*·F})={T->T*·F,F->·id,F->·(E),F->·num};I10=go(I2,/)=closure({T->T/·F})={T->T/·F,F->·id,F->(E),F->·num};从I5出发:I11=go(I5,E)=closure({F->(E·),E->E·+T,
7、E->E·-T})={F->(E·),E->E·+T,E->E·-T};从I7出发:I12=go(I7,T)=closure({E->E+T·,T->T·*F,T->T·/F})={E->E+T·,T->T·*F,T->T·/F};从I8出发:I13=go(I8,T)=closure({E->E-T·,T->T·*F,T->T·/F})={E->E-T·,T->T·*F,T->T·/F};从I9出发:I14=go(I9,F)=closure({T->T*F·})={T->T*F·};从I10出发:I15=go(I10,F)=closure({
8、T->T/F·})={T->T/F·};从I11出发:I16=go(I11,))=closure({F->(E)·})={F->(E)·};LR分析表如下:goto