编译原理词法分析器c++源程序#includeiostream.h#includefstream.h#includestdlib.h#includestdio.h#includestring.h#includeconio.h#includeprocess.h/*头文件*/voidinit();char*DchangeB(char*buf);intsearch(char*buf,inttype,intcommand);voidintdeal(char*buffer);voidchardeal(char*buffer);voiderrordeal(charerror,intlineno);voidscanner();voidinit(){char*key[]={,auto,break,case,char,const,continue,default,do,double,else,enum,extern,float,for,goto,if,int,long,register,return,short,signed,sizeof,static,struct,switch,typedef,union,unsigned,void,volatile,while};/*C语言所有关键字/char*limit[]={,(,),[,],-,.,!,++,--,&,~,*,/,%,+,-,,,,=,,=,==,!=,&&,||,=,+=,-=,*=,/=,,,;,{,},#,_,'};/*运算、限界符*/fstreamoutfile;inti,j;char*c;outfile.open(key.txt,ios::out);for(i=0;i32;i++)outfilekey[i]endl;outfile.close();outfile.open(Limit.txt,ios::out);for(j=0;j38;j++)outfilelimit[j]endl;c=;outfilec;outfile.close();outfile.open(bsf.txt,ios::out);outfile.close();outfile.open(cs.txt,ios::out);outfile.close();outfile.open(output.txt,ios::out);outfile.close();}char*DchangeB(char*buf){inttemp[20];char*binary;intvalue=0,i=0,j;for(i=0;buf[i]!='\0';i++)value=value*10+(buf[i]-48);/*将字符转化为十进制数*/if(value==0){binary=newchar[2];binary[0]='0';binary[1]='\0';return(binary);}i=0;while(value!=0){temp[i++]=value%2;value/=2;}temp[i]='\0';binary=newchar[i+1];for(j=0;j=i-1;j++)binary[j]=(char)(temp[i-j-1]+48);binary[i]='\0';return(binary);/*十进制转化为二进制*/}intsearch(char*buf,inttype,intcommand){intnumber=0;fstreamoutfile;charch;chartemp[30];inti=0;switch(type){case1:outfile.open(key.txt,ios::in);break;case2:outfile.open(bsf.txt,ios::in);break;case3:outfile.open(cs.txt,ios::in);break;case4:outfile.open(limit.txt,ios::in);break;}outfile.get(ch);while(ch!=EOF){while(ch!='\n'){temp[i++]=ch;outfile.get(ch);}temp[i]='\0';i=0;number++;if(strcmp(temp,buf)==0){outfile.close();returnnumber;/*若找到,返回在相应表中的序号*/}elseoutfile.get(ch);}//结束外层while循环if(command==1){outfile.close();return0;/*找不到,当只需查表,返回0,否则还需造表*/}switch(type){case1:outfile.open(key.txt,ios::in);break;case2:outfile.open(bsf.txt,ios::in);break;case3:outfile.open(cs.txt,ios::in);break;case4:outfile.open(limit.txt,ios::in);break;}outfilebuf;outfile.close();returnnumber+1;}voidintdeal(char*buffer){fstreamoutfile;intresult;result=search(buffer,1,1);/*先查关键字表*/outfile.open(output.txt,ios::app);if(result!=0)outfilebufferresultendl;/*若找到,写入输出文件*/else{result=search(buffer,2,2);/*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/outfilebufferresultendl;}/*写入输出文件*/outfile.close();}voidchardeal(char*buffer){fstreamoutfile;intresult;result=search(buffer,1,1);/*先查关键字表*/outfile.open(output.txt,ios::app);if(result!=0)outfilebufferresultendl;/*若找到,写入输出文件*/else{result=search(buffer,2,2);/*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/outfilebufferresultendl;}/*写入输出文件*/outfile.close();}voiderrordeal(charerror,intlineno){cout\nerror:error,linelineno;}voidscanner(){fstreaminfile,outfile;charfilename[20];charch;interr=0;inti=0,line=1;intcount,result,errorno=0;chararray[30];char*word;printf(\npleaseinputthefilescannername:);scanf(%s,filename);err=1;infile.open(filename,ios::nocreate|ios::in);while(!infile){coutcannotopenfileendl;printf(pleaseinputthefilenameagain:\n);scanf(%s,filename);infile.open(filename,ios::nocreate|ios::in);err++;if(err==3){coutSORROYYOUCAN'TVUEWTHEPRGARME\n;coutTANKEYOUVIEWendl;exit(0);}}infile.get(ch);while(ch!=EOF){/*按字符依次扫描源程序,直至结束*/i=0;if(((ch='A')&&(ch='Z'))||((ch='a')&&(ch='z'))||(ch=='_')){/*以字母开头*/while(((ch='A')&&(ch='Z'))||((ch='a')&&(ch='z'))||(ch=='_')||((ch='0')&&(ch='9'))){array[i++]=ch;infile.get(ch);}word=newchar[i+1];memcpy(word,array,i);word[i]='\0';intdeal(word);if(ch!=EOF)infile.seekg(-1,ios::cur);}elseif(ch='0'&&ch='9'){/*以数字开头*/while(ch='0'&&ch='9'){array[i++]=ch;infile.get(ch);}word=newchar[i+1];memcpy(word,array,i);word[i]='\0';intdeal(word);if(ch!=EOF)infile.seekg(-1,ios::cur);}elseif((ch=='')||(ch=='\t'));/*消除空格符和水平制表符*/elseif(ch=='\n')line++;/*消除回车并记录行数*/elseif(ch=='/'){/*消除注释*/infile.get(ch);if(ch=='='){/*判断是否为‘/=’符号*/outfile.open(output.txt,ios::noreplace|ios::app);outfile/=\t\t\t4\t\t\t32\n;outfile.close();}elseif(ch!='*'){/*若为除号,写入输出文件*/outfile.open(output.txt,ios::noreplace|ios::app);outfile/\t\t\t4\t\t\t13\n;outfile.close();outfile.seekg(-1,ios::cur);}elseif(ch=='*'){/*若为注释的开始,消除包含在里面的所有字符*/count=0;infile.get(ch);while(count!=2){/*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/count=0;while(ch!='*')infile.get(ch);count++;infile.get(ch);if(ch=='/')count++;elseinfile.get(ch);}}}elseif(ch==''){/*消除包含在双引号中的字符串常量*/outfile.open(output.txt,ios::noreplace|ios::app);outfilech\t\t\t4\t\t\t37\n;outfile.close();while(ch!='')infile.get(ch);infilech\t\t\t4\t\t\t37\n;infile.close();}else{/*首字符为其它字符,即运算限界符或非法字符*/array[0]=ch;infile.get(ch);/*再读入下一个字符,判断是否为双字符运算、限界符*/if(ch!=EOF){/*若该字符非文件结束符*/array[1]=ch;word=newchar[3];memcpy(word,array,2);word[2]='\0';result=search(word,4,1);/*先检索是否为双字符运算、限界符*/if(result==0){/*若不是*/word=newchar[2];memcpy(word,array,1);word[1]='\0';result=search(word,4,1)