资源描述:
《词法分析程序源码.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、词法分析程序源码#include#include#include#includeFILE*in,*out;//文件输入输出指针intnum=0;//词的数量intquotationFlag=0,bracketFlag=0;//quotationFlag弓丨号标识,bracketFlag括弧标识voidsubletterhandler(char*1etter)//字母处理子程序if(strcmp(1etter,〃const〃)=0){num++;fprintf(out,〃%dCONSTTK%s〃,num,1ett
2、er);}elseif(strcmp(letter,nt〃)==0){num++;fprintf(out,〃%dINTTK%srT,num,letter);}elseif(strcmp(letter,z,float?,)==0){num++;fprintf(out,〃%dFLOATTK%s〃,num,1etter);}elseif(strcmp(letter,/,char/,)==0){num++;fprintf(out,〃%dCHARTK%s〃,num,1etter);elseif(strcmp(letter,/zmain,z)=0){num++;fprintf(out,〃%d
3、MAINTK%s,z,num,letter);elseif(strcmp(letter,〃if〃)=0){num++;fprintf(out,〃%dIFTK%s〃,num,letter);}elseif(strcmp(letter,〃else〃)=0){num++;fprintf(out,〃%dELSETK%s,z,num,letter);elseif(strcmp(letter,"do〃)=0){num++;fprintf(out,〃%dDOTK%s〃,num,letter);elseif(strcmp(letter,〃switch")=0){num++;fprintf(
4、out,〃%dSWITCHTK%s〃,num,letter);elseif(strcmp(letter,"case")二二0){num++;fprintf(out,"%dCASETK%s,z,num,letter);elseif(strcmp(letter,"default")=0){num++;fprintf(out,〃%dDEFAULTTK%srT,num,letter);elseif(strcmp(letter,〃for〃)=0){num++;fprintf(out,"%dFORTK%s〃,num,letter);elseif(strcmp(letter,〃scanf"
5、)=O){num++;fprintf(out,〃%dSCANFTK%s〃,num,letter);ijelseif(strcmp(letter,〃printf〃)=0){num++;fprintf(out,〃%dPRINTFTK%s",num,letter);ijelseif(strcmp(letter,〃returrT)=0){num++;fprintf(out,〃%dRETURNTK%s〃,num,letter);ijelse{//inta二0;intj;for(j=0;j<=strlen(letter);j++)if(letter[j]〉二'0’&&letter[j]〈二
6、'9')a=l;if(a=0){num++;fprintf(out,〃%dSTRCON%srT,num,letter);else{num++;fprintf(out,〃%dIDEN%s〃,num,letter);ijijcharletterhandler(charch){inti二0;charletter[50];if(quotationFlag==0){while(isalnum(ch)!=0){letter[i++]=ch;ch=fgetc(in);ijletter[i]=, ?;subletterhandler(letter);Ijelseif(quotationFlag=l
7、){while(ch!二'‘’){letter[i++]=ch;ch=fgetc(in);ijletter[i]=, ?;num++;fprintf(out,〃%dCHARCON%s〃,num,letter);Ijelse{while(ch!=,〃‘){letter[i++]=ch;ch二fgetc(in);ijletter[i]二' ';nuni++;fprintf(out,〃%dSTRCON%s〃,n