編譯原理LL語(yǔ)法分析代碼及結(jié)果_第1頁(yè)
編譯原理LL語(yǔ)法分析代碼及結(jié)果_第2頁(yè)
編譯原理LL語(yǔ)法分析代碼及結(jié)果_第3頁(yè)
編譯原理LL語(yǔ)法分析代碼及結(jié)果_第4頁(yè)
編譯原理LL語(yǔ)法分析代碼及結(jié)果_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、ll語(yǔ)法分析:源代碼#include "stdio.h"include ”stdlib.h”#define maxrulenum 8#define maxvnnum 5#define maxvtnum 5#define maxstackdepth 20#define maxplength 20#define maxstlength 50struct prnode /*產(chǎn)生式右部結(jié)構(gòu)*/int rcursor;struct prnode *next;struct pnodeint lcursor;int rlength; /*右部長(zhǎng)度*/struct prnode *rhea

2、d; /*右部結(jié)點(diǎn)頭指針*/;char vnmaxvnnum+ 1;/*非終結(jié)符集*/int vnnum;char vtmaxvtnum + 1 ; /*終結(jié)符集*/int vtnum;struct pnode pmaxrulenum;int pnum;char buffermaxplength + 1 j;char ch;char stmaxstlength; /*要分析的符號(hào)串*/struct collectnodeint nvt;struct collectnode * next;struct collectnode* firstmaxvnnum + 1; /*first 集*/stru

3、ct collectnode* followfmaxvnnum + 1; /*follow 集*/int analysetablemax vn num + 1 jimaxvtnum + 1 + 1;int analysestackmaxstackdepth + 1; /*分析棧*/int topanalyse; /*分析棧頂*/void init();/*初始化*/int indexch(char ch);void inputvt();嚴(yán)輸入終結(jié)符*/void inputvn();/*輸入非終結(jié)符*/void showcharray(char* collect, int num);/*輸出 v

4、n 或 vt 的內(nèi)容*/void inputp();/*產(chǎn)生式輸入*/bool checkp(char * st);/*判斷產(chǎn)生式正確性*/void first(int u);void addfirst(int u, int nch); /*加入 first 集*/bool haveempty(int nvn);void follow(int v);/*計(jì)算 follow 集*/void addfollow(int v, int nch, int kind);void showcollect(struct collectnode *collect);/*輸出 first 或 follow 集*

5、/ void firstfollow();/*計(jì)算 first 和 follow*/void createat();/*構(gòu)造預(yù)測(cè)分析表*/void showat();/*輸 th 分析表*/void identify(char *st);void initstack();void showstack();void pop();void push(int r);void main(void)char todo,ch;lnit();inputvn();inputvt();inp utp();getchar();firstfollow();printf(”所得 first 集為:“);showcol

6、lect(first);printfc*所得 follow 集為:”);showcollect(follow);createat();showat();todo = 'y'while('y* = todo)printf(un是否繼續(xù)進(jìn)行句型分析? (y/n):u);todo = getchar();while('y' != todo && 'n' != todo)printf(hn(y / n)? “);todo = getchar();if(y = todo)int i;initstack();primf(”請(qǐng)輸入符號(hào)串

7、(以#結(jié)束):”);ch = getchar();i = 0;while('#* != ch && i < maxstlength)iff 1 != ch && w !=ch)sti+ = ch;)ch = getchar();if(# = ch && i < maxstlength)stij = ch;identify(st);elseprintf(”輸入出錯(cuò)! nm);getchar();int ij;vnnum = 0;vtnum = 0;pnum = 0;for(i = 0; i <= maxvnnum; i+)

8、vni = '0'for(i = 0; i <= maxvtnum; i+)vti =、(r;for(i = 0; i < maxrulenum; i+)pi.lcursor = null;pij.rhead = null;pi.rlength = 0;pnum = 0;for(i = 0; i <= maxplength; i+) bufferi = '0*;for(i = 0; i < maxvnnum; i+)firstil = null;followi = null;for(i = 0; i <= maxvnnum; i+)for(

9、j = 0; j <= maxvnnum + 1; j+) analysetableij = - i;int indexch(char ch)int n;n = 0; /*is vn?*/while(ch != vnn && '0 != vnn) n+;if('0 != vnn)return 100 + n;n = 0; /*is vt?*/while(ch != vtn &&!= v") n+;if('0' != vtn)return n;return -1;/*輸出vn或vt的內(nèi)容*/void showchar

10、ray(char* collect)int k = 0;while('0' != collectk)printf(" %c ”, collectk+);printf(hnh);嚴(yán)輸入非終結(jié)符*/void lnputvn()int inerr = 1;int n,k;char ch;while(inerr)printf(un請(qǐng)輸入所有的非終結(jié)符,注意:“);printf(h請(qǐng)將開始符放在第一位,并以#號(hào)結(jié)束:n“);ch 二';n = 0;/*初始化數(shù)組*/while(n < maxvnnum)vnn+二 0;)n = 0;while('# !=

11、ch) && (n < maxvnnum)if('' != ch && 'n* != ch && -1 = indexch(ch)vnn+ = ch;vnnum+;ch = getchar();vnn二#; /*以“#“標(biāo)志結(jié)束用于判斷長(zhǎng)度是否合法*/ k = n;if('#' != ch)if( != (ch = getchar()while(# != (ch = getchar()printf(mn符號(hào)數(shù)目超過(guò)限制! nh);inerr = 1;continue;)/*正確性確認(rèn),正確則,執(zhí)行下下

12、面,否則重新輸入*/vnk二'0:showcharray(vn);ch =1 ;while('y' != ch && 'n' != ch)ifcn* != ch)printf(”輸入正確確認(rèn)?(y/n):");scanf("%c", &ch);if('n' = ch)printf(h錄入錯(cuò)誤重新輸入! n");inerr = 1;elseinerr = 0;/*輸入終結(jié)符*/void inputvt()int inerr = 1;int n,k;char ch;while(i

13、nerr)printf(mn請(qǐng)輸入所有的終結(jié)符,注意:“); print® #號(hào)結(jié)束:n“);ch 二';n = 0;/*初始化數(shù)組*/while(n < maxvtnum)vtn+ "o'n = 0;while(c#' != ch) && (n < maxvtnum)iff * != ch && xn* != ch && -1 = indexch(ch) vtn+ = ch;vtnum+;ch = getchar();vtln = #*;k = n;if(# != ch)if( # !=

14、(ch = getchar()while('#' != (ch = getchar()printf("n符號(hào)數(shù)目超過(guò)限制! n");in err = 1;continue;vtk = w;showcharray(vt);ch ='whilecy* != ch && *n' != ch)if('n' != ch)printf(”輸入正確確認(rèn)?(y/n):h);scanf("%c", &ch);if('n' = ch)printf(h錄入錯(cuò)誤重新輸入! nh);inerr

15、 = 1;elseinerr = 0;/*產(chǎn)生式輸入*/void inputp()char ch;int i = (), n.num;printf(h請(qǐng)輸入文法產(chǎn)生式的個(gè)數(shù):“);scanf(h%d", &num);pnum = num;getchar(); /*消除回車符*/n, num);printfc'xn請(qǐng)輸入文法的d個(gè)產(chǎn)生式,并以回車分隔每個(gè)產(chǎn)生式: printf(hnn);while(i < num)printf(”第d 個(gè):”,i);/*初始化*/for(n =0; n < maxplenglh; n+)buffern = '0

16、9;嚴(yán)輸入產(chǎn)生式串*/ch ='n = 0;while('n' != (ch = getchar() && n < maxplength)iff 1 != ch)buffern+ = ch;buffern二'0:if(checkp(buffer)prnode *pt, *qt;pi.lcursor = indexch(buffero); pt = (prnode*)malloc(sizeof(prnode); pt->rcursor = indexch(buffer3); pt->next = null;pfij.rhead =

17、 pt;n = 4;while('0' != buffern)qt = (prnode*)malloc(sizeof(prnode); qt->rcursor = indexch(buffer n); qt->next = null;pt->next = qt;pt = qt;n+;pi.rlength 二 n 3;i+;elseprintf(”輸入符號(hào)含非法在成分,請(qǐng)重新輸入! nu);/寺判斷產(chǎn)生式正確性*/bool checkp(char * st)int n;if(10() > indexch(sto)return false;if(,j!=st

18、l)return false;ifc>f != st2)return false;for(n = 3; *0' != stn; n +) if(-l = indexch(stn) return false;return true;1void first(int u)int i,j;for(i = 0; i < pnum; i+)if(pi.lcursor = u)struct prnode* pt; pt = pi.rhead;j = 0;while(j < pi.rlenglh)if(100 > pt->rcursor)addfirst(u, pt-&g

19、t;rcursor);break;)elseif(null = firstrpt->rcursor 100) first(pt->rcursor);addfirst(u, pt->rcursor);if(! haveempty(pt->i*cursor)break;elsept = pt->next;if(j >= pi.rlength) /*當(dāng)產(chǎn)生式右部都能推出空時(shí)*/ addfirst(u,-l);/*加入first集*/void addfirst(int u, int nch)struct collectnode *pt, *qt; int ch; /

20、*用于處理vn*/ pt = null;qt = null;if(nch < 100)pl = firstfu - 1001; vvhile(null != pt) if(pt->nvt = nch) break;elseqt = pt;pt = pt->next;)if(null = pt)pt = (struct collectnode *)malloc(sizeof(struct collectnode); pt->nvt = nch;pt->next = null;if(null = firstl u 100)firstru 100 = pt;elseqt

21、->next = pt; /*qt指向first集的最后一個(gè)元素*/pt = pt->next;elsept = firstnch - 100;while(null != pt)ch = pt->nvt;if(-l !=ch)addfirst(u, ch);pt = pt->next; bool haveempty(int nvn) if(nvn < 100)retui*n false;struct collectnode *pt;pt = firstfnvn - 100; while(null != pt) if(-l = pt->nvt) return

22、true;pt = pt->next;return false;void follow(int v)inti;struct prnode *pt;if(100 = v) /*當(dāng)為初始符時(shí)*/addfollow(v, -1,0);for(i = 0; i < pnum; i+)pt = pi.rhead;while(null != pt && pt->rcursor != v)pt = pt->next;if(null != pt)pt = pt->n ext;if(null = pt)if(null = followpi.lcursor - 100

23、 && pi.icursor != v) follow(pi.lcursor);addfollow(v, pi.lcursor, 0);elsewhile(null != pt && haveempty(pt>rcursor)addfollow(v» pt->rcursor, 1);pt = pt->next;if(null = pt)if(null = followpi.lcursor- 100 && pi.lcursor != v)follow(pi.lcursor);addfollow(v, plij.lcurs

24、or, 0);addfollow(y pt->rcursor, 1);) void addfollow(int v, int nch, int kind)struct coll ectnode *pt, *qt;int ch;pt = null;qt = null;if(nch < 100) /*為終結(jié)符時(shí)*/pt = followv - 100;while(null != pt)if(pt->nvt = nch)break;elseqt = pt;pt = pt->next;if(null = pt)pt = (struct collectnode *)malloc(

25、sizeof(struct collectnode);pt->nvt = nch;pt->next = null;if(null = followv 100)followfv- 100 = pt;elseqt->next = pl; /*qt指向follow集的最后一個(gè)元素*/ pt = pt-> next;elseif(0 = kind)pt = follownch 100;while(null != pt)ch = pt->nvt;addfollow(v, ch, 0);pt = pt->next;elsept = firstnch 100;while(

26、null != pt)ch = pt->nvt;if(-l != ch)addfollow(v, ch, 1);pt = pt->next;/*輸出 first 或 follow 集*/void showcollect(struct collectnode *collect) inti;struct collectnode *pt;i = 0;while(null != collectij)pt = collecti;printf(nn%c:tu, vni);while(null != pt)if(-l != pt->nvt)printf(m %c”, vtlpt->n

27、vtj);elseprintf(n #“);pt = pt->next;)i+;printf(hnh);/*計(jì)算 first 和 follow*/void firstfollow()int i;i = o;whuecxo* != vni)if(null = firsti)first(l()o + i);i+;i = o;whilecxo* != vni)if(null = followi)follow(100 + i);i+;/嚀勾造預(yù)測(cè)分析表*/void createat()int i;struct prnode *pt;struct collectnode *ct;for(i = 0

28、; i < pnum; i+)pt = p|i.rhead;while(null != pt && haveempty(pj>rcursor)ct = firstlpt->rcursor 100;while(null!=ct)if(-l != ct->nvt)analysetablepi.lcursor - 100ct->nvt = i;ct = ct->next;pt = pt->next;if(null = pt)ct = followpi.lcursor - 100;while(null!=ct)if(-l != ct->n

29、vt)analysetablefpi.lcursor 10()ct>nvt| = i; elseanalysetablepi.lcursor - 100vtnum = i; ct = ct-> next;elseif(100 <= pt->rcursor) /*不含空的非終結(jié)符勺 ct = firstfpt->rcursor - 10();while(null != ct)analysetablepi.lcursor - 100ct->nvt = i;ct = ct->next;else /*終結(jié)符或者空勺if(l = pt->rcursor)c

30、t = followpi.lcursor - 100;while(null!=ct)if(-l != ct->nvt)analysetablepi.lcursor - 100ct->nvt = i;else /*當(dāng)含有#號(hào)時(shí)*/analysetablepi.lcursor- 100vtnum = i; ct = ct->next;else /*為終結(jié)符*/analysetablepi.lcursor - 100pt->rcursor = i;)/*輸岀分析表*/void showat()int i,j;printf("構(gòu)造預(yù)測(cè)分析表如下:n”);printf(

31、” t|t”);for(i = 0; i< vtnum; i+)printf(”ct”, vti); printf(h#tnh);printf(h- - -t|- -for(i = 0; i <= vtnum; i+)printfc'- - -tn);printf(hnn);for(i = 0; i < vnnum; i+)printf(n%ct|tm, vnfil);for(j = 0; j <= vtnum; j+)if(-l != analysetableij)printf(hr(%d)th, analysetableij);elseprintf(&quo

32、t;errort");printf(,nm);void identify(char *st)int current,step,r; /*r表使用的產(chǎn)生式的序號(hào)*/ printf("n%s 的分析過(guò)程:n", st);printfc*步驟t分析符號(hào)棧t當(dāng)前指示字符t使用產(chǎn)生式序號(hào)n“);step = 0;current = 0;printf(" %dt" ,step);showstack();printf("tt%ctt- -nu, stcurrentj);while(# != stfcurrent)if(100 > analys

33、estacktopanalyse)if(analysestacktopanalyse = indexch(stcurrent)popo;current+;step+;printf(n%dtn, step);showstack();printf("tt%ctt 出棧、后移n", stcurrent);elseprintf(n%c-%c 不匹配!役 analysestacktopanalyse, stcurrent); printf(m此串不是此文法的句子! nm);return;else /*當(dāng)為非終結(jié)符時(shí)*/r = analysetableanalysestacktopan

34、alysej 100 indexch(stcurrent);if(-l !=r)push(r);step+;printf(”dt", step);showstack();printf("tt%ctt%dn", stfcurrent, r);printf(n此串不是此文法的句子! nh); return;if(# = stfcurrent)if(o = topanalyse && # = stcurrent)step+;printf(h%dt", step);showstack();printf("tt%ctt 分析成功! n", stcurrent); printf(h%s是給定文法的句子! n st);elsewhile(topanalyse > 0)if(100 > analysestackftopanalyse)printf(”此串不是此文法的句子! n“);return;elser = analysetableanalysestacktopanalyse - 100vtnum; if(-l !=r)push(r); /*產(chǎn)生式右部代替左部,指示器不移動(dòng)*/ step+;printf("

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論