#include<stdio.h> #include<string.h> #define N 80 void scaner(char a[],char *c[]) { int i=0,j=0,n=0,m=0; char b[N]; while(a[i]!='#') { if(a[i]>=65&&a[i]<=122) { j=0; while((a[i]>=65&&a[i]<=122)||(a[i]>='0'&&a[i]<='9')||a[i]=='_') { b[j]=a[i]; j++; i++; } i--; b[j]='\0'; for(n=0;n<6;n++) { if(strcmp(b,c[n])==0) { printf("<%d,%s>\n",n+1,c[n]); m=1; break; } } if(m==0) printf("<10,%s>\n",b); } else { switch(a[i]) { case '+': printf("<13,+>\n");break; case '-': printf("<14,->\n");break; case '*': printf("<15,*>\n");break; case '/': printf("<16,/>\n");break; case ':':if(a[i+1]=='=') { printf("<18,:=>\n"); i++; } else printf("<17,:>\n"); break; case '<': if(a[i+1]=='=') { printf("<21,<=>\n"); i++; } else if(a[i+1]=='>') { printf("<22,<>>\n"); i++; } else printf("<20,<>\n"); break; case '>': if(a[i+1]=='=') { printf("<24,>=>\n"); i++; } else printf("<23,>>\n"); break; case '=': printf("<25,=>\n");break; case ';': printf("<26,;>\n");break; case '(': printf("<27,(>\n");break; case ')': printf("<28,)>\n");break; case' ': break; case'\n': break; default: if(a[i]>='0'&&a[i]<='9') printf("<11,%c>\n",a[i]); else printf("'%c'输入错误!\n",a[i]); break; } } i++; } if(a[i]=='#') printf("<0,#>\n"); } main() { int i; char a[N]; char *c[6]={"begin","if","then","while","do","end"}; printf("请输入源程序:(以#结束)\n"); for(i=0;i<N;i++) { scanf("%c",&a[i]); if(a[i]=='#') break; } a[i+1]='\0'; i=0; printf("识别出的种别码为:\n"); scaner(a,c); }