您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 词法分析程序C C++
%采用C/C++语言编写的词法分析程序#includeiostream#includefstream#includestring.h#includemalloc.husingnamespacestd;ifstreamfp(C语言程序.c,ios::in);charcharess;char*key[13]={if,else,for,while,do,return,break,continue,int,void,main,const,printf};//关键字char*border[7]={,,;,{,},(,),//};//分界符char*arithmetic[6]={+,-,*,/,++,--};//运算符char*relation[7]={,=,=,,=,==,!=};//关系运算符char*lableconst[80];//标识符intconstnum=40;intlableconstnum=0;intlinenum=1;//统计常数和标识符数量intsearch(charsearchchar[],intwordtype){inti=0,t=0;switch(wordtype){case1:{for(i=0;i=12;i++)//关键字{if(strcmp(key[i],searchchar)==0)return(i+1);}return(0);}case2:{for(i=0;i=6;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:coutERROR!;}}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))coutlinenum:linenumString=alphatp\t\t\t关键字endl;else{atype=search(alphatp,6);//标识符coutlinenum:linenumString=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);coutlinenum:linenumString=digittp\t\t\tDATAendl;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))//判断该运算符是否是由连续的两个字符组成的{coutlinenum:linenumString=othertp\t\t\t运算符endl;fp.get(buffer);gotoout;}else//单字符逻辑运算符{othertp[1]='\0';coutlinenum:linenumString=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))//判断该关系运算符是否是//由连续的两个字符组成的{coutlinenum:linenumString=othertp\t\t\t关系运算符endl;fp.get(buffer);gotoout;}else//单字符逻辑运算符{othertp[1]='\0';coutlinenum:linenumString=othertp\t\t\t逻辑运算endl;gotoout;}}if(buffer=='!')//==的判断{fp.get(buffer);if(buffer=='=')//cout!=(2,2)\n;fp.get(buffer);gotoout;}else{if(otype=search(othertp,2))//分界符{coutlinenum:linenumString=othertp\t\t\t分界符endl;fp.get(buffer);gotoout;}}if((buffer!='\n')&&(buffer!=''))cout错误!,字符非法\t\t\tbufferendl;fp.get(buffer);out:return(buffer);}voidmain(){inti;for(i=0;i=50;i++){lableconst[i]=;//用于保存标识符}if(!fp)cout文件打开错误!!endl;else{fp.get(charess);while(!fp.eof()){if(charess=='\n'){linenum++;fp.get(charess);}elseif(isalpha(charess)){charess=alphaprocess(charess);}elseif(isdigit(charess)){charess=digitprocess(charess);}elsecharess=otherprocess(charess);}}cout标识符个数是:lableconstnum分别是endl;i=0;while(ilableconstnum){coutlableconst[i++];}coutendl;coutSUCCESS!\n;getchar();}
本文标题:词法分析程序C C++
链接地址:https://www.777doc.com/doc-4147820 .html