从左至右地对源程序进行扫描,按照语言的词法规则识别各类单词,并产生以{种别码,属性}为格式的结果。
<字母> => a|b|c...x|y|z
<数字> => 0|1|2...7|8|9
<数字常数> => <数字>|<数字常数><数字>|<数字常数>.<数字常数>
<标识符> => <字母>|<标识符><字母>|<标识符><数字>
<关键字> => begin|if|then|while|do|end
<运算符> => +|-|*...>|>=|=
<界符> => ;|(|)|#
#include<stdio.h> #include<string.h> int Distinguish(char *input,char *output,int *pos); #define Max 1000 void main () { char input[Max]="",output[Max*5]=""; int pos=0; printf("input:"); gets(input); while(Distinguish(input,output,&pos)); printf("%s",output); } int Distinguish(char *input,char *output,int *pos) { while(input[*pos]=='\n'||input[*pos]=='\t'||input[*pos]==' ')(*pos)++; if(input[*pos]>='A'&&input[*pos]<='z') { int i=1; for((*pos)++;input[*pos]>='A'&&input[*pos]<='z'||input[*pos]>='0'&&input[*pos]<='9';i++,(*pos)++); if(!strncmp(&input[*pos]-i,"begin",5)) { strcat(output,"{1,begin}\n"); } else if(!strncmp(&input[*pos]-i,"if",2)) { strcat(output,"{2,if}\n"); } else if(!strncmp(&input[*pos]-i,"then",4)) { strcat(output,"{3,then}\n"); } else if(!strncmp(&input[*pos]-i,"while",5)) { strcat(output,"{4,while}\n"); } else if(!strncmp(&input[*pos]-i,"do",2)) { strcat(output,"{5,do}\n"); } else if(!strncmp(&input[*pos]-i,"end",3)) { strcat(output,"{6,end}\n"); } else { strcat(output,"{10,"); strncat(output,&input[*pos]-i,i); strcat(output,"}\n"); } } else if(input[*pos]>='0'&&input[*pos]<='9') { int i=1; for((*pos)++;input[*pos]>='0'&&input[*pos]<='9';i++,(*pos)++); strcat(output,"{11,"); strncat(output,&input[*pos]-i,i); strcat(output,"}\n"); } else { switch(input[*pos]) { case '+': { strcat(output,"{13,+}\n"); (*pos)++; break; } case '-': { strcat(output,"{14,-}\n"); (*pos)++; break; } case '*': { strcat(output,"{15,*}\n"); (*pos)++; break; } case '/': { strcat(output,"{16,/}\n"); (*pos)++; break; } case '(': { strcat(output,"{27,(}\n"); (*pos)++; break; } case ')': { strcat(output,"{28,)}\n"); (*pos)++; break; } case '#': { strcat(output,"{0,#}\n"); (*pos)++; return 0; } case ';': { strcat(output,"{26,;}\n"); (*pos)++; break; } case '=': { strcat(output,"{25,=}\n"); (*pos)++; break; } case ':': { (*pos)++; if(input[*pos]=='=') { strcat(output,"{18,:=}\n"); (*pos)++; } else { strcat(output,"{17,:}\n"); } break; } case '<': { (*pos)++; if(input[*pos]=='=') { strcat(output,"{21,<=}\n"); (*pos)++; } else if(input[*pos]=='>') { strcat(output,"{22,<>}\n"); (*pos)++; } else { strcat(output,"{20,<}\n"); } break; } case '>': { (*pos)++; if(input[*pos]=='=') { strcat(output,"{24,>=}\n"); (*pos)++; } else { strcat(output,"{23,>}\n"); } break; } } } return 1; }
来源:https://www.cnblogs.com/hehe2333/p/5925794.html