#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
char prog[80]; //存放所有输入字符
char token[8]; //存放词组
char ch; //单个字符
int syn,p,m,n;//syn:种别编码
double sum;
int count;
int Signal; //是否带正负号(0不带,1负号)
int xiaoshu; //是否是小数
double isxiaoshu; //小数
int zhishu; //是否是指数
int index; //指数幂
int fushu; //是否带负号
double temp;
int temp2;
void scanner();
char *rwtab[6]={“function”,”if”,”then”,”while”,”do”,”endfunc”};
int main()
{
p=0;
count=0;
xiaoshu=0;
index=0;
int b,a;
FILE *fp;
printf(“以文件输入请按1!\n”);
printf(“以键盘输入请按2!\n”);
scanf(“%d”,&a);
if(a==1)
{
if((fp=fopen(“D:\111.txt”,”r”))==NULL)
{
printf(“Failure to open 111.txt!\n”);
exit(0);
}
fscanf(fp,”%s”,prog);
fclose(fp);
}
else if(a==2)
{
printf(“\n Please input string:\n”);
do{
ch=getchar();
prog[p++]=ch;
}while(ch!=””#””);
}
p=0;
do{
scanner();
switch(syn)
{
printf(“以文件输出请按1!\n”);
printf(“以屏幕输出请按2!\n”);
scanf(“%d”,&b);
if(b==1)
{
case 11: if(xiaoshu==0)
{
fprintf(fp,”%2d,%8d”,syn,(int)sum);
//printf(“(%2d,%8d)\n”,syn,(int)sum);
break;
}
else if(zhishu==1)
{
fprintf(fp,”(%2d,%10.5e)\n”,syn,sum);
//printf(“(%2d,%10.5e)\n”,syn,sum);
zhishu=0;
xiaoshu=0;
break;
}
else if(xiaoshu==1)
{
fprintf(fp,”(%2d,%8.4f)\n”,syn,sum);
//printf(“(%2d,%8.4f)\n”,syn,sum);
xiaoshu=0;
break;
}
case -1:
fprintf(fp,”input error\n”);
printf(“input error\n”);
break;
default:
fprintf(fp,”(%2d,%8s)\n”,syn,token);
//printf(“(%2d,%8s)\n”,syn,token);
}
}while(syn!=0);
return 0;
}
else if(b==2)
{
case 11: if(xiaoshu==0)
{
printf(“(%2d,%8d)\n”,syn,(int)sum);
break;
}
else if(zhishu==1)
{
printf(“(%2d,%10.4e)\n”,syn,sum);
zhishu=0;
xiaoshu=0;
break;
}
else if(xiaoshu==1)
{
printf(“(%2d,%8.4f)\n”,syn,sum);
xiaoshu=0;
break;
}
case -1:
printf(“input error\n”);
break;
default:
printf(“(%2d,%8s)\n”,syn,token);
}
while(syn!=0);
return 0;
}
void scanner()
{
sum=0;
isxiaoshu=0;
m=0;
for(n=0;n<8;n++)
token[n]=NULL;
ch=prog[p++];
while(ch==”” “”||ch==””\n””||ch==””\t””)
ch=prog[p++];
if(((ch>=””a””)&&(ch<=””z””))||((ch>=””A””)&&(ch<=””Z””)))
{
while(((ch>=””a””)&&(ch<=””z””))||((ch>=””A””)&&(ch<=””Z””))||((ch>=””0″”)&&(ch<=””9″”)))
{
token[m++]=ch;
ch=prog[p++];
}
token[m++]=””\0″”;
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″”))
{
Num:
if(Signal==1)
{
token[m++]=””-“”;
}
while((ch>=””0″”)&&(ch<=””9″”))
{
sum=sum*10+ch-“”0″”;
ch=prog[p++];
}
if(ch==””.””)
{
xiaoshu=1;
ch=prog[p++];
while((ch>=””0″”)&&(ch<=””9″”))
{ //pow(x,y)计算x的y次幂
temp=(ch-“”0″”)*pow(0.1,++count);
isxiaoshu=isxiaoshu+temp;
ch=prog[p++];
}
sum=sum+isxiaoshu;
}
if(ch==””e””||ch==””E””)
{
zhishu=1;
ch=prog[p++];
if(ch==””-“”)
{
fushu=1;
ch=prog[p++];
}
while((ch>=””0″”)&&(ch<=””9″”))
{ //指数
index=index*10+ch-“”0″”;
ch=prog[p++];
}
if(fushu)
sum=sum*pow(0.1,index);
else
sum=sum*pow(10,index);
}
if(Signal==1)
{
sum=-sum;
Signal=0;
}
p–;
syn=11;
}
else
switch(ch)
{
case “”<“”:
m=0;
token[m++]=ch;
ch=prog[p++];
if(ch==””=””)
{
syn=21;
token[m++]=ch;
}
else if(ch==””=””)
{
syn=21;
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=25;
token[m++]=ch;
}
else
{
syn=18;
p–;
}
break;
case “”+””:
temp2=prog[p];
if((temp2>=””0″”)&&(temp2<=””9″”))
{
Signal=2;
ch=prog[p++];
goto Num;
}
syn=13;
token[m++]=ch;
break;
case “”-“”:
temp2=prog[p];
if((temp2>=””0″”)&&(temp2<=””9″”))
{
Signal=1;
ch=prog[p++];
goto Num;
}
syn=14;
token[m++]=ch;
break;
case “”*””:
syn=15;
token[m++]=ch;
break;
case “”/””:
syn=16;
token[m++]=ch;
break;
case “”;””:
syn=26;
token[m++]=ch;
break;
case “”(“”:
syn=27;
token[m++]=ch;
break;
case “”)””:
syn=28;
token[m++]=ch;
break;
case””#””:
syn=0;
token[m++]=ch;
break;
default:
syn=-1;
}
}