




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí) 驗(yàn) 報(bào) 告實(shí)驗(yàn)項(xiàng)目列表序號(hào)實(shí)驗(yàn)項(xiàng)目名稱(chēng)成績(jī)01DFA的生成與字符串的識(shí)別02程序段的單詞識(shí)別03自頂向下語(yǔ)法分析方法的實(shí)現(xiàn)04050607080910111213141516總評(píng)成績(jī): 教員簽字:一、 實(shí)驗(yàn)名稱(chēng)自頂向下語(yǔ)法分析方法的實(shí)現(xiàn)二、實(shí)驗(yàn)?zāi)康?.掌握自頂向下語(yǔ)法分析的方法; 2.運(yùn)用編程的手段實(shí)現(xiàn)自頂向下語(yǔ)法分析。三、實(shí)驗(yàn)內(nèi)容和要求四、實(shí)驗(yàn)環(huán)境1硬件環(huán)境:PC機(jī)2軟件環(huán)境:Windows操作系統(tǒng),VC+集成開(kāi)發(fā)環(huán)境五、算法設(shè)計(jì)思想六、主要問(wèn)題與解決方法七、實(shí)驗(yàn)結(jié)果以下是程序的用戶運(yùn)行界面截圖:八、體會(huì)、質(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<<"錯(cuò)誤!無(wú)法存儲(chǔ)用戶數(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"<<'"'<<"的分析過(guò)程"<<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)/簡(jiǎn)易畫(huà)線代碼,呵呵,畫(huà)行直線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+)/畫(huà)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. 本站所有資源如無(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中級(jí)社會(huì)工作者考試逐步深化理解與應(yīng)用策略試題及答案
- 法警面試題目解析及答案
- 高一全科試題及答案
- 初級(jí)執(zhí)法考試題庫(kù)及答案
- 系統(tǒng)分析師職務(wù)解析試題及答案
- 2025貨車(chē)司機(jī)勞動(dòng)合同模板
- 2025系統(tǒng)分析師考試復(fù)習(xí)計(jì)劃試題及答案
- 有效應(yīng)對(duì)軟件評(píng)測(cè)師考試的工具試題及答案
- 生豬出欄面試題目及答案
- 群眾路線測(cè)試題及答案
- GB/T 8488-2001耐酸磚
- GB/T 23703.2-2010知識(shí)管理第2部分:術(shù)語(yǔ)
- 采購(gòu)合同英文版
- 國(guó)開(kāi)經(jīng)濟(jì)學(xué)(本)1-14章練習(xí)試題及答案
- 二0二三年度六年級(jí)上冊(cè)Module1《多維閱讀》第八級(jí)DifferentPlants教學(xué)設(shè)計(jì)
- 中國(guó)古代文學(xué)史(二)正式課件
- 頸椎病護(hù)理查房 (3)PPT
- 私教工作表格健康問(wèn)卷
- 國(guó)外教材精選 課后習(xí)題答案量子力學(xué)概論 格里菲斯 習(xí)題解
- 稀土產(chǎn)業(yè)園建設(shè)項(xiàng)目建議書(shū)(參考范文)
- 少先隊(duì)員入隊(duì)好人好事記錄表
評(píng)論
0/150
提交評(píng)論