2012-04-15 1 views
0

나는이 요소의 메모리 위치가 필요하지만 모두 seem to point to the same union position이므로 bison의 공용체 정의에서 구조체에 대한 포인터를 사용하는 데 문제가 있습니다. 확실하지 않은 경우 올바른 방법을 사용하고 있습니다. 내 코드 같이 보인다 :C 및 bison : % 유니온 정의의 struct에 대한 포인터

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); 
    ... 
} 
+0

Bison에서 (이전 Yacc에서와 같이)'% union'은 C'' union을 선언합니다. –

답변

0

확실하지 않음 나는 그것들을 구조체로 변환했다.

main.h

typedef struct _control *control; 
struct _control { ... }; 

typedef struct _symbol *symbol; 
struct _symbol { ... }; 

typedef struct _global *global; 
struct _global { ... }; 

parser.y

%{ 
    #include "main.h" 
%} 

%union { 
    global g; 
} 
... 
%% 
... 
%% 
int main (int argc, char** argv) { 
    ... 
    yylval.g->ctrl->some_element ... 
    yylval.g->s_head ... 
    ... 
} 

음, 이것은 단지 작동합니다.