标签:
#include<stdio.h>
#include<string.h>
char aa[80],bb[8]={‘\0‘};
char ch;
int a,p,m=0,n,row,sum=0;
char *rwtab[6]={"begin","if","then","while","do","end"};
char *cc[6]={"=","+","(",")","<",">"};
char *dd[7]={"~","!","#","for","?","{","}"};
void scaner()
{
ch=aa[p++];
while(ch==‘ ‘)
{
ch=aa[p];
p++;
}
if((ch>=‘a‘&&ch<=‘z‘)||(ch>=‘A‘&&ch<=‘Z‘)) //可能是标示符或者变量名
{
m=0;
while((ch>=‘0‘&&ch<=‘9‘)||(ch>=‘a‘&&ch<=‘z‘)||(ch>=‘A‘&&ch<=‘Z‘))
{
bb[m++]=ch;
ch=aa[p++];
}
bb[m++]=‘\0‘;
p--;
a=10;
for(n=0;n<6;n++) //将识别出来的字符和已定义的标示符作比较,
if(strcmp(bb,rwtab[n])==0)
{
a=n+1;
break;
}
}
else if((ch>=‘0‘&&ch<=‘9‘)) //数字
{
{
sum=0;
while((ch>=‘0‘&&ch<=‘9‘))
{
sum=sum*10+ch-‘0‘;
ch=aa[p++];
}
}
p--;
a=11;
if(sum>32767)
a=-1;
}
else switch(ch) //其他字符
{
case‘<‘:m=0;bb[m++]=ch;
ch=aa[p++];
if(ch==‘>‘)
{
a=21;
bb[m++]=ch;
}
else if(ch==‘=‘)
{
a=22;
bb[m++]=ch;
}
else
{
a=23;
p--;
}
break;
case‘>‘:m=0;bb[m++]=ch;
ch=aa[p++];
if(ch==‘=‘)
{
a=24;
bb[m++]=ch;
}
else
{
a=20;
p--;
}
break;
case‘:‘:m=0;bb[m++]=ch;
ch=aa[p++];
if(ch==‘=‘)
{
a=18;
bb[m++]=ch;
}
else
{
a=17;
p--;
}
break;
case‘*‘:a=13;bb[0]=ch;
break;
case‘/‘:a=14;bb[0]=ch;
break;
case‘+‘:a=15;bb[0]=ch;
break;
case‘-‘:a=16;bb[0]=ch;
break;
case‘=‘:a=25;bb[0]=ch;
break;
case‘;‘:a=26;bb[0]=ch;
break;
case‘(‘:a=27;bb[0]=ch;
break;
case‘)‘:a=28;bb[0]=ch;
break;
case‘#‘:a=0;bb[0]=ch;
break;
case‘\n‘:a=-2;break;
default: a=-1;break;
}
}
int main()
{
p=0;
row=1;
printf("Please input string:");
do
{
get(ch);
aa[p++]=ch;
}
while(ch!=‘#‘);
p=0;
do
{
scaner();
switch(a)
{
case 11: printf("");
break;
case -1: printf("‘\n‘"); break;
case -2: row=row++;break;
default: printf("");break;
}
}
while (a!=0);
}
标签:
原文地址:http://www.cnblogs.com/qq1141100952com/p/5955691.html