标签:
#include<stdio.h> #include<string.h> char ch,token[8],prog[80]; int syn,p,m,n,sum; char *rwtab[6]={"begin","if","then","while","do","end"}; void scaner(); main() { p=0; printf("请输入一个字符串(输入#表示结束):"); do{ scanf("%c",&ch); prog[p++]=ch; }while(ch!=#); p=0; do{ scaner(); switch(syn) { case 11:printf("%-10d%5d\n",sum,syn); break; case -1:printf("输入的字符串有误!\n"); getch(); exit(0); default:printf("%-10s%5d\n",token,syn); break; } }while(syn!=0); getch(); } void scaner() { sum=0; for(m=0;m<8;m++) token[m++]=NULL; ch=prog[p++]; m=0; while((ch==‘ ‘)||(ch==‘\n‘)) ch=prog[p++]; if(((ch<=‘z‘)&&(ch>=‘a‘))||((ch<=‘Z‘)&&(ch>=‘A‘))) { while(((ch<=‘z‘)&&(ch>=‘a‘))||((ch<=‘Z‘)&&(ch>=‘A‘))||((ch>=‘0‘)&&(ch<=‘9‘))) { token[m++]=ch; ch=prog[p++]; } p--; syn=10; for(n=0;n<6;n++) if(strcmp(token,rwtab[n])==0) { syn=n+1; break; } } else if((ch>=‘0‘)&&(ch<=‘9‘)) { while((ch>=‘0‘)&&(ch<=‘9‘)) { sum=sum*10+ch-‘0‘; ch=prog[p++]; } p--; syn=11; } else switch(ch) { case ‘<‘:token[m++]=ch; ch=prog[p++]; if(ch==‘=‘) { syn=22; token[m++]=ch; } else { syn=20; p--; } break; case ‘>‘:token[m++]=ch; ch=prog[p++]; if(ch==‘=‘) { syn=24; token[m++]=ch; } else { syn=23; p--; } break; case ‘+‘: token[m++]=ch; ch=prog[p++]; if(ch==‘+‘) { syn=17; token[m++]=ch; } else { syn=13; p--; } break; case ‘-‘:token[m++]=ch; ch=prog[p++]; if(ch==‘-‘) { syn=29; token[m++]=ch; } else { syn=14; p--; } break; case ‘!‘:ch=prog[p++]; ch=prog[p++]; if(ch==‘=‘) { syn=21; token[m++]=ch; } else { syn=31; p--; } break; case ‘=‘:token[m++]=ch; ch=prog[p++]; if(ch==‘=‘) { syn=25; token[m++]=ch; } else { syn=18; p--; } break; } }
标签:
原文地址:http://www.cnblogs.com/229zyx/p/5936575.html