标签:
#include<stdio.h> #include<string.h> #include<ctype.h> #define M 1000 struct key { char g[20]; }; typedef struct key KEY; int main() { KEY k[6]; int i=0,j=0; char a[M],b[M]; strcpy(k[0].g,"begin"); strcpy(k[1].g,"if"); strcpy(k[2].g,"then"); strcpy(k[3].g,"while"); strcpy(k[4].g,"do"); strcpy(k[5].g,"end"); printf("请输入源程序:\n"); gets(a); while(a[i]!=‘\0‘) { j=0; if((isalpha(a[i]))||(a[i]==‘_‘)) { while((a[i]!=‘ ‘)&&(a[i]!=‘\0‘)) { if((isalpha(a[i]))||(isdigit(a[i]))) { b[j]=a[i]; j++; i++; } else break; } if((a[i]==‘ ‘)||(a[i]==‘\0‘)) { b[j]=‘\0‘; if(strcmp(b,k[0].g)==0) printf("begin\t种别码 1\n"); else if(strcmp(b,k[1].g)==0) printf("if \t种别码 2\n"); else if(strcmp(b,k[2].g)==0) printf("then \t种别码 3\n"); else if(strcmp(b,k[3].g)==0) printf("while\t种别码 4\n"); else if(strcmp(b,k[4].g)==0) printf("do \t种别码 5\n"); else if(strcmp(b,k[5].g)==0) printf("end \t种别码 6\n"); else printf("%s \t种别码 10\n",b); } else { b[j]=‘\0‘; printf("%s \t种别码 10\n",b); i--; } } else if(isdigit(a[i])) { j=0; while(isdigit(a[i])) { b[j]=a[i]; j++; i++; } b[j]=‘\0‘; printf("%s \t种别码 11\n",b); i--; } else { switch(a[i]) { case ‘+‘: printf("%c \t种别码 13\n",a[i]);break; case ‘-‘: printf("%c \t种别码 14\n",a[i]);break; case ‘*‘: printf("%c \t种别码 15\n",a[i]);break; case ‘/‘: printf("%c \t种别码 16\n",a[i]);break; case ‘:‘: if(a[i+1]==‘=‘) { printf(":= \t种别码 18\n");i++;break;} else { printf("%c \t种别码 17\n",a[i]);break;} case ‘<‘: if(a[i+1]==‘=‘) { printf("<= \t种别码 21\n");i++;break;} if(a[i+1]==‘>‘) { printf("<> \t种别码 22\n");i++;break;} else printf("%c \t种别码 20\n",a[i]);break; case ‘>‘: if(a[i+1]==‘=‘) { printf(">= \t种别码 24\n");i++;break;} else printf("%c \t种别码 23\n",a[i]);break; case ‘=‘: printf("= \t种别码 25\n");break; case ‘;‘: printf("; \t种别码 26\n");break; case ‘(‘: printf("( \t种别码 27\n");break; case ‘)‘: printf(") \t种别码 28\n");break; case ‘#‘: printf("# \t种别码 0\n");break; default: printf("%c \t语法错误!\n",a[i]);break; } } i++; } }
标签:
原文地址:http://www.cnblogs.com/57rongjielong/p/4830384.html