#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct node{ char *string; struct node *next; }NODE; int main() { int i; char *ch; char *delim=":;,+-*/="; char input[20]; char Keywords[][10]={{"auto"},{"double"},{"int"},{"struct"},{"break"},{"else"},{"long"},{"switch"},{"case"}, {"enum"},{"register"},{"typedef"},{"char"},{"extern"},{"return"},{"union"},{"const"},{"float"},{"short"},{"unsigned"},{"continue"}, {"for"},{"signed"},{"void"},{"default"},{"goto"},{"sizeof"},{"volatile"},{"do"},{"if"},{"while"},{"static"},{"+"},{"-"},{"*"},{"/"},{":"}, {":="},{"<"},{"<="},{"<>"},{">"},{">="},{"="},{":"},{"("},{")"},{"#"},{"{"},{"}"}}; char number[]="0123456789"; char biaowords[]="abcdefghijklmnopqrstuvwxyz"; NODE *head=NULL; NODE *thekeywords=NULL; NODE *biaoshi=NULL; NODE *yunsuanfu=NULL; NODE *shuzi=NULL; NODE *jiefu=NULL; NODE *p=NULL; NODE *q=NULL; head=(NODE*)malloc(sizeof(NODE)); head->next=NULL; thekeywords=(NODE*)malloc(sizeof(NODE)); thekeywords->next=NULL; biaoshi=(NODE*)malloc(sizeof(NODE)); biaoshi->next=NULL; yunsuanfu=(NODE*)malloc(sizeof(NODE)); yunsuanfu->next=NULL; jiefu=(NODE*)malloc(sizeof(NODE)); jiefu->next=NULL; shuzi=(NODE*)malloc(sizeof(NODE)); shuzi->next=NULL; //scanf("%s",input); do { scanf("%s\n",input); i=0; while(input[i] !='\0') { //printf("char=%c\n",input[i]); if(input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '=') { //printf("\nhello\n"); p=(NODE*)malloc(sizeof(NODE)); p->next=NULL; p->string=(char*)malloc(sizeof(char)*2); p->string[0]='\0'; strncpy(p->string,&input[i],1); p->next=yunsuanfu->next; yunsuanfu->next=p; // printf("yunsuanfu is %c\n",input[i]); } else if(ispunct(input[i]) && input[i] != '~') { //ch=(char*)malloc(sizeof(NODE)); p=(NODE*)malloc(sizeof(NODE)); p->next=NULL; p->string=(char*)malloc(sizeof(char)*2); p->string[0]='\0'; strncpy(p->string,&input[i],1); p->next=jiefu->next; jiefu->next=p; } i++; } ch=strtok(input,delim); do { if(input[0] =='~') break; q=head; p=(NODE*)malloc(sizeof(NODE)); p->next=NULL; p->string=(char*)malloc(sizeof(input)); p->string[0]='\0'; strcat(p->string,ch); while(q->next !=NULL) q=q->next; p->next=q->next; q->next=p; }while(ch=strtok(NULL,delim)); }while(input[0] !='~'); i=0; p=head->next; printf("\nhead:"); while(p !=NULL) { printf("%s\t",p->string); p=p->next; } p=head->next; while(p !=NULL) { q=(NODE*)malloc(sizeof(NODE)); q->next=NULL; if(strspn(p->string,number)) { q->string=p->string; q->next=shuzi->next; shuzi->next=q; } else { for(i=0;i<=49;i++) { if(strcmp(p->string,Keywords[i])==0) { q->string=Keywords[i]; if(i>=0 && i<=31) { q->next=thekeywords->next; thekeywords->next=q; } else if(i>=32 && i<=35) { q->next=yunsuanfu->next; yunsuanfu->next=q; } break; } } } if(i>49) { if(strspn(p->string,biaowords)) { q->string=p->string; q->next=biaoshi->next; biaoshi->next=q; } } p=p->next; } printf("\nthekeywords:\n"); p=thekeywords->next; while(p != NULL) { printf("%s\t",p->string); p=p->next; } printf("\nyunsuanfu:\n"); p=yunsuanfu->next; while(p != NULL) { printf("%s\t",p->string); p=p->next; } printf("\nbiaoshi:\n"); p=biaoshi->next; while(p != NULL) { printf("%s\t",p->string); p=p->next; } printf("\njiefu:\n"); p=jiefu->next; while(p != NULL) { printf("%s\t",p->string); p=p->next; } printf("\nshuzi:\n"); p=shuzi->next; while(p != NULL) { printf("%s\t",p->string); p=p->next; } return 0; }
<字母>:S-〉A|AAnode
A->{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,w,y,spa
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z}
<数字>:S->A|AA|A0|A.code
A->{1,2,3,4,5,6,7,8,9}
<整数常数> :S->A|AA|A0blog
A->{1,2,3,4,5,6,7,8,9}input
<标识符>:S->A|A_|ABstring
A->{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,w,y,it
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z}io
B->{0,1,2,3,4,5,6,7,8,9}class
<关键字>:S->{int,float,double,long,unsige,static,include,define,for,if,whlie,do,for,...}float
<运算符>:S->{+,-,*,/,%}<界符>; S->{; , ( , ) , , }