您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 编译原理-实验2-词法分析器
编译原理实验2词法分析器一、实验目的1.通过设计编制调试一个具体的词法分析程序,加深对词法分析原理的理解。2.掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。3.编制一个读单词的程序,从输入的源程序中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符和分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。(遇到错误时可显示“Error”,然后跳过错误部分继续显示)二、词法分析的基础知识1.词法分析器的功能和输出格式词法分析器的功能是输入源程序,输出单词符号。词法分析器的单词符号常常表示成以下的二元式(单词种别码,单词符号的属性值)。在本实验中,采用的是一类符号一种别码的方式。标识符的BNF表示:标识符-字母字母数字串字母数字串-字母字母数字串|数字字母数字串|ε无符号整数的BNF表示:无符号整数-数字数字串数字串-数字数字串|ε运算符的BNF表示:加法运算符-+减法运算符--大于关系运算符-大于等于关系运算符-=2.超前搜索词法分析时,常常会用到超前搜索方法。如当前待分析字符串为“ai”,当前字符为“”,此时,分析器到底是将其分析为大于关系运算符还是大于等于关系运算符呢?显然,只有知道下一个字符是什么才能下结论。于是分析器读入下一个字符“+”,这时可知应将“”解释为大于运算符。但此时,超前读了一个字符“i”,所以要回退一个字符,词法分析器才能正常运行。在分析标识符,无符号整数等时也有类似情况。三、程序要求1.程序输入示例:如源程序为C语言,输入如下一段:main(){inta,b;a=10;b=a+20;}2.程序输出示例:(2,“main”)(5,“(”)(5,“)”)(5,“{”)(1,“int”)(2,“a”)(5,“,”)(2,“b”)(5,“;”)(2,“a”)(4,“=”)(3,“10”)(5,“;”)(2,“b”)(4,“=”)(2,“a”)(4,“+”)(3,“20”)(5,“;”)(5,“}“)3.具体要求如下:(1)识别保留字:if、int、for、while、do、return、break、continue等。(2)运算符包括:+、-、*、/、=、、、=、=、!=(3)分隔符包括:,、;、{、}、(、)(4)常数为无符号整形数;(5)其它的都识别为标识符;4.程序思路:(1)定义部分:定义常量、变量、数据结构。(2)初始化:从文件将源程序全部输入到字符缓冲区中。(3)取单词前:去掉多余空白。(4)取单词:读出单词的每一个字符,组成单词,分析类型,其中,关键是如何判断取单词结束,取到的单词是什么类型的单词。(5)显示结果。四、实验结果#includestdio.h#includestdlib.h#includestring.h#defineN100//定义要分析的标识符或常数的最大个数#defineM20//标识符的长度char*sourceFile=D:\\AnalyzeFile.txt;//定义进行词法分析的源文件char*key[8]={if,else,for,while,do,return,break,continue};//关键字char*border[6]={,,;,{,},(,)};//界符定义char*arithmetic[4]={+,-,*,/};//算术运算符定义char*relation[6]={,=,=,,=,};//关系运算符定义char*consts[N];//常数定义char*label[N];//标识符intconstnum=0,labelnum=0;//constnum-常数个数;labelnum-标识符个数//判断一个字符是不是字母intIsletter(charch){if(ch='a'&&ch='z'||ch='A'&&ch='Z')return1;return0;}//判断一个字符是不是数字intIsDigit(charch){if(ch='0'&&ch='9')return1;return0;}//判断单词符号类型intsearch(charsearchchar[],intwordtype){inti=0;switch(wordtype){case1:for(i=0;i=7;i++){if(strcmp(key[i],searchchar)==0)//返回具体的关键字return(i+1);}case2:{for(i=0;i=5;i++)if(strcmp(border[i],searchchar)==0)//返回具体的界符return(i+1);return(0);}case3:{for(i=0;i=3;i++)if(strcmp(arithmetic[i],searchchar)==0)//返回具体的算术运算符return(i+1);return(0);}case4:{for(i=0;i=5;i++)if(strcmp(relation[i],searchchar)==0)//返回具体的关系运算符return(i+1);return(0);}case5:{for(i=0;iconstnum;i++)if(strcmp(consts[i],searchchar)==0)//返回具体的整型常数return(i+1);consts[i]=(char*)malloc(sizeof(searchchar));strcpy(consts[i],searchchar);constnum++;return(i);}case6:{for(i=0;ilabelnum;i++)if(label[i]!=NULL)if(strcmp(label[i],searchchar)==0)//返回标识符return(i+1);label[i-1]=(char*)malloc(sizeof(searchchar));strcpy(label[i-1],searchchar);labelnum++;return(i);}}return-1;}//常数处理chardigitprocess(charbuffer,FILE*fp){inti=-1;chardigittp[M];intdtype;while((IsDigit(buffer))){digittp[++i]=buffer;buffer=fgetc(fp);}digittp[i+1]='\0';dtype=search(digittp,5);//输出整型常数printf(%s(5,%d)\n,digittp,dtype-1);return(buffer);}//标识符或关键字charalphaprocess(charbuffer,FILE*fp){intatype;inti=-1;charalphatp[M];while((Isletter(buffer))||(IsDigit(buffer))){alphatp[++i]=buffer;buffer=fgetc(fp);}alphatp[i+1]='\0';if(atype=search(alphatp,1))//输出关键字printf(%s(1,%d)\n,alphatp,atype-1);else{atype=search(alphatp,6);//输出标识符printf(%s(6,%d)\n,alphatp,atype-1);}return(buffer);}//其它处理(运算符,界符等)charotherprocess(charbuffer,FILE*fp){inti=-1;charothertp[M];intotype,otypetp;othertp[0]=buffer;othertp[1]='\0';if(otype=search(othertp,3)){printf(%s(3,%d)\n,othertp,otype-1);buffer=fgetc(fp);gotoout;}if(otype=search(othertp,4)){buffer=fgetc(fp);othertp[1]=buffer;othertp[2]='\0';if(otypetp=search(othertp,4)){printf(%s(4,%d)\n,othertp,otypetp-1);gotoout;}elseothertp[1]='\0';printf(%s(4,%d)\n,othertp,otype-1);gotoout;}if(buffer==':'){buffer=fgetc(fp);if(buffer=='=')printf(:=(2,2)\n);buffer=fgetc(fp);gotoout;}else{if(otype=search(othertp,2)){printf(%s(2,%d)\n,othertp,otype-1);buffer=fgetc(fp);gotoout;}}if((buffer!='\n')&&(buffer!=''))printf(%cerror,notaword\n,buffer);buffer=fgetc(fp);out:return(buffer);}intmain(intargc,char*argv[]){inti;FILE*fp;//文件指针,指向要分析的源程序charcbuffer;//保存最新读入的字符for(i=0;i=N;i++){label[i]=NULL;//初始化标识符consts[i]=NULL;//初始化常数}if((fp=fopen(sourceFile,rb))==NULL)//判断源文件是否存在printf(文件%s不存在,sourceFile);else{cbuffer=fgetc(fp);//读入字符while(cbuffer!=EOF)//如果文件没有结束,就一直循环{if(Isletter(cbuffer))//若为字母cbuffer=alphaprocess(cbuffer,fp);elseif(IsDigit(cbuffer))//若为数字cbuffer=digitprocess(cbuffer,fp);elsecbuffer=otherprocess(cbuffer,fp);}printf(over\n);getchar();}return0;}
本文标题:编译原理-实验2-词法分析器
链接地址:https://www.777doc.com/doc-4385830 .html