您好,欢迎访问三七文档
《词法分析》实验报告《编译技术》实验一实验报告1目录目录············································································································································11实验目的································································································································22实验内容································································································································22.1TINY计算机语言描述··························································································································22.2实验要求·············································································································································23此法分析器的程序实现········································································································33.1状态转换图·········································································································································33.2程序源码·············································································································································43.3实验运行效果截图·····························································································································94实验体会······························································································································10《编译技术》实验一实验报告21实验目的1、学会针对DFA转换图实现相应的高级语言源程序。2、深刻领会状态转换图的含义,逐步理解有限自动机。3、掌握手工生成词法分析器的方法,了解词法分析器的内部工作原理。2实验内容2.1TINY计算机语言描述TINY计算机语言的编译程序的词法分析部分实现。从左到右扫描每行该语言源程序的符号,拼成单词,换成统一的内部表示(token)送给语法分析程序。为了简化程序的编写,有具体的要求如下:1、数仅仅是整数。2、空白符仅仅是空格、回车符、制表符。3、代码是自由格式。4、注释应放在花括号之内,并且不允许嵌套TINY语言的单词保留字特殊符号其他if+标识符(一个或更多的字母)then-else*end/repeat=数(一个或更多的数字)untilread{write};2.2实验要求要求实现编译器的以下功能1、按规则拼单词,并转换成二元式形式2、删除注释行3、删除空白符(空格、回车符、制表符)《编译技术》实验一实验报告34、列表打印源程序,按照源程序的行打印,在每行的前面加上行号,并且打印出每行包含的记号的二元形式5、发现并定位错误词法分析进行具体的要求1、记号的二元式形式中种类采用枚举方法定义;其中保留字和特殊字符是每个都一个种类,标示符自己是一类,数字是一类;单词的属性就是表示的字符串值。2、词法分析的具体功能实现是一个函数GetToken(),每次调用都对剩余的字符串分析得到一个单词或记号识别其种类,收集该记号的符号串属性,当识别一个单词完毕,采用返回值的形式返回符号的种类,同时采用程序变量的形式提供当前识别出记号的属性值。这样配合语法分析程序的分析需要的记号及其属性,生成一个语法树。3、标示符和保留字的词法构成相同,为了更好的实现,把语言的保留字建立一个表格存储,这样可以把保留字的识别放在标示符之后,用识别出的标示符对比该表格,如果存在该表格中则是保留字,否则是一般标示符。3此法分析器的程序实现3.1状态转换图图1TINY语言的确定有限自动机(DFA)《编译技术》实验一实验报告43.2程序源码#includeiostreamusingnamespacestd;#includefstream#includestring#includeiomanipclassCA{public:voidgetToken();//识别标识符voidpreDo(char);//预处理源程序voiddisplay();//打印出CA(){m=n=0;}private:longcount2;charID[10][10];//符号表intm;//设m为符号表中已有的标识符的个数charCons[10][10];//常数表intn;//设n为常数表中已有的常数的个数charbuffer1[99999];boolisDigit(char);//检查是否为数字boolisLetter(char);//检查是否为字符intInsertID(char*strToken);//添加符号intInsertConst(char*strToken);//添加符号intSearch(chara[][10],constchar*,int);//二分查找};//enumvariety{标识符,数字,关键字1,关键字2,关键字3,关键字4,关键字5,关键字6,关键字7,关键字8,特殊符号1,//特殊符号2,特殊符号3,特殊符号4,特殊符号5,特殊符号6,特殊符号7,特殊符号8,特殊符号9,特殊符号10};//关键字bi编码12345678charK[18][10]={else,end,if,read,repeat,then,until,write,%,*,+,-,/,:,;,,=,};boolCA::isDigit(charc)《编译技术》实验一实验报告5{if(c='9'&&c='0')returntrue;elsereturnfalse;}boolCA::isLetter(charc){if(c='a'&&c='z'||c='A'&&c='Z')returntrue;elsereturnfalse;}intCA::Search(chara[18][10],constchar*b,intn){intleft=0;intright=n-1;intmid=0;while(left=right){mid=(left+right)/2;if(strcmp(a[mid],b)==0)returnmid;elseif(strcmp(a[mid],b)0)left=mid+1;elseright=mid-1;}return-1;}intCA::InsertID(char*strToken){inti=0;while(im)//设m为符号表中已有的标识符的个数{if(!strcmp(ID[i],strToken))returni;i++;}strcpy(ID[i],strToken);m++;returni;}《编译技术》实验一实验报告6intCA::InsertConst(char*strToken){inti=0;while(in)//设n为常数表中已有的常数的个数{if(!strcmp(Cons[i],strToken))returni;i++;}strcpy(Cons[i],strToken);n++;returni;}voidCA::display(){charfilename[30];//输入的时候一定要是...:/.../...格式cout请输入源程序代码.cpp文件的绝对路径:endl;cinfilename;ifstreaminfile(filename,ios::in);if(!infile){cerropenerror!endl;abort();}charch;intcount1=1;count2=0;cout第count1行;while(infile.get(ch)){preDo(ch);coutch;//逐行打印出源程序if(ch=='\n'){count1++;cout第count1行;}}buffer1[count2+1]='\0';《编译技术》实验一实验报告7coutendl;//对buffer1中的字符进行扫描getToken();infile.close();coutendl;cout符号表:;for(inti=0;im;i++)coutID[i];coutendl;cout数字表:;for(intj=0;jn;j++)coutCons[j];coutendl;}voidCA::preDo(charc){char*p=buffer1;buffer1[0]='#';//赋#给扫描缓冲区的第一个元素count2++;*(p+count2)=c;if(buffer1[count2-1]=='{')//删除注释{if(c=='{')cout注释不允许嵌套!;//注释不允许嵌套if(c!='}')count2--;elsecount2=count2-2;}elseif(buffer1[count2-1]==''&&c=='')count2--;//若干相继的空白符结合成一个if(c=='\r')count2--;if(c=='\t')count2--;}voidCA::getToken(){intn=1;《编译技术》实验一实验报告8cout第1行:;for(inti=1;istrlen(buffer1);i++){if(buffer1[i]=='\n'){n++;cout\n第n行:;}elseif(isLetter(buffer1[i])){stringtok=;tok+=buffer1[i];while(isLetter(buffer1[++i]))tok+=buffer1[i];intr=Search(K,tok.c_str(),8);i--;if(r!=-1)cout(关键字r+1,tok);//关键字编码else{cout(标识符,tok);//标识符编码r=InsertID((char*)tok.c_str());}}elseif(isDigit(buffer1[i])){stringtok=;tok+=buffer1[i];while(isDigit(buffer1[++i])){tok+=buffer1[i];}inty=InsertConst((char*)t
本文标题:词法分析的实验报告
链接地址:https://www.777doc.com/doc-2026548 .html