#include<stdio.h> #include<string.h> #include<stdlib.h> #define Max 1000 int j=1; long g=0; char nchar[Max]={""}; int k=0; char ch=NULL; char m[30]={""}; main() { char cha; int p; printf("请输入一串字符串:"); while((cha=getchar())!=‘$‘) { nchar[g]=cha; g++; } do { p=getsym(); }while(p); } int getsym() { int i; char a[2]={""}; char word[][15]={"begin","do","end","if","then","while"}; int wsym[6]={1,2,3,4,5,6}; int ssym[9]={25,26,27,28,0,13,14,15,16}; char b[9]={‘=‘,‘;‘,‘(‘,‘)‘,‘#‘,‘+‘,‘-‘,‘*‘,‘/‘}; if(j==1&&k<g) { ch=nchar[k]; j++; k++; } while(ch==‘ ‘||ch==10||ch==9||ch==13) { if(k==g) { return 0; } ch=nchar[k]; k++; } if(ch>=‘a‘&&ch<=‘z‘) { while((ch>=‘a‘&&ch<=‘z‘)||(ch>=‘0‘&&ch<=‘9‘)||ch==‘_‘) { a[0]=ch; strcat(m,a); if(k==g) { break; } ch=nchar[k]; k++; } for(i=0;i<12;i++) { if(strcmp(m,&word[i][0])==0) break; } if(i<12) { sym=wsym[i]; printf("保留字%s 种别码%d\n",m,wsym[i]); } else { sym=11; printf("标识符%s 种别码11\n",m); } if(k==g) return 0; strcpy(m,""); return 1; } else if(ch>=‘0‘&&ch<=‘9‘) { while(ch>=‘0‘&&ch<=‘9‘) { a[0]=ch; strcat(m,a); if(k==g) { break; } ch=nchar[k]; k++; }
部分代码
原文:http://www.cnblogs.com/fzjKim/p/5925000.html