符号与种别码对照表:
单词符号 种别码 单词符号 种别码
begin 1 : 17
if 2 := 18
then 3 < 20
while 4 <= 21
do ,{ 5 <> 22
end ,} 6 > 23
l(l|d)* 10 >= 24
dd* 11 = 25
+ 13 ; 26
- 14 ( 27
* 15 ) 28
/ 16 # 0
! 31 != 30
\ 32
#include <stdio.h> #include <string.h> char c[80],t[8],ch; int s,p,m,n,sum; char *w[6]={"begin","if","then","while","do","end"}; void scaner(); main() { p=0; printf("请输入一段字符(以'#'结束)"); do{ scanf("%c",&ch); c[p++]=ch; }while(ch!='#'); p=0; do{ scaner(); switch(s) { case 11:printf("< %-10d%5d>\n",sum,s); break; case -1:printf("你输入了错误的单词符号\n"); getch(); exit(0); default: printf("<%-10s%5d >\n",t,s); break; } }while(s!=0); getch(); } void scaner() { sum=0; for(m=0;m<8;m++) t[m++]=NULL; ch=c[p++]; m=0; while((ch==' ')||(ch=='\n')) ch=c[p++]; if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))) { while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9'))) { t[m++]=ch; ch=c[p++]; } p--; s=10; for(n=0;n<6;n++) if(strcmp(t,w[n])==0) { s=n+1; break; } } else if((ch>='0')&&(ch<='9')) { while((ch>='0')&&(ch<='9')) { sum=sum*10+ch-'0'; ch=c[p++]; } p--; s=11; } else switch(ch) { case '<':t[m++]=ch; ch=c[p++]; if(ch=='>') { s=22; t[m++]=ch; } else { s=20; p--; } if(ch=='=') { s=21; t[m++]=ch; } break; case '>':t[m++]=ch; ch=c[p++]; if(ch=='=') { s=24; t[m++]=ch; } else { s=23; p--; } break; case '+': s=13; t[m++]=ch; break; case '-': s=14; t[m++]=ch; break; case '!':ch=c[p++]; if(ch=='=') { s=30; t[m++]=ch; } else { s=31; p--; } break; case '=': s=25; t[m++]=ch; break; case '*': s=15; t[m++]=ch; break; case '/': s=16; t[m++]=ch; break; case '(': s=27; t[m++]=ch; break; case ')': s=28; t[m++]=ch; break; case '{': s=5; t[m++]=ch; break; case '}': s=6; t[m++]=ch; break; case ';': s=26; t[m++]=ch; break; case '\"': s=32; t[m++]=ch; break; case '#': s=0; t[m++]=ch; break; case ':':t[m++]=ch; ch=c[p++]; if(ch=='=') { s=18; t[m++]=ch; } else { s=17; p--; } break; default: s=-1; break; } t[m++]='\0'; }