永发信息网

谁帮我调式下这个C语言程序

答案:1  悬赏:60  手机版
解决时间 2021-04-13 08:41

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

FILE *fp;
char cbuffer;
char *key[8]={”if”,”else”,”for”,”while”,”do”,”return”,”break”,”continue”};
char *border[6]={”,”,”;”,”{”,”}”,”(”,”)”};
char *arithmetic[4]={”+”,”-”,”*”,”/”};
char *relation[6]={”<","<=","=",">“,”>=”,”<>“};
char *consts[20];
char *label[20];
int constnum=0,labelnum=0;

int search(char * searchchar,int wordtype)
{
int i=0;
switch (wordtype) {
case 1:
for (i=0;i<=7;i++)
{
if (strcmp(key[i],searchchar)==0)
return(i+1);
}
case 2:
for (i=0;i<=5;i++)
{
if (strcmp(border[i],searchchar)==0)
return(i+1);
} return(0);
case 3:
for (i=0;i<=3;i++)
{
if (strcmp(arithmetic[i],searchchar)==0)
{
return(i+1);
}
return(0);
}
case 4:
for (i=0;i<=5;i++)
{
if (strcmp(relation[i],searchchar)==0)
{
return(i+1);
}
}
return(0);
case 5:
for (i=0;i<=constnum;i++)
{
// if (strcmp(consts[i],searchchar)==0)
{
return(i+1);
}
}
consts[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(consts[i-1],searchchar);
constnum++;
return(i);
case 6:
for (i=0;i<=labelnum;i++)
{
// if (strcmp(label[i],searchchar)==0)
{
return(i+1);
}
}
label[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(label[i-1],searchchar);
labelnum++;
return(i);
}//switch结束
}

char alphaprocess(char buffer)
{
int atype;
int i=-1;
char alphatp[20];
while ((isalpha(buffer))||(isdigit(buffer)))
{
alphatp[++i]=buffer;
buffer=fgetc(fp);
}
alphatp[i+1]='\0';
if (atype=search(alphatp,1))
printf("%s (1,%d)\n",alphatp,atype-1);
else
{
atype=search(alphatp,6);
printf("%s (6,%d)\n",alphatp,atype-1);
}
return(buffer);
}

char digitprocess(char buffer)
{
int i=-1;
char digittp[20];
int dtype;
while ((isdigit(buffer)))
{
digittp[++i]=buffer;
buffer=fgetc(fp);
}
digittp[i+1]='\0';
dtype=search(digittp,5);
printf("%s (5,%d)\n",digittp,dtype-1);
return(buffer);
}

char otherprocess(char buffer)
{
int i=-1;
char othertp[20];
int otype,otypetp;
othertp[0]=buffer;
othertp[1]='\0';
if (otype=search(othertp,3))
{
printf("%s (3,%d)\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
}

if (otype=search(othertp,4))
{
buffer=fgetc(fp);
othertp[1]=buffer;
othertp[2]='\0';
if (otypetp=search(othertp,4))
{
printf("%s (4,%d)\n",othertp,otypetp-1);
buffer=fgetc(fp);
goto out;
}
else
{
othertp[1]='\0';
printf("%s (4,%d)\n",othertp,otype-1);
}
goto out;
}

if (buffer==':')
{
buffer=fgetc(fp);
if (buffer=='=')
printf(":= (2,2)\n");
buffer=fgetc(fp);
goto out;
}
else
{
if (otype=search(othertp,2))
{
printf("%s (2,%d)\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
}
}

if ((buffer=='\n')||(buffer==' '))
buffer=fgetc(fp);
else
printf("%c error,not a word\n",buffer);

buffer=fgetc(fp);
out: return(buffer);
}

void main()
{
int i;
for (i=0;i<=20;i++)
{
label[i]=NULL;
consts[i]=NULL;
};

{
if((fp=fopen("example.c","r"))==NULL)
printf("error\n");
else
cbuffer = fgetc(fp);
while (cbuffer!=EOF)
{
if (isalpha(cbuffer))
cbuffer=alphaprocess(cbuffer);
else if (isdigit(cbuffer))
cbuffer=digitprocess(cbuffer);
else
cbuffer=otherprocess(cbuffer);
}
printf("over\n");
getchar();
}
}

这是一个简陋的词法分析器,我直接敲得老师给的参考代码,但有问题啊

最佳答案

char *key[8]={”if”,”else”,”for”,”while”,”do”,”return”,”break”,”continue”};
char *border[6]={”,”,”;”,”{”,”}”,”(”,”)”};
char *arithmetic[4]={”+”,”-”,”*”,”/”};
char *relation[6]={”<","<=","=",">“,”>=”,”<>“};


这么长的代码没仔细看,但是这几行太扎眼了呀。


你难道连中文的双引号”和英文的双引号"都分不清吗?

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
QQ三国59级号怎么升级快?
原湘情境文化主题餐厅(河西总店)怎么去啊,有
“琪”和“琦”哪个更适合女生的名字中用?两
如何在手机上看QQ群空间。
毛泽东《沁园春?雪》“望长城内外,惟余莽莽
我想问一下一个MV的女歌星是外国人来的.黑头
先存在科学还是先存在人?
海贼王古代版是多少集?
java 嵌套for循环 输出下列数字金字塔1 121 1
为什么力法刷冰龙画面会卡住不动
厨房冰箱管理制度标语,爱好冰箱标语
唐高宗李治娶了多少他爹的老婆?
求小强清凉一夏的注册格式,我怎么也注册不了
夫妻分居会有什么结果?
慈悲的近义词
推荐资讯
?来帮忙啦!?
单选题有人说,价格听证会往往就是涨价听证会
人生有的时候为什么会感到失落?
旅行期间英语怎么说,在旅行期间的英文怎么说
单选题Whentheredlightis________,youmust
法人相互持股的目的是为什么?
全新捷达怎么样?家用,看到网上很多人都说风
艺术作品展 该如何欣赏
广西3的炎麻护肩能卖多少现在?
今年河池的天气会不会比去年冷?
广昌停车区这个地址在什么地方,我要处理点事
口字里有个对号是怎么整的啊
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?