converse 发表于 2008-03-30 23:28

我发现lex&&yacc上的代码在flex下编译出错

第二章的ch2-05.l:


%{
#undef input
#undef unput
int input(void);
void unput(int ch);
unsigned verbose;
char *progName;
%}

%%

-h        |
"-?"        |
-help        { printf("usage is: %s [-help | -h | -? ] [-verbose | -v]"
       " [(-file| -f) filename]\n", progName);
        }
-v        |
-verbose { printf("verbose mode is on\n"); verbose = 1; }

%%
char **targv;        /* remembers arguments */
char **arglim;        /* end of arguments */

main(int argc, char **argv)
{
        progName = *argv;
        targv = argv+1;
        arglim = argv+argc;
        yylex();
}

static unsigned offset = 0;

int
input(void)
{
        char c;

        if (targv >= arglim)
                return(0);        /* EOF */
        /* end of argument, move to the next */
        if ((c = targv) != '\0')
                return(c);
        targv++;
        offset = 0;
        return(' ');
}

/* simple unput only backs up, doesn't allow you to */
/* put back different text */
void
unput(int ch)
{
        /* AT&T lex sometimes puts back the EOF ! */
        if(ch == 0)
                return;        /* ignore, can't put back EOF */
        if (offset) {        /* back up in current arg */
                offset--;
                return;
        }

        targv--;        /* back to previous arg */
        offset = strlen(*targv);
}



$ flex ch2-05.l

Lichuang@LICHUANG /e/source/lex&&yacc
$ gcc lex.yy.c-lfl
lex.yy.c:514: warning: static declaration of 'input' follows non-static declaration
ch2-05.l:4: warning: previous declaration of 'input' was here
ch2-05.l:37: error: redefinition of 'input'
lex.yy.c:1096: error: previous definition of 'input' was here


环境是MSYS下的flex.
难道是lex和flex的有兼容问题?
:outu: :outu:

cjaizss 发表于 2008-03-31 13:24

用lex ch2-05.l
或者flex -l ch2-05.l
试下

converse 发表于 2008-03-31 17:10

回复 #2 cjaizss 的帖子

还是不行:

Lichuang@LICHUANG /e/source/lex&&yacc
$ flex -l ch2-05.l

Lichuang@LICHUANG /e/source/lex&&yacc
$ gcc lex.yy.c-lfl
lex.yy.c:559: warning: static declaration of 'input' follows non-static declaration
ch2-05.l:4: warning: previous declaration of 'input' was here
ch2-05.l:37: error: redefinition of 'input'
lex.yy.c:1128: error: previous definition of 'input' was here

Lichuang@LICHUANG /e/source/lex&&yacc
$

cjaizss 发表于 2008-04-01 17:29

最前面加两句

%{
#define YY_NO_INPUT
#define YY_NO_UNPUT
#undef input
#undef unput
int input(void);
void unput(int ch);
unsigned verbose;
char *progName;

%}

converse 发表于 2008-04-02 22:32

回复 #4 cjaizss 的帖子

感谢,这次可以编译过去了,我找找lex的说明,回头在这里给一个解释.

Lion King 发表于 2008-06-25 21:13

回复 #4 cjaizss 的帖子

版主怎么想到这种解决方案?因为
#ifndef YY_NO_INPUT
#ifdef __cplusplus
static int yyinput YY_PROTO(( void ));
#else
static int input YY_PROTO(( void ));
#endif
#endif
吗?添加之后是可以正常编译,但为什么可以这样处理?难道lex生成的c中这些代码是冗余的?

cjaizss 发表于 2008-06-25 21:44

回复 #6 Lion King 的帖子

呵呵,确实是因为这段代码

Lion King 发表于 2008-06-25 23:41

这么快就回复了, 不过你没完全回答我的问题啊-_-#
btw, 你的头像是名子组成的?!

cjaizss 发表于 2008-06-26 00:53

呵呵,你的观察力让我佩服.
那里有一堆宏,让你可以定制,不能说冗余的:)

Lion King 发表于 2008-06-26 01:07

^^过奖~
多谢指教
页: [1]
查看完整版本: 我发现lex&&yacc上的代码在flex下编译出错