0
我在使用指向结构的指针指向Bison的union定义时遇到了一些麻烦,因为我需要这些元素的内存位置,但它们都是seem to point to the same union position。不知道如果我使用正确的方式。我的代码如下所示:C和野牛:指向%联合定义中的结构的指针
main.h:
typedef struct _control *control;
struct _control { ... };
typedef struct _symbol *symbol;
struct _symbol { ... };
...
#include "parser.h"
parser.y
%{
#include "main.h"
%}
%union {
control ctrl;
symbol s_head;
symbol s_tail;
}
...
%%
...
%%
int main (int argc, char** argv) {
...
yylval.ctrl = malloc(sizeof(struct _control));
yylval.s_head = malloc(sizeof(struct _symbol));
yylval.s_tail = malloc(sizeof(struct _symbol));
// This will give me the same memory position
printf("%ld %ld %ld %ld\n",
yylval, yylval.ctrl,
yylval.s_head, yylval.s_tail);
...
}
在野牛(和它的前身Yacc一样),'%union'声明了一个C'union',你应该更多地阅读这些。 – 2012-04-15 15:12:36