能夠識別標識符、關鍵字、數字和運算符,對注釋進行過濾,同時還能識別出程序錯誤。
使用說明:
本程序的輸入由當前目錄下的in.txt文件讀取輸入,輸出為一系列二元式#include#include #include //用指針而不是二維數組表示,這樣就不用指定字符串長度,只是不能修改指針所指向字符串的內容 char *key[]={"int","char","float","double","void","const","for","if","else","then","while","switch","break","main","return"}; char buffer[20];//存儲當前識別出的單詞 char *identifier[50]; char *num[50]; int ident_num;//標志符數 int number;//數字數 int judgement_num(char c) { if (c >= '0' && c <= '9') return 0; else if (c == '.') return 1; return -1; } int judge(char c){ if(c=='_') return 2; else if(c>='0'&&c<='9') return 1; else if(c>='a'&&c<='z'||c>='A'&&c<='Z') return 0; return -1; } //next_i,next_j分別代表著狀態轉換表move的當前狀態與接收到的字符 //width代表每一狀態可能遇到的狀態數,length代表終態集大小 int DFA(int begin,int move[],int width,int final[],int length,int(*judge)(char)){ int len=0,next_i,next_j,tmp; char next_char; memset(buffer,0,sizeof(buffer)); next_char=getchar(); next_i=begin; while((next_j=judge(next_char))!=-1){ tmp=next_i; next_i=move[next_i*width+next_j]; if(next_i==-1){ printf("move[%d][%d]has not next state\n",tmp,next_j); return -1; } buffer[len++]=next_char; next_char=getchar(); } ungetc(next_char,stdin); buffer[len]='\0'; for(int i=0;i '||c=='<'){ next_char=getchar(); if(next_char=='=') printf("(rlop,%c=)\n",c); else { printf("(rlop,%c)\n",c); ungetc(next_char,stdin); } } else if(c=='!'){ next_char=getchar(); if(next_char=='=') printf("(!=,_)\n"); else { printf("(not,_)\n"); ungetc(next_char,stdin); } } else if(c=='|'){ next_char=getchar(); if (next_char == '|') printf("(or,||)\n"); else { ungetc(next_char, stdin); } } else if(c=='&'){ next_char=getchar(); if (next_char == '&') printf("(and,&&)\n"); else { ungetc(next_char, stdin); } } else if(c=='='||c=='('||c==')'||c=='['||c==']'||c==';'||c==','||c=='{'||c=='}'){ printf("(%c,_)\n",c); } } void is_digit(){ int begin=0; int move[]={1,-1,1,2,2,-1}; int final[]={1,2}; int result=-1; result=DFA(begin,move,2,final,2,judgement_num); if(result==-1){ printf("digit DFA error\n"); exit(-1); } else if(result==0){ //printf("%s\n",buffer); for(int i=0;i