#include<stdio.h> #include<string.h> char prog[800],token[20]; char ch; int syn,p,m,sum; char *rwtab[6]={"begin","if","then","while","do","end"}; scaner() { int n; for(n=0;n<20;n++) m=0; sum=0; ch=prog[p++]; while(ch>='a'&&ch<='z') { ch=prog[p++]; } if(ch>='a'&&ch<='z'||ch>='0'&&ch<='9') { token[m++]=ch; ch=prog[p++]; } syn=10; p--; for(n=0;n<6;n++)//标识符是否为关键词 { if(strcmp(token,rwtab[n])==0) { syn=n+1; break; } else if { if(ch>='0'&&ch<='9') { sum=sum*10+(ch-'0'); ch=prog[p++]; } syn=11; p--; } else { switch(ch) { case '<': token[m++]=ch; ch=prog[p++]; if(ch='>') { syn=21; token[m++]=ch; } else if(ch=='=') { syn=22; token[m++]=ch; } else {syn=20; p--; }break; case '>':m=0; token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=24;token[m++]=ch; } else { syn=23;p--; } break; case ':': m=0;token[m++]=ch; ch=prog[p++]; if(ch=='=') { syn=18; token[m++]=ch; } else { syn=17; p--; } break; case '+':syn=13;token[0]=ch;break; case '-':syn=14;token[0]=ch;break; case '*':syn=15;token[0]=ch;break; case '/':syn=16;token[0]=ch;break; case '=':syn=25;token[0]=ch;break; case ';':syn=26;token[0]=ch;break; case '(':syn=27;token[0]=ch;break; case ')':syn=28;token[0]=ch;break; case '#':syn=0; token[0]=ch;break; default: syn=-1;token[0]=ch; } } main() { p=0; printf("\n请输入源程序段"); do{ ch=getchar(); prog[p++]=ch; }while(ch!='#'); p=0; do{ scaner();//读取一个单词符号,类型syn,单词token或sum switch(syn) { case 11:printf("\n(%d,%d)",syn,sum); break; case -1:printf("\n(%s,出错!)",token); break; default:printf("\n(%d,%s)",syn,token); } }while(syn!=0); System("pause"); }