标签:
#include<stdio.h> #include<string.h> #define max 100 int n=0,i=0; char token[max]; char a; char word[max]; void fuhao(); void shuzi(); main() { char *rwtab[6]={"begin","if","then","while","do","end"}; int p=0,j=0,k=0,m=0,q=1; int key=0; printf("请输入源程序(以#键结束):"); do{ scanf("%c",&a); word[p]=a; p++; }while(a!=‘#‘); //关键字的判断 while(word[i]!=‘#‘) { while((word[i]>=‘a‘&&word[i]<=‘z‘)||(word[i]>=‘A‘&&word[i]<=‘Z‘)||(word[i]>=‘0‘&&word[i]<=‘9‘)) { if((word[i]>=‘a‘&&word[i]<=‘z‘)||(word[i]>=‘A‘&&word[i]<=‘Z‘)||(word[i]>=‘0‘&&word[i]<=‘9‘)) { token[n]=word[i]; n++; token[n]=‘\0‘; i++; if(word[i]>=‘0‘&&word[i]<=‘9‘) q++; } for(k=0;k<6;k++) { key=0; if(strcmp(rwtab[k],token)==0) { printf("%s %d\n",rwtab[k],k+1); key=1; break; } } } if((word[i-q]>=‘a‘&&word[i-q]<=‘z‘)||(word[i-q]>=‘A‘&&word[i-q]<=‘Z‘)) { if(key==0) { printf("%s 10\n",token); } } key=1; i=i+1; n=0; } fuhao(); shuzi(); } //特殊符号的判断 void fuhao() { int p=0,sign=0; for(p=0;p<max;p++) { switch(word[p]) { case ‘+‘: printf("+ 13\n"); break; case ‘-‘: printf("- 14\n"); break; case ‘*‘: printf("* 15\n"); break; case ‘/‘: printf("/ 16\n"); break; case ‘:‘: if(word[p+1]==‘=‘) { printf(":= 18\n"); p++; } else printf(": 17\n"); break; case ‘<‘: if(word[p+1]==‘=‘) { printf("<= 21\n"); sign=1; p++; } else if(word[p+1]==‘>‘) { printf("<> 22\n"); sign=1; p++; } else if(sign==0) printf("< 20\n"); break; case ‘>‘: if(word[p+1]==‘=‘) { printf(">= 24\n"); p++; } 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 ‘#‘: printf("# 29\n"); break; } } } //数字的判断 void shuzi() { int p=0,j=0,t=0,key=0,q=1; char Shu[100]; while(word[p]!=‘#‘) { key=0; j=0; while(word[p]>=‘0‘&&word[p]<=‘9‘) { Shu[j]=word[p]; p++; j++; key=1; q++; } if((word[p-q]>=‘a‘&&word[p-q]<=‘z‘)||(word[p-q]>=‘A‘&&word[p-q]<=‘Z‘)) { key=0; q=1; } if(key==1) { for(t=0;t<j;t++) { printf("%c",Shu[t]); } printf(" 11\n"); j=0; } p++; } }
标签:
原文地址:http://www.cnblogs.com/hanqilin/p/4878081.html