资源描述:
《编译原理词法分析器c》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、编译原理词法分析器C++源程序#include#include#include#include#include#include#include/*头文件*/voidinit();char*DchangeB(char*buf);intsearch(char*buf,inttype,intcommand);voidintdeal(char*buffer);voi
2、dchardeal(char*buffer);voiderrordeal(charerror,intlineno);voidscanner();voidinit(){cha厂key卜r「autoTbreakTcaseTcharTconstTcontinueTdefaultTdoTdouble”,”elseTenumTextemTfloatTforTgotoTifTintTlongTregiste广,"return","shorr,"signed*',"sizeof","static","struct
3、","switch","typedef",“unionTunsignedTvoidTvolatileTwhile”};/*C语言所有关键字/=,+fstreamoutfile;inti,j;char*c;outfile.open(Mkey.txr,ios::out);for(i=0;i<32;i++)outfile«key[i]«endl;outfile.close();outfile.open("Limit.txt",ios::out);for(j=0;jv38;j++)outfile«limit
4、[j]«endl;c=,,n;outfile«c;outfile.close();outfile.open(,,bsf.txt,,,ios::out);outfile.close();outfile.open(ncs.txt,ios::out);outfile.close();outfile.open(Moutput.txrjos::out);outfile.close();}char*DchangeB(char*buf){int怕mp[20];char*binary;intvalue=0,i=0
5、,j;for(i=0;buf[i]!='H, ,,n;i++)value=value*10+(buf[i]-48);广将字符转化为十进制数"/if(value==0){binary=newchar[2];binary[O]=,mO,m;binary[1]=,,,, ,u,;return(binary);}i=0;while(value!=O){temp[i++]=value%2;value/=2;}temp[i]=',n ,H,;binary=newchar[i+1];for(j=0;j<=i
6、-1;j++)binary[j]=(char)(temp[i-j-1]+48);binary[i]=m, ,,M;retum(binary);广十进制转化为二进制碎/intsearch(char*buf,inttype)ntcommand){intnumber=0;fstreamoutfile;charch;chartemp[30];inti=0;switch(type){case1:outfile.open(”key.txt「ios::in);break;case2:outfile.open(,
7、,bsf.txt,ios::in);break;case3:outfile.open(Mcs.txtM,ios::in);break;case4:outfile.open(f,limit.txt,,,ios::in);break;}outfile.get(ch);while(ch!=EOF){while(ch!=m,,m){temp[i++]=ch;outfile.get(ch);}temp[i]严0””;i=0;number++;if(strcmp(temp,buf)==O){outfile
8、.close();returnnumber;广若找到,返回在相应表中的序号"/}elseoutfile.get(ch);}〃结束外层while循坏if(command==1){outfile.close();return0;广找不到,当只需查表,返回0,否则还需造衣*/}switch(type){case1:outfile.open(Mkey.txtH,ios::in);break;case2:outfile.open(,,bsf.txt,,Jos::in);brea