




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實 驗 報 告實驗項目列表序號實驗項目名稱成績01DFA的生成與字符串的識別02程序段的單詞識別03自頂向下語法分析方法的實現(xiàn)04050607080910111213141516總評成績: 教員簽字:一、 實驗名稱自頂向下語法分析方法的實現(xiàn)二、實驗?zāi)康?.掌握自頂向下語法分析的方法; 2.運用編程的手段實現(xiàn)自頂向下語法分析。三、實驗內(nèi)容和要求四、實驗環(huán)境1硬件環(huán)境:PC機2軟件環(huán)境:Windows操作系統(tǒng),VC+集成開發(fā)環(huán)境五、算法設(shè)計思想六、主要問題與解決方法七、實驗結(jié)果以下是程序的用戶運行界面截圖:八、體會、質(zhì)疑、建議九、源代碼 #include<fstream>#includ
2、e<sstream>#include<iostream>#include<vector>#include<string>#include<windows.h>#include<iterator>#include<algorithm>#include<stdlib.h>#define M 20using namespace std;void gotoxy(int x,int y)COORD coord;coord.X=x;coord.Y=y;SetConsoleCursorPosition( GetS
3、tdHandle( STD_OUTPUT_HANDLE ), coord );class tablepublic:void read();friend class an_process;string find_str(char row,char col);private:string matrixMM;char start;vector<string> vec_row;vector<string> vec_col;class sentencepublic:void read_in();friend class an_process;private:string str_
4、input;class an_processpublic:void analyse(const table LL1_table,const sentence input);void write_result();void print2(int x,int y);private:vector<int> step;vector<vector<char> > stack_note;vector<string> remain;vector<string> production;void table:read()string a,str;cha
5、r x,y;int linenum = 0;char line1024=0;ifstream infile("table1.txt");if(!infile)cerr<<"錯誤!無法存儲用戶數(shù)據(jù)!n"while(infile.getline(line,sizeof(line)stringstream word(line);if(linenum>1)if(word>>x&&word>>y&&word>>str)matrixx-48y-48=str;/*cout<&l
6、t;x<<" "cout<<y<<" "cout<<matrixx-48y-48<<" "*/while(word>>a&&linenum<2)if(linenum=0)vec_col.push_back(a);/*cout<<a<<" "*/else if(linenum=1)vec_row.push_back(a);/*cout<<a<<" "*/*c
7、out<<endl;*/linenum+;start=vec_col00;/*for(int icnt=0;icnt<vec_col.size();icnt+)cout<<vec_colicnt<<" "*/string table:find_str(char row,char col)for(int x=0;x<vec_col.size();x+)for(int y=0;y<vec_row.size();y+)if(row=vec_colx0&&col=vec_rowy0)return matrixx+
8、1y+1;return ""void an_process:analyse(table LL1_table,sentence input)int icnt=0,jcnt=0,kcnt;string ac,prod="",re=""vector<char> stack;char Now_word=input.str_inputicnt;char NOW_state=LL1_table.start;string:const_iterator Eiter;string:const_iterator Biter;stack.pus
9、h_back('#');stack.push_back(NOW_state);for(;icnt<input.str_input.size();)step.push_back(+jcnt);stack_note.push_back(stack);for(kcnt=icnt;kcnt<input.str_input.size();kcnt+)re=re+input.str_inputkcnt;re=re+"#"remain.push_back(re);re.erase();Now_word=input.str_inputicnt;NOW_state
10、=stack.back();if(NOW_state=Now_word)stack.erase(stack.end()-1);icnt+;production.push_back("(匹配)");elseac=LL1_table.find_str(NOW_state,Now_word);Biter=ac.begin();Biter-;Eiter=ac.end();Eiter-;if(ac="")prod=prod+NOW_state;prod=prod+""prod=prod+""stack.erase(stack
11、.end()-1);production.push_back(prod);prod.erase();else if(ac!="")stack.erase(stack.end()-1);prod.empty();prod=prod+NOW_state;prod=prod+""+ac;production.push_back(prod);prod.erase();for(;Eiter!=Biter;Eiter-)stack.push_back(*Eiter);else cout<<"分析失敗n"exit(0);if(stack
12、.size()!=1|stack.back()!='#')cout<<"分析失敗n"exit(0);void an_process:write_result()step.push_back(step.back()+1);vector<char> a;a.push_back('#');stack_note.push_back(a);remain.push_back("#");production.push_back("接受");gotoxy(27,1);cout<<&q
13、uot;輸入串"<<'"'<<remain0;cout<<"b"<<'"'<<"的分析過程"<<endl<<endl;print2(step.size()+1,4);vector<int>:const_iterator iter1=step.begin();vector<vector<char> >:const_iterator iter2=stack_note.begin(
14、);vector<string>:const_iterator iter3=remain.begin();vector<string>:const_iterator iter4=production.begin();gotoxy(8,4);cout<<"步驟"gotoxy(27,4);cout<<"分析棧"gotoxy(44,4);cout<<"剩余輸入串"gotoxy(63,4);cout<<"所用產(chǎn)生式"for(int xy=0;iter1
15、!=step.end();iter1+)gotoxy(9,xy+6);cout<<(*iter1);xy=xy+2;for(xy=0;iter2!=stack_note.end();iter2+)gotoxy(27,xy+6);for(int icnt=0;icnt<(*iter2).size();icnt+)cout<<(*iter2)icnt;xy=xy+2;for(xy=0;iter3!=remain.end();iter3+)gotoxy(44,xy+6);cout<<(*iter3);xy=xy+2;for(xy=0;iter4!=produ
16、ction.end();iter4+)gotoxy(63,xy+6);cout<<(*iter4);xy=xy+2;cout<<endl<<endl;void an_process:print2(int x,int y)/簡易畫線代碼,呵呵,畫行直線int i,j;for(int icnt=0;icnt<y-1;icnt+)for(i=0;i<37/y;i+)printf("");printf("");for(i=0;i<37/y;i+)printf("");putchar(10)
17、;for(icnt=0;icnt<y-1;icnt+)for(i=0;i<37/y;i+)printf(" ");printf("");putchar(10);for(j=0;j<x-1;j+)/畫5行直線for(icnt=0;icnt<y-1;icnt+)for(i=0;i<37/y;i+)printf("");printf("");for(i=0;i<37/y;i+)printf("");putchar(10);for(icnt=0;icnt<y-1;icnt+)for(i=0;i<37/y;i+)printf(" ");printf("");putchar(10);for(icnt=0;icnt<y-1;icnt+)for(i=0;i<37/y;i+)printf("");printf("");for(i=0;i<
溫馨提示
- 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)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年高效酸霧凈化器項目資金需求報告代可行性研究報告
- 2025科研設(shè)備租賃合同樣本
- 2025娛樂場所員工勞動合同書模板
- 2025合肥服務(wù)合同范本
- 2025年北京市勞動合同樣本
- 2025二手車買賣合同
- 2025新款企業(yè)辦公房產(chǎn)租賃合同
- 2025年簽訂的違章建筑房屋租賃合同是否有效
- 2025企業(yè)合同轉(zhuǎn)讓協(xié)議
- 2025年的擔(dān)保公司貸款合同范本
- 2025屆上海市浦東新區(qū)高三二模英語試卷(含答案)
- 開曼群島公司法2024版中文譯本(含2024年修訂主要內(nèi)容)
- 【MOOC】航空燃氣渦輪發(fā)動機結(jié)構(gòu)設(shè)計-北京航空航天大學(xué) 中國大學(xué)慕課MOOC答案
- 悅己人生-大學(xué)生心理健康智慧樹知到期末考試答案章節(jié)答案2024年哈爾濱工業(yè)大學(xué)
- 職業(yè)衛(wèi)生評價考試計算題匯總
- JJF 1318-2011 影像測量儀校準規(guī)范-(高清現(xiàn)行)
- 5711裝備質(zhì)量問題處理通用要求
- 酸洗磷化線材項目建議書范文
- 中山大學(xué)教授和副教授職務(wù)聘任實施辦法(試行)
- 恒速傳動裝置的工作原理
- 住院病歷交接登記本
評論
0/150
提交評論