编程实验一 词法分析程序
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node{ char zifuji[20]; struct node *next; }NODE; typedef struct stack{ char num[20]; int size; }STACK; void cmp(char *ch); int shizi(char ch[]); //字转词 void zizhuanci(NODE *node){ NODE *p; p=node->next; while(p!=NULL){ cmp(p->zifuji); p=p->next; } }
//判断词义 void cmp(char *ch){ int key; if(strcmp(ch,"begin")==0){ printf("<%s \t 1 >\n",ch); } else if(strcmp(ch,"if")==0){ printf("<%s \t 2 >\n",ch); } else if(strcmp(ch,"then")==0){ printf("<%s \t 3 >\n",ch); } else if(strcmp(ch,"while")==0){ printf("<%s \t 4 >\n",ch); } else if(strcmp(ch,"do")==0){ printf("<%s \t 5 >\n",ch); } else if(strcmp(ch,"end")==0){ printf("<%s \t 6 >\n",ch); } else if(strcmp(ch,"+")==0){ printf("<%s \t 13>\n",ch); } else if(strcmp(ch,"-")==0){ printf("<%s \t 14>\n",ch); } else if(strcmp(ch,"*")==0){ printf("<%s \t 15>\n",ch); } else if(strcmp(ch,"/")==0){ printf("<%s \t 16>\n",ch); } else if(strcmp(ch,":")==0){ printf("<%s \t 17>\n",ch); } else if(strcmp(ch,":=")==0){ printf("<%s \t 18>\n",ch); } else if(strcmp(ch,"<")==0){ printf("<%s \t 20>\n",ch); } else if(strcmp(ch,"<=")==0){ printf("<%s \t 21>\n",ch); } else if(strcmp(ch,"<>")==0){ printf("<%s \t 22>\n",ch); } else if(strcmp(ch,">")==0){ printf("<%s \t 23>\n",ch); } else if(strcmp(ch,">=")==0){ printf("<%s \t 24>\n",ch); } else if(strcmp(ch,"=")==0){ printf("<%s \t 25>\n",ch); } else if(strcmp(ch,";")==0){ printf("<%s \t 26>\n",ch); } else if(strcmp(ch,"(")==0){ printf("<%s \t 27>\n",ch); } else if(strcmp(ch,")")==0){ printf("<%s \t 28>\n",ch); } else if(strcmp(ch,"#")==0){ printf("<%s \t 0>\n",ch); } else{ key=shizi(ch); if(key==1) printf("<%s \t 12>\n",ch,key); else if(key==2) printf("<%s \t 11>\n",ch,key); else printf("<%s \t 错误>\n",ch,key); } } //判断数字词义 int shizi(char ch[]){ int i; int key=0; for(i=0;i<=sizeof(ch);i++) if((ch[i]>='0'&&ch[i]<='9')||(ch[i]>='a'&&ch[i]<='z')) key=1; else if(ch[i]>='0'&&ch[i]<='9') key=2; return key; } //字分成词 int word_splitter(char* str,NODE *node){ NODE *p,*q; p=node; int length = strlen(str); char aword[20]; int number = 0; int start = 0; memset(aword, 0, 20); for(int i = start; i < length; ++i){ if(str[i] == ' '){ memcpy(aword, str + start, i - start); ++number; //printf("%d\t%s\n", number, aword); q=(NODE *)malloc(sizeof(NODE)); q->next=NULL; strcpy(q->zifuji,aword); p->next=q; p=q; memset(aword, 0, 20); start = i + 1; } } if(start < length) { memcpy(aword, str + start, length - start); ++number; //printf("%d\t%s\n", number, aword); q=(NODE *)malloc(sizeof(NODE)); q->next=NULL; strcpy(q->zifuji,aword); p->next=q; p=q; } return number; } int main(){ NODE *node,*p,*q; int i; STACK *stack; int size=-1; node = p =(NODE *)malloc(sizeof(NODE)); stack=(STACK *)malloc(sizeof(STACK)); printf("请输入:\n"); for(i=0;i<1;i++){ q=(NODE *)malloc(sizeof(NODE)); gets(node->zifuji); q=p->next; puts(node->zifuji); //printf("%d",sizeof(node->zifuji)); } i=word_splitter(node->zifuji,node); /* p=node->next; while(p){ puts(p->zifuji); printf("\n"); p=p->next; } */ zizhuanci(node); return 0; }