资源描述:
《词法分析代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#include#include#include#include#includevoidinit();char*DchangeB(char*buf);intsearch(char*bufjnttype,intcommand);voidintdeal(char*buffer);voidchardeal(char*bu什er);voiderrordeal(charerror,in
2、tlineno);voidseanner();voidinit(){char*ey[]={“main”,”if',”elseTint“,”whileTcha门;广关键字*/char广运算、限界符*/fstreamoutfile;inti,j;char*c;outfile.open(”key・txt”,ios::out);for(i=0;i<5;i++)outfile«key[i]«endl;outfile.close();outfile.open(”Limit.txt”,ios::out);for(j=0;j<14;j++)outfile«limi
3、t[j]«endl;c=,m;outfile«c;outfile.close();outfile.open(”bsf・txt;ios::out);outfile.close();outfile.open(”cs.txt;ios::out);outfile.close();outfile.open(Sutput・txr;ios::out);outfile.close();}char*DchangeB(char*buf){inttemp[20];char*binary;intvalue=0,i=0,j;for(i=0;buf[i]!=, ,;i++)
4、value=value*10+(buf[i]-48);广将字符转化为十进制数*/if(value==0){binary=newchar[2];binary[O]=,O,;binary[1]=' :return(binary);}i=0;while(value!=O){temp[i++]=value%2;value/=2;}temp[i]=, ,;binary=newchar[i+1];for(j=0;j<=i-1;j++)binary[j]=(char)(temp[i-j-1]+48);binary[i]=, ,;return(binary)
5、;厂十进制转化为二进制*/}intsearch(char*buf,inttype,intcommand){intnumber=0;fstreamoutfile;charch;chartemp[30];inti=0;switch(type)case1:outfile.open(Mkey.txtnJos::in);break;case2:outfile.open(Hbsf.txtH,ios::in);break;case3:outfile.open(Hcs.txtnJos::in);break;case4:outfile.open(“limit・txt
6、”,ios::in);break;}outfile.get(ch);while(ch!=EOF){while(ch!='‘){temp[i++]=ch;outfile.get(ch);}temp[i]=, ,;i=0;number++;if(strcmp(temp,buf)==O){outfile.close();returnnumber;广若找到,返回在相应表中的序号*/elseoutfile.get(ch);〃结束外层while循环if(command==1){outfile.close();return0;广找不到,当只需查表,返回0,
7、否则还需造表*/switch(type){case1:outfile.open(l,key.txt,ios::in);break;case2:outfile.open(nbsf.txrjos::in);break;case3:outfile.open(Hcs.txtn,ios::in);break;case4:outfile・open(”limit・txt:ios::in);break;outfile«buf;outfile.close();returnnumber+1;}voidintdeal(char^buffer){fstreamoutfi
8、le;intresult;result=search(buffer,1,1);广先查关键字表*/outfile.open(