c语言编写的词法分析程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includestdlib.h#includestring.h#includectype.h#defineSPACE0x20//空格键#defineCHANGE'c'//转义字符#defineSTRING'S'//引号里的字符串#defineBOUNDARY'B'//界符#defineOPERATION'O'//运算符#defineWORD'I'//标识符#defineKEY_WORD'K'//关键字#defineDIGIT'n'//数字,包括小数#defineDOTH'd'//头文件,“.h”文件#defineINT't'#defineVOID'v'#defineIF'f'#defineELSE'e'#defineWHILE'w'#defineFOR'r'#defineMAIN'm'#definePRINTF'p'#defineINCLUDE'u'#defineFLOAT'l'#defineKEYNUM10#defineBOUNDNUM8#defineOPERNUM7#defineVALUENUM100/************************************************************************//*datatype*//************************************************************************/structbianliang{intid;charname[20];intvalue;};structchangliang{intdata;};charKeyWord[KEYNUM][8]={int,void,if,else,while,for,main,printf,include,float};charBoundary[BOUNDNUM]={'',';',',','(',')','#','{','}'};charOperation[OPERNUM]={'+','-','*','/','=','',''};charLogogram[KEYNUM]={INT,VOID,IF,ELSE,WHILE,FOR,MAIN,PRINTF,INCLUDE,FLOAT};charValue[VALUENUM][8];intValue_n=0;intErrorLine=0;intQuotationCount=0;//计算引号的数量intQuotationFlag=0;//引号数量为奇数时为1,偶数时为0charLastOpera=0;//用于判断==号存前一个=号/************************************************************************//*functiondescribe*//************************************************************************/intisKeyWord(char*s)//是关键字返回1,否则返回0{inti;for(i=0;iKEYNUM;i++){if(strcmp(KeyWord[i],s)==0){returni+1;}}return0;}intisBoundary(charch)//是界符{inti;for(i=0;iBOUNDNUM;i++){if(Boundary[i]==ch){returni+1;}}return0;}intisOperation(charch)//是运算符{inti;for(i=0;iOPERNUM;i++){if(Operation[i]==ch){returni+1;}}return0;}intisDigit(char*s)//是数字{inti,l=strlen(s);for(i=0;il;i++){if(!isdigit(s[i])){break;}}if(i=l){return1;}else{if('.'==s[i]){i++;for(;il;i++){if(!isdigit(s[i])){break;}}}else{return0;}if(i=l)return1;elsereturn0;}}intisDotH(char*s)//是头文件{inti,l=strlen(s);for(i=0;il;i++){if(!isalpha(s[i])){break;}}if(i=l){return0;}else{if('.'==s[i]){i++;if(s[i]=='h')return1;elsereturn0;}elsereturn0;}}intisWord(char*s)//是标识符{inti=0,l=strlen(s);if(isalpha(s[i])){i++;for(;il;i++){if(!isalnum(s[i])){break;}}}if(i=l){return1;}elsereturn0;}voidisWhat(int*i,char*str,FILE*fileOutput,FILE*fileOutput2)//判断字符串,并把相应类型存入文件{intn;structbianliangtemp;structchangliangt;FILE*file=fopen(num.txt,ab);if(1!=*i){str[--(*i)]='\0';if(n=isKeyWord(str)){fputs(str,fileOutput);fputc('\t',fileOutput);fputc(KEY_WORD,fileOutput);fputc('\t',fileOutput);if(isKeyWord(str)9)fputc(isKeyWord(str)+'a'-10,fileOutput);elsefputc(isKeyWord(str)+'0',fileOutput);fputc('\t',fileOutput);fputc(Logogram[n-1],fileOutput);fputc('\n',fileOutput);}elseif(isWord(str)){fputs(str,fileOutput);fputc('\t',fileOutput);fputc(WORD,fileOutput);fputc('\t',fileOutput);for(n=0;nValue_n;n++){if(0==strcmp(Value[n],str))break;}if(n==Value_n){n=++Value_n;strcpy(Value[n-1],str);temp.id=n-1;strcpy(temp.name,str);temp.value=0;fwrite(&temp,sizeof(structbianliang),1,fileOutput2);fputc('0'+n,fileOutput);}else{fputc('0'+n+1,fileOutput);}fputc('\t',fileOutput);fputc('i',fileOutput);fputc('\n',fileOutput);}elseif(isDotH(str)){fputs(str,fileOutput);fputc('\t',fileOutput);fputc('\t',fileOutput);fputc('\t',fileOutput);fputc(DOTH,fileOutput);fputc('\n',fileOutput);}elseif(isDigit(str)){fputs(str,fileOutput);fputc('\t',fileOutput);fputc('\t',fileOutput);fputc('\t',fileOutput);fputc(DIGIT,fileOutput);fputc('\n',fileOutput);t.data=atoi(str);fwrite(&t,sizeof(structchangliang),1,file);}(*i)=0;}fclose(file);}voidwriteMessage(FILE*f){fputs(字符\t类型\t附加值\t缩写\n,f);}voiderror(){printf(%dlineerror!\n,ErrorLine);exit(1);}/************************************************************************//*main*//************************************************************************/voidmain(){FILE*fileInput;FILE*fileOutput;FILE*fileOutput2;charch,qtemp;charstr[20];inti;intflag=0;fileInput=fopen(1.cpp,r);fileOutput=fopen(object.txt,w);fileOutput2=fopen(variable.txt,w);writeMessage(fileOutput);if(NULL!=fileInput){i=0;do{ch=fgetc(fileInput);str[i++]=ch;if(isBoundary(ch)){if(1==isBoundary(ch)){QuotationCount++;if(0==QuotationCount%2){QuotationFlag=0;}elseQuotationFlag=1;}isWhat(&i,str,fileOutput,fileOutput2);fputc(ch,fileOutput);fputc('\t',fileOutput);fputc('B',fileOutput);fputc('\t',fileOutput);if(isBoundary(ch)9)fputc(isBoundary(ch)+'a'-10,fileOutput);elsefputc(isBoundary(ch)+'0',fileOutput);fputc('\t',fileOutput);fputc(Boundary[isBoundary(ch)-1],fileOutput);fputc('\n',fileOutput);i=0;}elseif(isOperation(ch)){if(0==QuotationCount%2){isWhat(&i,str,fileOutput,fileOutput2);if('='==ch){qtemp=fgetc(fileInput);if('='==qtemp){fputc('=',fileOutput);fputc('=',fileOutput);flag=1;}else{fputc('=',fileOutput);fseek(fileInput,-1,SEEK_CUR);flag=0;}}elsefputc(ch,fileOutput);fputc('\t',fileOutput);fputc(OPERATION,fileOutput);fputc('\t',fileOutput);if(isOperation(ch)9)fputc(isOperation(ch)+'a'-10,fileOutput);elsefputc(isOperation(ch)+'0',fileOutput);fputc('\t',fileOutput);if(flag){fputc('q',fileOutput);}elsefputc(Operation[isOperation(ch)-1],fileOutput);fputc('\n',fileO

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功