版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、LL(1)文法(源代碼)#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 pNode int lCursor; int rLength; /*右部長度*/ struct pRNode *rHead; /*右
2、部結(jié)點頭指針*/;char VnMaxVnNum + 1; /*非終結(jié)符集*/int vnNum;char VtMaxVtNum + 1; /*終結(jié)符集*/int vtNum;struct pNode PMaxRuleNum; int PNum; char bufferMaxPLength + 1;char ch; char stMaxStLength; /*要分析的符號串*/struct collectNode int nVt; struct collectNode *next;struct collectNode* firstMaxVnNum + 1; /*first集*/struct c
3、ollectNode* followMaxVnNum + 1; /*follow集*/int analyseTableMaxVnNum + 1MaxVtNum + 1 + 1;int analyseStackMaxStackDepth + 1; /*分析棧*/int topAnalyse; /*分析棧頂*/void Init();/*初始化*/int IndexCh(char ch);void InputVt(); /*輸入終結(jié)符*/void InputVn();/*輸入非終結(jié)符*/void ShowChArray(char* collect, int num);/*輸出Vn或Vt的內(nèi)容*/v
4、oid 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);/*計算follow集*/void AddFollow(int V, int nCh, int kind);void ShowCollect(struct collectNode *collect);/*輸出first或follow集*/void FirstFollow();
5、/*計算first和follow*/void CreateAT();/*構(gòu)造預(yù)測分析表*/void ShowAT();/*輸出分析表*/void Identify(char *st);void InitStack();void ShowStack();void Pop();void Push(int r);void main(void) char todo,ch; Init(); InputVn(); InputVt(); InputP(); getchar(); FirstFollow(); printf(所得first集為:); ShowCollect(first); printf(所得f
6、ollow集為:); ShowCollect(follow); CreateAT(); ShowAT(); todo = y; while(y = todo) printf(n是否繼續(xù)進行句型分析?(y / n):); todo = getchar(); while(y != todo & n != todo) printf(n(y / n)? ); todo = getchar(); if(y = todo) int i; InitStack(); printf(請輸入符號串(以#結(jié)束) : ); ch = getchar(); i = 0; while(# != ch & i MaxStL
7、ength) if( != ch & n != ch) sti+ = ch; ch = getchar(); if(# = ch & i MaxStLength) sti = ch; Identify(st); else printf(輸入出錯!n); getchar();void Init() int i,j; vnNum = 0; vtNum = 0; PNum = 0; for(i = 0; i = MaxVnNum; i+) Vni = 0; for(i = 0; i = MaxVtNum; i+) Vti = 0; for(i = 0; i MaxRuleNum; i+) Pi.lC
8、ursor = NULL; Pi.rHead = NULL; Pi.rLength = 0; PNum = 0; for(i = 0; i = MaxPLength; i+) bufferi = 0; for(i = 0; i MaxVnNum; i+) firsti = NULL; followi = NULL; for(i = 0; i = MaxVnNum; i+) for(j = 0; j = MaxVnNum + 1; j+) analyseTableij = -1; int IndexCh(char ch) int n; n = 0; /*is Vn?*/ while(ch !=
9、Vnn & 0 != Vnn) n+; if(0 != Vnn) return 100 + n; n = 0; /*is Vt?*/ while(ch != Vtn & 0 != Vtn) n+; if(0 != Vtn) return n; return -1;/*輸出Vn或Vt的內(nèi)容*/void ShowChArray(char* collect) int k = 0; while(0 != collectk) printf( %c , collectk+); printf(n);/*輸入非終結(jié)符*/void InputVn() int inErr = 1; int n,k; char c
10、h; while(inErr) printf(n請輸入所有的非終結(jié)符,注意:); printf(請將開始符放在第一位,并以#號結(jié)束:n); ch = ; n = 0; /*初始化數(shù)組*/ while(n MaxVnNum) Vnn+ = 0; n = 0; while(# != ch) & (n MaxVnNum) if( != ch & n != ch & -1 = IndexCh(ch) Vnn+ = ch; vnNum+; ch = getchar(); Vnn = #; /*以“#”標(biāo)志結(jié)束用于判斷長度是否合法*/ k = n; if(# != ch) if( # != (ch = g
11、etchar() while(# != (ch = getchar() ; printf(n符號數(shù)目超過限制!n); inErr = 1; continue; /*正確性確認,正確則,執(zhí)行下下面,否則重新輸入*/ Vnk = 0; ShowChArray(Vn); ch = ; while(y != ch & n != ch) if(n != ch) printf(輸入正確確認?(y/n):); scanf(%c, &ch); if(n = ch) printf(錄入錯誤重新輸入!n); inErr = 1; else inErr = 0; /*輸入終結(jié)符*/void InputVt() in
12、t inErr = 1; int n,k; char ch; while(inErr) printf(n請輸入所有的終結(jié)符,注意:); printf(以#號結(jié)束:n); ch = ; n = 0; /*初始化數(shù)組*/ while(n MaxVtNum) Vtn+ = 0; n = 0; while(# != ch) & (n MaxVtNum) if( != ch & n != ch & -1 = IndexCh(ch) Vtn+ = ch; vtNum+; ch = getchar(); Vtn = #; k = n; if(# != ch) if( # != (ch = getchar()
13、 while(# != (ch = getchar() ; printf(n符號數(shù)目超過限制!n); inErr = 1; continue; Vtk = 0; ShowChArray(Vt); ch = ; while(y != ch & n != ch) if(n != ch) printf(輸入正確確認?(y/n):); scanf(%c, &ch); if(n = ch) printf(錄入錯誤重新輸入!n); inErr = 1; else inErr = 0; /*產(chǎn)生式輸入*/void InputP() char ch; int i = 0, n,num; printf(請輸入文
14、法產(chǎn)生式的個數(shù):); scanf(%d, &num); PNum = num; getchar(); /*消除回車符*/ printf(n請輸入文法的%d個產(chǎn)生式,并以回車分隔每個產(chǎn)生式:, num); printf(n); while(i num) printf(第%d個:, i); /*初始化*/ for(n =0; n MaxPLength; n+) buffern = 0; /*輸入產(chǎn)生式串*/ ch = ; n = 0; while(n != (ch = getchar() & n rCursor = IndexCh(buffer3); pt-next = NULL; Pi.rHea
15、d = pt; n = 4; while(0 != buffern) qt = (pRNode*)malloc(sizeof(pRNode); qt-rCursor = IndexCh(buffern); qt-next = NULL; pt-next = qt; pt = qt; n+; Pi.rLength = n - 3; i+; else printf(輸入符號含非法在成分,請重新輸入!n); /*判斷產(chǎn)生式正確性*/bool CheckP(char * st) int n; if(100 IndexCh(st0) return false; if(- != st1) return f
16、alse; if( != st2) return false; for(n = 3; 0 != stn; n +) if(-1 = IndexCh(stn) return false; return true;void 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 pt-rCursor) AddFirst(U, pt-rCursor); break; else if(NULL = firstpt-rCursor -
17、100) First(pt-rCursor); AddFirst(U, pt-rCursor); if(!HaveEmpty(pt-rCursor) break; else pt = pt-next; j+; if(j = Pi.rLength) /*當(dāng)產(chǎn)生式右部都能推出空時*/ AddFirst(U, -1); /*加入first集*/void AddFirst(int U, int nCh) struct collectNode *pt, *qt; int ch; /*用于處理Vn*/ pt = NULL; qt = NULL; if(nCh nVt = nCh) break; else
18、qt = pt; pt = pt-next; if(NULL = pt) pt = (struct collectNode *)malloc(sizeof(struct collectNode); pt-nVt = nCh; pt-next = NULL; if(NULL = firstU - 100) firstU - 100 = pt; else qt-next = pt; /*qt指向first集的最后一個元素*/ pt = pt-next; else pt = firstnCh - 100; while(NULL != pt) ch = pt-nVt; if(-1 != ch) Add
19、First(U, ch); pt = pt-next; bool HaveEmpty(int nVn) if(nVn nVt) return true; pt = pt-next; return false;void Follow(int V) int i; struct pRNode *pt ; if(100 = V) /*當(dāng)為初始符時*/ AddFollow(V, -1, 0 ); for(i = 0; i rCursor != V) pt = pt-next; if(NULL != pt) pt = pt-next; if(NULL = pt) if(NULL = followPi.lC
20、ursor - 100 & Pi.lCursor != V) Follow(Pi.lCursor); AddFollow(V, Pi.lCursor, 0); else while(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, Pi.lCursor, 0); else AddFollow(V
21、, pt-rCursor, 1); void AddFollow(int V, int nCh, int kind) struct collectNode *pt, *qt; int ch; pt = NULL; qt = NULL; if(nCh nVt = nCh) break; else qt = pt; pt = pt-next; if(NULL = pt) pt = (struct collectNode *)malloc(sizeof(struct collectNode); pt-nVt = nCh; pt-next = NULL; if(NULL = followV - 100
22、) followV - 100 = pt; else qt-next = pt; /*qt指向follow集的最后一個元素*/ pt = pt-next; else if(0 = kind) pt = follownCh - 100; while(NULL != pt) ch = pt-nVt; AddFollow(V, ch, 0); pt = pt-next; else pt = firstnCh - 100; while(NULL != pt) ch = pt-nVt; if(-1 != ch) AddFollow(V, ch, 1); pt = pt-next; /*輸出first或f
23、ollow集*/void ShowCollect(struct collectNode *collect) int i; struct collectNode *pt; i = 0; while(NULL != collecti) pt = collecti; printf(n%c:t, Vni); while(NULL != pt) if(-1 != pt-nVt) printf( %c, Vtpt-nVt); else printf( #); pt = pt-next; i+; printf(n);/*計算first和follow*/void FirstFollow() int i; i
24、= 0; while(0 != Vni) if(NULL = firsti) First(100 + i); i+; i = 0; while(0 != Vni) if(NULL = followi) Follow(100 + i); i+; /*構(gòu)造預(yù)測分析表*/void CreateAT() int i; struct pRNode *pt; struct collectNode *ct; for(i = 0; i rCursor) ct = firstpt-rCursor - 100; while(NULL != ct) if(-1 != ct-nVt) analyseTablePi.l
25、Cursor - 100ct-nVt = i; ct = ct-next; pt = pt-next; if(NULL = pt) ct = followPi.lCursor - 100; while(NULL != ct) if(-1 != ct-nVt) analyseTablePi.lCursor - 100ct-nVt = i; else analyseTablePi.lCursor - 100vtNum = i; ct = ct-next; else if(100 rCursor) /*不含空的非終結(jié)符*/ ct = firstpt-rCursor - 100; while(NULL
26、 != ct) analyseTablePi.lCursor - 100ct-nVt = i; ct = ct-next; else /*終結(jié)符或者空*/ if(-1 = pt-rCursor) ct = followPi.lCursor - 100; while(NULL != ct) if(-1 != ct-nVt) analyseTablePi.lCursor - 100ct-nVt = i; else /*當(dāng)含有#號時*/ analyseTablePi.lCursor - 100vtNum = i; ct = ct-next; else /*為終結(jié)符*/ analyseTablePi.
27、lCursor - 100pt-rCursor = i; /*輸出分析表*/void ShowAT() int i,j; printf(構(gòu)造預(yù)測分析表如下:n); printf(t|t); for(i = 0; i vtNum; i+) printf(%ct, Vti); printf(#tn); printf(- - -t|- - -t); for(i = 0; i = vtNum; i+) printf(- - -t); printf(n); for(i = 0; i vnNum; i+) printf(%ct|t, Vni); for(j = 0; j analyseStacktopAn
28、alyse) if(analyseStacktopAnalyse = IndexCh(stcurrent) Pop(); current+; step+; printf(%dt, step); ShowStack(); printf(tt%ctt出棧、后移n, stcurrent); else printf(%c-%c不匹配!, analyseStacktopAnalyse, stcurrent); printf(此串不是此文法的句子!n); return; else /*當(dāng)為非終結(jié)符時*/ r = analyseTableanalyseStacktopAnalyse - 100IndexCh(stcurrent); if(-1 != r) Push(r); step+; printf(%dt, step); ShowStac
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年極性微晶玻璃項目發(fā)展計劃
- 保護環(huán)境的宣傳語
- 數(shù)學(xué)模擬試卷:常見數(shù)學(xué)問題的解決方法與步驟
- 福建省廈門市雙十中學(xué)2024-2025學(xué)年九上數(shù)學(xué)開學(xué)聯(lián)考試題【含答案】
- 小學(xué)健康故事模擬試卷
- 大學(xué)生青春十九演講稿
- 青少年對文化多元化和國際交流的關(guān)注和參與
- 小學(xué)學(xué)科探索模擬試卷
- 小學(xué)校園安全教育模擬試卷
- 2024年法律知識法治建設(shè)知識競賽-出入境管理法考試近5年真題集錦(頻考類試題)帶答案
- 美術(shù)教師招聘考試試題及答案+教師招聘考試試題及答案大全
- 山東省第五屆財會知識大賽試題及答案
- 初中《學(xué)憲法講憲法》第八個國家憲法日主題教育課件
- GB/T 44394-2024化學(xué)品粉塵爆炸危害識別和防護指南
- 基礎(chǔ)模塊 1 Unit 3 Shopping 單元過關(guān)檢測-【中職適用】2025年高考英語一輪復(fù)習(xí)教材全面梳理(高教版2023修訂版)
- 2024年人教版五年級數(shù)學(xué)(上冊)期末考卷及答案(各版本)
- 新修訂《軍人撫恤優(yōu)待條例》解讀:軍人權(quán)益的全面保障
- 體育行業(yè)賽事直播平臺搭建方案
- (正式版)JBT 7248-2024 閥門用低溫鋼鑄件技術(shù)規(guī)范
- 北師大版 六級上冊數(shù)學(xué) 六單元 比的認識(省級作業(yè)設(shè)計大賽作品)
- 口腔診所醫(yī)保費用結(jié)算制度范本
評論
0/150
提交評論