




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、編譯原理程序設計實驗報告表達式語法分析器的設計實現(xiàn) 班級:計算機1306班 姓名:李佳澤 學號:20133951 實驗目標:實現(xiàn)表達式語法分析器的設計實現(xiàn),使用遞歸下降子程序、ll1分析法、lr分析法,判斷一個表達式是否符合某個語法。 實驗內(nèi)容:算數(shù)表達式的變換文法:e -> t e1 i. (w0 t e1 u)0 e )#)t -> f t1 (l (|t1> 31 f t1 |(1wo0)1()#iet-ti® jf二" ll(1)分析表:f t i | (e)(流程圖:逆序壓棧數(shù)據(jù)結(jié)構(gòu):struct nodechar conten七30; int
2、七 ype;node * next;建立鏈表用于標識符rr,常數(shù)ct ,字符存ct ,字符串st , token源程序代碼:1 遞歸調(diào)用子程序#inc lude<iostream># include<string.h># include<fstream>#include<stdlib.h>#include <stack> using namespace std;#define notkey 50 #define 汀 00#define ctol#define st 02 #define ct 03 char * kt5=”inttma
3、intvoidtifvchar;char*pt18斗”>=”,n<=h; int ty =3;int dd=o;char vn=,e,;e,t;t,;f;charvt=,ivww;c;)#;char * right二'te“,“wte“,”,“ft“,“wfu,“”,”i“,”(e)“;int seekkt(char * token)for(int i=0; i<5; i+)if(strcmp(token,kti) = 0)return i+4;return notkey;int seekpt(char * token)for(int i=0; i<18; i+
4、) if(strcmp(token,pti) = 0) return i+10;return -1;int main()int tohn();int intn ();int sean();int right();int print();tohn();intn ();scan();if(dd=o)cout«endl;cout«"分析結(jié)果:” «endl;cout«endl;right();print();return 0;stack <char> s;struct nodechar content30; int type;node *
5、 next;node * ithead;node * cthead;node * cthead;node * sthead;node * tohead:node * pp;void e();void t();void f();void tohn()tohead = new node(); strcpy(tohead->content,hn);tohead->type = 0;tohead->next = null;void setton(char * content,int type)node * p = tohead ;node * temp = new node(); w
6、hile(p->next !=null)p = p->next;strcpy(temp> content,content); temp->type = type;temp-> next = null;p->next = temp;初始化所有表頭結(jié)點void intn()ithead = new node(); strcpy(ithead->content/,u); ithead->type = it; ithead->next = null;sthead = new node();strcpy(sthead->content,nn);
7、sthead->type = st;sthead->next = null;cthead = new node();strcpy(cthead->content/,n);cthead->type = ct;cthead->next = null;cthead = new node();strcpy(cthead->content/ih);cthead->type = ct; cthead->next = null;創(chuàng)建新節(jié)點void setn(char * contentjnt type,node * head)node * p = head ;
8、node * temp = new node();int flag = 0;while(p->next!=null)if(strcmp(content,p next>content) = 0)flag = 1;p = p->next;if(flag = 0)strcpy(temp->content,content);temp->type = type;temp->next = null;p->next = temp;void print()node * p = tohead;p = p->next; while(p!=null)p = p->
9、;next;void sean()/char filename50j;char ch;char a30;char * token;int i;file * fp;/*printf(h輸入語法分析的c語言程序(形如d:test.txt): n”); scanf(m%sn,filename);fp = fopen(filename,"rn);*/fp=fopen("test.txt","rm);ch = fgetc(fp);while(ch!-#r)i = 0;以字母或者下劃線開頭,處理關(guān)鍵字或者標識符if(ch>=,a, && ch&
10、lt;=,z,) | (ch>=,a, && ch<=,z,) | ch =)while(ch>='a, && ch<=,z,)|(ch>=,a, && ch<=,z,)|(ch>=,0, && chv=9) | ch =ai+ = ch;ch = fgetc(fp);token = new chari+l;memcpy(token,a,i);tokeni二 v):int seektemp = seekkt(token);if(seektemp!=notkey)setton(tok
11、en,seektemp);else setn(tokenjtjthead);setton(token,it);fseek(fp,-1 l,seek_cur);/以數(shù)字開頭,處理數(shù)字else if(ch >='0' && chv二9)int flag = 0;int flag2 = 0;while(ch >='0' && ch<=,9,)ai+ = ch;ch =fgetc(fp);1處理floatif(ch = 7)flag = 1;ai+ = ch;ch = fgetc(fp);if(ch>=,0, &a
12、mp;& chv=9)while(ch>=,0, && ch<=,9,)ai+ = ch;ch = fgetc(fp);處理 doubleif(ch = e | ch = 0)ai+ = ch; ch = fgetc(fp);if(ch =| ch = ai+ = ch; ch = fgetc(fp);if(ch >=,0, && chv=9)ai+ = ch;ch = fgetc(fp);token= new chari+l;memcpy(tokenai);tokeni二 v):if(flag = 0)setn(token,ct,ct
13、head);setton(tokemct);elsesetn(token.ct.cthead);setton(token,ct);fseek(fp,-ll,seek_cur);處理常量字符串else if(ch = uh)ai+ = ch;ch = fgetc(fp); while(ch!=hm)ai+ = ch;ch = fgetc(fp);ai+ = ch;ch = fgetc(fp);token = new chari+l;memcpy(token,a,i);tokeni二 vx;setn(token,st,sthead);setton(token.st);fseek(fp,-1 l,s
14、eek_cur);處理常量字符else if(ch = v)ai+ = ch; ch = fgetc(fp);while(ch!=v)ai+ = ch; ch = fgetc(fp);ai+ = ch;ch = fgetc(fp);token = new chari+l j;memcpy(tokenai);tokeni二 v):setn(token,ct,cthead);setton(token,ct);fseek(fp<l l,seek_cur);結(jié)朿符elseif(ch>=*! '&&chv 二 7')|(ch> 二丫&&c
15、h<=,)|(ch>=t&&ch<="')|(ch>=,(,&&ch<=-')int seektemp;if(ch=,<,|ch=,>,|ch='=,)ai+ = ch;ch = fgetc(fp);else ai+ = ch;ch = fgetc(fp);)token = new chari+l; memcpy(token,a,i);token i =、0'seektemp=seekpt(token);if(seektemp=-l)/結(jié)束符不存在cout« “結(jié)束符錯
16、誤請檢查”endl;dd=l;1elsesetton(token,seektemp);fseek(fp, 1 l,seek_cur);ch = fgetc(fp);setton(“#“,333);fclose(fp);void right()pp=tohead;pp=pp->next;e();if(strcmp(pp->content,',#'r) = ()&&ty=3)cout«,right',«endl;elseif(strcmp(pp->content,n#h) != 0 &&ty=3)cout
17、«,wrong,«endl«endl; cout«"理由:括號不匹配"«endl;cout«endl;1else 訐(ty= 1)cout«hwrongm«endl«endl;coutvv”理由:非法字符u«endl;cout«endl;else if(ty =2)cout«hwrong"«endl«endi;cout«h理由:缺少 “)”“endl;cout«endl;void e()if (ty !=
18、3) return;t();while (strcmp(pp->content,m+n) = o|strcmp(pp->content,-n) = 0)pp=pp next;t();return ;void t()if (ty != 3) return;f();while (strcmp(pp->content/,*f,) = 0|strcmp(pp->content/7n) = 0)pp=pp->next;f();return ;void f()訐(ty != 3) return;if (pp->type = ct|pp->type = it)pp=
19、pp->next;return;elseif(strcmp(pp->content/l(h) = 0) pp=pp->next;e();if (strcmp(pp->content,h),f) = 0)pp=pp > next;return;elsety = 2;return;elsereturn;ty= 1;12.ll1分析法# include<iostream># include<stri ng.h># include<fstream>#include<stdlib.h> #include <stack&g
20、t; using namespace std; #define notkey 50 #define it 00 #define ct 01 #define st 02 #define ct 03 char * kt5=rinttmaintvoidtiftchar"char * pt18=n>=n,<=",”二”; int dd=o;int sss=o;int seekkt(chcir * token)for(int i=0; i<5; i+)if(strcmp(token,kti) = 0)return i+4;return notkey;int seek
21、pt(char * token)for(int i=0; i<18; i+)if(strcmp(token,pti) = 0) return i+10;return -1;int main()inttohn();intintn ();intscan();int print();int wrong();tohn();intn ();scan();print();if(dd=o)cout«"分析結(jié)果:m«endl«endl; wrong();cout«endl;return 0;stack <char*> s;struct nod
22、echar content30j;int type;node * next;node * ithead;node * cthead;node * cthead;node * sthead;node * tohead;node * pp;void tohn()tohead = new node(); strcpy(tohead->content, ” ”);tohead->type = 0;tohead->next = null;void setton(char * content,int type)node * p = tohead ;node * temp = new no
23、de(); while(p->next!=null)p = p->next;strcpy(temp> content,content);temp->type = type;temp->next = null;p->next = temp;初始化所有表頭結(jié)點 void intn()ithead = new node(); s trcpy (i thead->conten t,tht);ithead->type = it; ithead->next = null;sthead = new node();strcpy(sthead->con
24、tent/'n); sthead->type = st; sthead->next = null;cthead = new node();strcpy(cthead->content/h,);cthead->type = ct;cthead->next = null;cthead = new node();strcpy(cthead->content,nn); cthead->type = ct; cthead->next = null;創(chuàng)建新節(jié)點void setn(char * content,int type,node * head)
25、node * p = head ;node * temp = new node();int flag = 0;while(p->next!=null)if(strcnip(content,p->next->conte nt) = 0)flag = 1;p = p->next;)if(flag = 0)strcpy(temp > content,conte nt);temp->type = type;temp-> next = null;p->next = temp;打印鏈表void print()node * p = tohead; p = p-
26、>next; while(p!=null) p = p->n ext;掃描程序void scan()/char filename50;char ch;char array 30; char * token;int i;file * fp;/*printf("輸入語法分析的c語言程序(形如test.txt): nh); scanf(m%sn,filename);fp = fopen(filename,"rn);*/fp=fopen("test.txt","rm);ch = fgetc(fp);while(ch!二'#'
27、)i = 0;if(ch>='a' && ch<='z,) | (ch>='a* && ch<=rz') | ch =while(ch>='a' && ch<='z')|(ch>=,a, && ch<='z')|(ch>=,0, && ch<='9') | ch =array i+ = ch;ch = fgetc(fp);token = new char
28、fi+1;memcpy(token,array,i);tokeni二'0;int seektemp = seekkt(token);if(seektemp!=notkey)setton(token,seektemp);else setn(token,it,ithead);setton(token,it);fseek(fp,-l l,seek_cur);/以數(shù)字開頭,處理數(shù)字else if(ch >='0' && ch<='9')int flag = 0;int flagl = 0;while(ch >='0
29、9; && ch<=,9,)arrayli+j = ch;ch =fgetc(fp);處理floatif(ch 二二 t)flagl = 1;array fi+ = ch;ch = fgetc(fp);if(ch>=,0, && chv二9)while(ch>='0' && ch<=,9,)arrayi+ = ch;ch = fgetc(fp);處理 doubleif(ch = 'e' | ch = 'e') arrayi+ = ch; ch = fgetc(fp);if(c
30、h =| ch ='')array fi+l = ch; ch = fgetc(fp);if(ch >='0* && ch<=*9') array fi+l = ch; ch = fgetc(fp);token= new chari+1;memcpy(token,arrays);tokeni = vx;if(flag = 0)setn(token.ct.cthead);setton(token,ct);elsesetn(token,ct,cthead);setton(token,ct); fseek(fp, 1 l,seek_cur)
31、;處理常量字符串else if(ch = nn)arrayi+ = ch;ch = fgetc(fp);while(ch!=um)arrayi+j = ch;ch = fgetc(fp);arrayi+j = ch;ch = fgetc(fp);token = new chari+l;memcpy(token,array,i);tokeni二 vt;setn(token,st,sthead);setton(token,st);fseek(fp,ll,seek_cur); /處理常量字符else if(ch = '")arrayi+ = ch;ch = fgetc(fp);wh
32、ile(ch!二arrayi+j = ch;ch = fgetc(fp);arrayi+ = ch;ch = fgetc(fp); token = new chari+l;memcpy (token .array, i);tokeni = vx;setn(token.ctcthead);setton(token,ct);fseek(fp<l l,seek_cur);結(jié)朿符 elseif(ch>=*! '&&ch<='/,)|(ch>=,: '&&ch<=,)|(ch>=,&&ch<
33、=,v,)|(ch>=,( '&&chv=) inf seektemp;if(ch=,<'|ch=,>,|ch=,=,) arrayi+ = ch; ch = fgetc(fp);) else arrayi+ = ch;ch = fgetc(fp);token = new chari+l; memcpy(token,array,i);tokens =seektemp=seekpt(token); if(seektemp=-1)/結(jié)束符不存在coutvv “結(jié)束符錯誤請檢査 vendl;dd=l;elsesetton(token,seektemp
34、);fseek(fp,-1 l,seek_cur);ch = fgetc(fp);setton(“#“,333);fclose(fp);內(nèi)容類碼(24aa0+16)25*188&23/19-17#123*/void ssss()if(s.top()二二”e”) if(pp->type=it)|(pp->type=ct) s.pop();s.push(“el“);s.push(,tn);sss=l;return;if(pp->type=24)s.pop();s.push("el");s.push(t);if(slop()=nelu)if(pp->
35、;type= 16)| (pp->type= 17) s.popo;s.push(” el“);s.push("tm);s.push("wo");sss=l;retum;if(pp->type=25)s.popo;sss=l;return;if(pp->type=333)s.pop();sss=l;return;if(s.top()二二”t“)if(pp->type=it)|(pp->type=ct) s.pop();s.push(”tl");s.pushc'f”);if(pp->type=24)s.pop();s.push(“tl“);s.push("f");sss=l;retum; 處理 floatif(s.top()=ntln)if(pp->type= 16)|(pp->type= 17) s.pop();sss=l;return;if(pp->type= 18)|(pp->type= 19) s.pop();s.push(“tl“);s.push("f");s.push("wl");sss=l;return;if(pp->type=25)s.pop();
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 英語高一北師大版unit9單元測試
- 餐飲公司食品安全信息公示及共享協(xié)議
- 部門產(chǎn)品培訓方案模板
- 燒烤店經(jīng)營權(quán)及設備轉(zhuǎn)讓協(xié)議書
- 農(nóng)村土房溫暖改造方案
- 車輛借用與押金退還管理合同范本
- 建筑項目管理升級方案
- 拆遷安置補償與房屋買賣服務合同
- 搜索能力面試題及答案
- 小學京劇面試題及答案
- 2024-2025年廣東省高中學業(yè)水平測試生物試卷(含答案)
- 護理進修匯報做
- 不良資產(chǎn)盡職調(diào)查工作底稿表
- 《正確對等得與失》課件
- 甩手運動原理課題研究報告
- 皮膚腫物培訓演示課件
- 糖尿病胃輕癱的護理查房課件
- 多孔硅酸鈣材料的制備
- (完整)工程竣工驗收單-范本
- 設備結(jié)算單表格
- 精神病學課件:抗抑郁藥物和抗焦慮藥物完整版
評論
0/150
提交評論