| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 546 人关注过本帖
标题:麻烦大家帮忙看看!一直找不到错误
只看楼主 加入收藏
san3
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-12
结帖率:100%
收藏
已结贴  问题点数:3 回复次数:6 
麻烦大家帮忙看看!一直找不到错误
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXTOKEN  100
#define BUFSIZE  100

void dcl(void);
void dirdcl(void);
int gettoken(void);
int getch(void);
void ungetch(int);     

enum{NAME,PARENS,BRACKETS};
int tokentype;          /*最后一个记号的类型*/
char token[MAXTOKEN];   /*最后一个记号字符串*/
char name[MAXTOKEN];    /*标识符名*/
char datatype[MAXTOKEN];  /*数据类型为char、int等*/
char out[1000];                 /*输出串*/
char buf[BUFSIZE]; /*用于ungetch函数的缓冲区*/
int bufp=0;/*buf中下一个空闲位置*/

int getch(void)
{
    return (bufp>0)? buf[--bufp] :getchar();
}

void ungetch(int c)
{
    if(bufp>=BUFSIZE)
         printf("ungetch:too many characters\n");
    else
         buf[bufp++]=c;
}

/*dcl函数:对一个声明符进行语法分析*/
void dcl(void)
{
    int ns;
    for(ns=0;gettoken()=='*';)      /*统计字符*的个数*/
        ns++;
    dirdcl();
    while(ns-->0)
        strcat(out,"pointer to");
}

/*dirdcl函数:分析一个直接声明*/
void dirdcl(void)
{
    int type;
    if (tokentype=='(') /*形式为(dcl)*/
    {      
        dcl();
        if(tokentype!=')')
            printf("error:missing )\n");
    }
    else if(tokentype==NAME)    /*变量名*/
        strcpy(name,token);
    else
        printf("error:expected name or(dcl)\n");
    while((type=gettoken())==PARENS||type==BRACKETS)
        if(type==PARENS)
            strcat(out,"function returning");
        else
        {
            strcat(out," array");
            strcat(out,token);
            strcat(out," of");
        }
}

int gettoken(void)
{
    int c;
    char *p=token;

    while((c=getch())==' '||c=='\t')
        ;
    if (c=='(')
    {   
        if ((c=getch())==')')
        {
            strcpy(token,"()");
            return tokentype=PARENS;
        }
        else
        {
            ungetch(c);
            return tokentype='(';
        }
    }
    else if(c=='[')
    {
        for(*p++=c;(*p++=getch())!=']';)
            ;
        *p='\0';
        return tokentype=BRACKETS;
    }
    else if(isalpha(c))
    {
        for(*p++=c;isalnum(c=getch());)
            *p++=c;
        *p='\0';
        ungetch(c);
        return tokentype=NAME;
    }
    else
        return tokentype=c;
}
void main()      #include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXTOKEN  100
#define BUFSIZE  100

void dcl(void);
void dirdcl(void);
int gettoken(void);
int getch(void);
void ungetch(int);     

enum{NAME,PARENS,BRACKETS};
int tokentype;          /*最后一个记号的类型*/
char token[MAXTOKEN];   /*最后一个记号字符串*/
char name[MAXTOKEN];    /*标识符名*/
char datatype[MAXTOKEN];  /*数据类型为char、int等*/
char out[1000];                 /*输出串*/
char buf[BUFSIZE]; /*用于ungetch函数的缓冲区*/
int bufp=0;/*buf中下一个空闲位置*/

int getch(void)
{
    return (bufp>0)? buf[--bufp] :getchar();
}

void ungetch(int c)
{
    if(bufp>=BUFSIZE)
         printf("ungetch:too many characters\n");
    else
         buf[bufp++]=c;
}

void dcl(void)
{
    int ns;
    for(ns=0;gettoken()=='*';)      
        ns++;
    dirdcl();
    while(ns-->0)
        strcat(out,"pointer to");
}

void dirdcl(void)
{
    int type;
    if (tokentype=='(')
    {      
        dcl();
        if(tokentype!=')')
            printf("error:missing )\n");
    }
    else if(tokentype==NAME)   
        strcpy(name,token);
    else
        printf("error:expected name or(dcl)\n");
    while((type=gettoken())==PARENS||type==BRACKETS)
        if(type==PARENS)
            strcat(out,"function returning");
        else
        {
            strcat(out," array");
            strcat(out,token);
            strcat(out," of");
        }
}

int gettoken(void)
{
    int c;
    char *p=token;

    while((c=getch())==' '||c=='\t')
        ;
    if (c=='(')
    {   
        if ((c=getch())==')')
        {
            strcpy(token,"()");
            return tokentype=PARENS;
        }
        else
        {
            ungetch(c);
            return tokentype='(';
        }
    }
    else if(c=='[')
    {
        for(*p++=c;(*p++=getch())!=']';)
            ;
        *p='\0';
        return tokentype=BRACKETS;
    }
    else if(isalpha(c))
    {
        for(*p++=c;isalnum(c=getch());)
            *p++=c;
        *p='\0';
        ungetch(c);
        return tokentype=NAME;
    }
    else
        return tokentype=c;
}
void main()      /*将声明转换为文字描述*/
{
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}
{
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}

error C2601: 'dirdcl' : local function definitions are illegal
error C2601: 'gettoken' : local function definitions are illegal
error C2601: 'main' : local function definitions are illegal
fatal error C1004: unexpected end of file found
搜索更多相关主题的帖子: void 标识 include 缓冲区 字符串 
2012-03-12 21:41
san3
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-12
收藏
得分:0 
上面的发乱了,不好意思
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAXTOKEN  100
#define BUFSIZE  100

void dcl(void);
void dirdcl(void);
int gettoken(void);
int getch(void);
void ungetch(int);     

enum{NAME,PARENS,BRACKETS};
int tokentype;          /*最后一个记号的类型*/
char token[MAXTOKEN];   /*最后一个记号字符串*/
char name[MAXTOKEN];    /*标识符名*/
char datatype[MAXTOKEN];  /*数据类型为char、int等*/
char out[1000];                 /*输出串*/
char buf[BUFSIZE]; /*用于ungetch函数的缓冲区*/
int bufp=0;/*buf中下一个空闲位置*/

int getch(void)
{
    return (bufp>0)? buf[--bufp] :getchar();
}

void ungetch(int c)
{
    if(bufp>=BUFSIZE)
         printf("ungetch:too many characters\n");
    else
         buf[bufp++]=c;
}

void dcl(void)
{
    int ns;
    for(ns=0;gettoken()=='*';)      
        ns++;
    dirdcl();
    while(ns-->0)
        strcat(out,"pointer to");
}

void dirdcl(void)
{
    int type;
    if (tokentype=='(')
    {      
        dcl();
        if(tokentype!=')')
            printf("error:missing )\n");
    }
    else if(tokentype==NAME)   
        strcpy(name,token);
    else
        printf("error:expected name or(dcl)\n");
    while((type=gettoken())==PARENS||type==BRACKETS)
        if(type==PARENS)
            strcat(out,"function returning");
        else
        {
            strcat(out," array");
            strcat(out,token);
            strcat(out," of");
        }
}

int gettoken(void)
{
    int c;
    char *p=token;

    while((c=getch())==' '||c=='\t')
        ;
    if (c=='(')
    {   
        if ((c=getch())==')')
        {
            strcpy(token,"()");
            return tokentype=PARENS;
        }
        else
        {
            ungetch(c);
            return tokentype='(';
        }
    }
    else if(c=='[')
    {
        for(*p++=c;(*p++=getch())!=']';)
            ;
        *p='\0';
        return tokentype=BRACKETS;
    }
    else if(isalpha(c))
    {
        for(*p++=c;isalnum(c=getch());)
            *p++=c;
        *p='\0';
        ungetch(c);
        return tokentype=NAME;
    }
    else
        return tokentype=c;
}
void main()      /*将声明转换为文字描述*/
{
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}
{
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}

error C2601: 'dirdcl' : local function definitions are illegal
error C2601: 'gettoken' : local function definitions are illegal
error C2601: 'main' : local function definitions are illegal
fatal error C1004: unexpected end of file found
2012-03-13 14:27
san3
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-12
收藏
得分:0 
麻烦大家抽空看看吧,谢谢!
2012-03-14 15:55
lwei
Rank: 5Rank: 5
等 级:职业侠客
威 望:3
帖 子:197
专家分:369
注 册:2005-5-4
收藏
得分:2 
void main()      /*将声明转换为文字描述*/
{
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}//
{//  没有看懂这个是什麽东东,删除后gcc编译通过。
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}

2012-03-14 21:36
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
收藏
得分:1 

编程之路定要走完……
2012-03-14 21:39
san3
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-12
收藏
得分:0 
回复 4楼 lwei
我是用VC6.0编译的,会不会是编译器的问题呢?
2012-03-15 14:18
san3
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-3-12
收藏
得分:0 
void main()      /*将声明转换为文字描述*/
{
    while (gettoken()!=EOF)
    {   
        strcpy(datatype,token);
        out[0]='\0';
        dcl();         
        if(tokentype!='\n')
            printf("syntax error\n");
        printf("%s:%s %s\n",name,out,datatype);
    }
}
上面发的主函数重复了一部分,应该是这样的
2012-03-15 14:23
快速回复:麻烦大家帮忙看看!一直找不到错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015507 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved