编译原理自顶向下语法分析方法的实现_第1页
编译原理自顶向下语法分析方法的实现_第2页
编译原理自顶向下语法分析方法的实现_第3页
编译原理自顶向下语法分析方法的实现_第4页
编译原理自顶向下语法分析方法的实现_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实 验 报 告实验项目列表序号实验项目名称成绩01DFA的生成与字符串的识别02程序段的单词识别03自顶向下语法分析方法的实现04050607080910111213141516总评成绩: 教员签字:一、 实验名称自顶向下语法分析方法的实现二、实验目的1.掌握自顶向下语法分析的方法; 2.运用编程的手段实现自顶向下语法分析。三、实验内容和要求四、实验环境1硬件环境:PC机2软件环境:Windows操作系统,VC+集成开发环境五、算法设计思想六、主要问题与解决方法七、实验结果以下是程序的用户运行界面截图:八、体会、质疑、建议九、源代码 #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<<"错误!无法存储用户数据!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<<"所用产生式"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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论