您好,欢迎访问三七文档
通达学院专业课程设计II题目:词法分析程序的构造专业计算机通信学生姓名班级学号28班09002801指导教师徐佳指导单位计算机学院计算机科学与技术系日期2012.11.12-2012.11.23教师评语同学出勤率(满勤、较高、一般,较低),学习态度(端正、较端正、一般、较差),程序设计基础(好、较好、一般、较差),演示程序(已经、没有)达到了基本要求,算法设计(好、较好、一般),界面友好程度(好、较好、一般),答辩过程中回答问题(准确、较准确、错误率较高),撰写报告格式(规范、一般)、内容(丰满、简单)、表述(清晰、一般、不清楚),(圆满、较好、基本)完成了课题任务。教师签名:年月日成绩评定备注词法分析程序的构造一、课题内容和要求通过状态转换图构造C或者PASCAL语言子集的词法分析程序。原理解析:选取语言,例如选取了C语言,选取其中一个子集,例如包含了部分关键字main、float、if、for等等,特殊符号(、、=、+等等,特殊定义的标识符变量以及部分常量等,采用《编译原理》词法分析中有穷自动机的思想构建出该语言子集的状态转换图,并编码实现。基本要求:(1)将选取的语言子集编写一个简单程序,放在一个文本文件中;(2)要将一个个单词区分清楚并归类(例如for属于关键字)。二、需求和思路分析本课题是用C++语言设计,选取的是C语言子集。编写对简单语言进行词法分析的词法分析程序。1、识别子集中的关键字、标识符、常数、运算符和分界符等。2、对子集中的字符类型进行归类三、概要设计(1)状态转换图:(2)核心代码:1)定义:charcbuffer;char*keyword[14]={if,else,for,while,do,float,return,break,continue,int,void,main,const,printf};//关键字char*border[8]={,,;,{,},(,),:=,.};//分隔符char*arithmetic[6]={+,-,*,/,++,--};//运算符char*relation[7]={,=,=,,=,==,!=};//关系运算符char*lableconst[80];//标识符2)函数调用:search(charsearchchar[],intwordtype)//查找类型alphaprocess(charbuffer)//字符处理过程digitprocess(charbuffer)//数字处理过程otherprocess(charbuffer)//分隔符、运算符、逻辑运算符等main()//主函数3)状态类型:状态转换图的形式:■每个状态对应一个带标号的case语句■转向边对应goto语句switch(wordtype){case1:{for(i=0;i=13;i++)//关键字{if(strcmp(keyword[i],searchchar)==0)return(i+1);}return(0);}case2:{for(i=0;i=7;i++)//分隔符{if(strcmp(border[i],searchchar)==0)return(i+1);}return(0);}case3:{for(i=0;i=5;i++)//运算符{if(strcmp(arithmetic[i],searchchar)==0)return(i+1);}return(0);}case4:{for(i=0;i=6;i++)//关系运算符{if(strcmp(relation[i],searchchar)==0)return(i+1);}return(0);}case5:{for(t=40;t=constnum;t++)//常数{if(strcmp(searchchar,lableconst[t])==0)//判断该常数是否已出现过return(t+1);}lableconst[t-1]=(char*)malloc(sizeof(searchchar));//为新的元素分配内存空间strcpy(lableconst[t-1],searchchar);//为数组赋值lableconst指针数组名constnum++;//常数个数自加return(t);}case6:{for(i=0;i=lableconstnum;i++){if(strcmp(searchchar,lableconst[i])==0)//判断标识符是否已出现过return(i+1);}lableconst[i-1]=(char*)malloc(sizeof(searchchar));strcpy(lableconst[i-1],searchchar);lableconstnum++;//标识符个数自加return(i);}5)单字符判断if(otypetp=search(othertp,3))//判断该运算符是否是由连续的两个字符组成的{coutrow:rowString=othertp\t\t\t运算符endl;fp.get(buffer);gotoout;}else//单字符逻辑运算符{othertp[1]='\0';coutrow:rowString=othertp\t\t\t逻辑运算符endl;gotoout;}四、详细设计实验环境:visualC++6.0win7系统源程序代码:#includeiostream#includectype.h#includefstream#includestring.h#includemalloc.husingnamespacestd;ifstreamfp(09002801.txt,ios::in);charcbuffer;char*keyword[14]={if,else,for,while,do,float,return,break,continue,int,void,main,const,printf};//关键字char*border[8]={,,;,{,},(,),:=,.};//分隔符char*arithmetic[6]={+,-,*,/,++,--};//运算符char*relation[7]={,=,=,,=,==,!=};//关系运算符char*lableconst[80];//标识符intconstnum=40;intlableconstnum=0;//统计常数和标识符数量introw=1;intsearch(charsearchchar[],intwordtype){inti=0,t=0;switch(wordtype){case1:{for(i=0;i=13;i++)//关键字{if(strcmp(keyword[i],searchchar)==0)return(i+1);}return(0);}case2:{for(i=0;i=7;i++)//分隔符{if(strcmp(border[i],searchchar)==0)return(i+1);}return(0);}case3:{for(i=0;i=5;i++)//运算符{if(strcmp(arithmetic[i],searchchar)==0)return(i+1);}return(0);}case4:{for(i=0;i=6;i++)//关系运算符{if(strcmp(relation[i],searchchar)==0)return(i+1);}return(0);}case5:{for(t=40;t=constnum;t++)//常数{if(strcmp(searchchar,lableconst[t])==0)//判断该常数是否已出现过return(t+1);}lableconst[t-1]=(char*)malloc(sizeof(searchchar));//为新的元素分配内存空间strcpy(lableconst[t-1],searchchar);//为数组赋值lableconst指针数组名constnum++;//常数个数自加return(t);}case6:{for(i=0;i=lableconstnum;i++){if(strcmp(searchchar,lableconst[i])==0)//判断标识符是否已出现过return(i+1);}lableconst[i-1]=(char*)malloc(sizeof(searchchar));strcpy(lableconst[i-1],searchchar);lableconstnum++;//标识符个数自加return(i);}default:cout错误!;}}charalphaprocess(charbuffer)//字符处理过程{intatype;inti=-1;charalphatp[20];while((isalpha(buffer))||(isdigit(buffer)))//这两个函数分别是判字符和判数字函数位于ctype.h中{alphatp[++i]=buffer;fp.get(buffer);}alphatp[i+1]='\0';//在末尾添加字符串结束标志if(atype=search(alphatp,1))coutrow:rowString=alphatp\t\t\t关键字endl;else{atype=search(alphatp,6);//标识符coutrow:rowString=alphatp\t\t\t标识符endl;}return(buffer);}chardigitprocess(charbuffer)//数字处理过程{inti=-1;chardigittp[20];intdtype;while((isdigit(buffer))){digittp[++i]=buffer;fp.get(buffer);}digittp[i+1]='\0';dtype=search(digittp,5);coutrow:rowString=digittp\t\t\t数字endl;return(buffer);}charotherprocess(charbuffer)//分隔符、运算符、逻辑运算符等{inti=-1;charothertp[20];intotype,otypetp;othertp[0]=buffer;othertp[1]='\0';if(otype=search(othertp,3)){fp.get(buffer);othertp[1]=buffer;othertp[2]='\0';if(otypetp=search(othertp,3))//判断该运算符是否是由连续的两个字符组成的{coutrow:rowString=othertp\t\t\t运算符endl;fp.get(buffer);gotoout;}else//单字符逻辑运算符{othertp[1]='\0';coutrow:rowString=othertp\t\t\t逻辑运算符endl;gotoout;}}if(otype=search(othertp,4))//关系运算符{fp.get(buffer);othertp[1]=buffer;othertp[2]='\0';if(otypetp=search(othertp,4))//判断该关系运算符是否是由连续的两个字符组成的{coutrow:rowString=othertp\t\t\t关系运算符endl;fp.get(buffer);gotoout;}else//单字符逻辑运算符{othertp[1]='\0';coutrow:rowString=othertp\t\t\t逻辑运
本文标题:词法分析程序的构造
链接地址:https://www.777doc.com/doc-4772715 .html