4

假设我有这个 C 代码,它在 C 中有两个不同的声明。

struct pcdata
{
    int x; int y;
};

int yyparse (struct pcdata *pp);
int yyparse (void *pp);

int yyparse (struct pcdata *pp)
{

}

用 cc/gcc 编译代码,我有conflicting types错误。

test> cc -c hello.c
hello.c:7:5: error: conflicting types for 'yyparse'
int yyparse (void *pp);
    ^
hello.c:6:5: note: previous declaration is here
int yyparse (struct pcdata *pp);
^
1 error generated.
test> gcc -c hello.c
hello.c:7: error: conflicting types for ‘yyparse’
hello.c:6: error: previous declaration of ‘yyparse’ was here

g++ 没有错误,因为 c++ 允许函数重载。

test> g++ -c hello.c

是否有任何 gcc 选项允许函数重载?我有这个(简化的)代码和 Bison 2.7 生成的 tab.c 代码。

4

2 回答 2

3

C doesn't have function overloading.

于 2013-07-08T00:48:51.030 回答
3

这肯定不会在 C 中工作,但它不应该是必要的。

bison 通常会创建一个yyparse根本不接受参数的 a,但是您可以使用%parse-param声明(至少在不太旧的 bison 版本中)告诉 bison 您希望它接受哪些参数。

在所有情况下,只有一个yyparse,除非您将两个单独的解析器编译到同一个程序中,当您有多个需要解析的语法时,当然会发生这种情况。在这种情况下,您可以使用%name-prefix告诉 bison 一个或两个由 bison 生成的解析器应该使用除yy.

于 2013-07-08T04:03:32.733 回答