www.pudn.com > cifafenxiqi.rar > SCANNERBYME.C
#include#include #include #include #include #include void sort(char ch); char getfirstch(); int iskeyword(char letter[]); int likeoperator(char ch); int isdelimer(char ch); void handlecom(char ch); void recogid(char ch); void recogstr(); void recogdig(char ch); void recogcomparison(char ch); void goback(); char getonech(); FILE *fp_source,*fp_token; int row=1; int column=0; int flog=0; char *keyword[]={"double","else","float","for","if","int","long","while","return","void","#","define"}; char *operator[]={"+","-","*","/"}; char *comparison[]={"<","<=","==",">",">=","!="}; char *delimer[]={",",";",":",".","(",")","{","}"}; void main() { if((fp_source=fopen("source.txt","r"))==NULL) { printf("Can't open this file source.txt!\n"); exit(0); } if((fp_token=fopen("token.txt","w"))==NULL) { printf("Can't open this file token.txt!\n"); exit(0); } while(!feof(fp_source)) { char firstch; firstch=getfirstch(); sort(firstch); } fclose(fp_source); fclose(fp_token); } void sort(char ch) { if(flog==0) { if(isalpha(ch)||ch=='#') recogid(ch); else if(isdigit(ch)) recogdig(ch); else if(ch=='"') recogstr(); else if(isdelimer(ch)) ; else if(ch=='<'||ch=='>'||ch=='!'||ch=='=') recogcomparison(ch); else if(likeoperator(ch)) handlecom(ch); } else if(likeoperator(ch)) handlecom(ch); } char getfirstch() { char ch; ch=getonech(); while(isspace(ch)) { if(ch=='\n') row++,column=0; ch=getonech(); } return ch; } char getonech() { column++; return fgetc(fp_source); } void goback() { fseek(fp_source,-1L,1); column--; } int iskeyword(char letter[]) { int i; for(i=0;i<12;i++) if(strcmp(keyword[i],letter)==0) return 1; return 0; } int likeoperator(char ch) { int i; for(i=0;i<4;i++) if(*operator[i]==ch) return 1; return 0; } void recogcomparison(char ch) { if(getonech()!='=') { if(ch=='=') fprintf(fp_token,"(fuzhuhao:%c,-)\n",ch); else fprintf(fp_token,"(comparison:%c,-)\n",ch); goback(); } else { fprintf(fp_token,"(comparison:%c=,-)\n",ch); } } int isdelimer(char ch) { int i; for(i=0;i<8;i++) if(*delimer[i]==ch) { fprintf(fp_token,"(delimer:%c,-)\n",ch); return 1; } return 0; } void recogid(char ch) { char letter[20]; int i=0; if(ch!='#') { while(isalnum(ch)!=0) { letter[i++]=ch; ch=getonech(); } letter[i]='\0'; goback(); if(iskeyword(letter)==1) fprintf(fp_token,"(keyword: %s,-)\n",letter); else fprintf(fp_token,"(identifier,%s)\n",letter); } else fprintf(fp_token,"(keyword: %c,-)\n",ch);; } void recogdig(char ch) { char num[20]; int i=0; while (isdigit(ch)!=0) { num[i++]=ch; ch=getonech(); } num[i]='\0'; goback(); if(isalpha(ch)==1) fprintf(fp_token,"(error,row:%d,column:%d)\n",row,column); else fprintf(fp_token,"(number,%s)\n",num); } void recogstr() { int i=0; char ch; char str[20]; ch=getonech(); while(ch!='"') { str[i++]=ch; ch=getonech(); } str[i]='\0'; fprintf(fp_token,"(str,%s)\n",str); } void handlecom(char ch) { switch(ch) { case '/': if(getonech()=='*') { flog--; if(flog!=-1) fprintf(fp_token,"(error,row:%d,column:%d)\n",row,column),flog=0; } else { fprintf(fp_token,"(operator:%c,-)\n",ch); goback(); flog=0; } break; case '*': if(getonech()=='/') { flog++; if(flog!=0) fprintf(fp_token,"(error,row:%d,column:%d)\n",row,column),flog=0; } else { fprintf(fp_token,"(operator:%c,-)\n",ch); goback(); flog=0; } break; default : fprintf(fp_token,"(operator:%c,-)\n",ch); break; } }