求大神帮助词法分析,当输入第一个1.2e12时可以输出,当时输入第二个1.2e12时就不能输出了,万分感谢,还有不能识别x=7*8+9中的+9,

C语言 码拜 10年前 (2015-05-11) 1161次浏览 0个评论
 

#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;
}
}

源码错误好多,先改改错误
就是那个错误不会改了,求大神帮忙啊谢谢!
先把它的状态机写出来
已经写出来了,就不懂为什么代码就是实现不了,只能第一次读取字符,而且代码中有一个错误,我差了好多资料都没改对,你能给我讲讲吗??
算法思路是什么?
状态机不对

代码未严格按照正确的状态机编写。
那你能给我讲讲吗??谢谢了,跪求!
10分
建议参考TCC源代码。
http://cn.bing.com/images/search?q=C%E8%AF%AD%E8%A8%80+%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90+%E7%8A%B6%E6%80%81%E8%BD%AC%E6%8D%A2%E5%9B%BE&go=%E6%8F%90%E4%BA%A4&qs=n&form=QBILPG&pq=c%E8%AF%AD%E8%A8%80+%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90+%E7%8A%B6%E6%80%81%E8%BD%AC%E6%8D%A2%E5%9B%BE&sc=0-0&sp=-1&sk=
30分
词法分析(1)—词法分析的有关概念以及转换图:
http://www.cppblog.com/woaidongmao/archive/2009/09/25/97245.aspx

喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!