2012-04-15 17 views
0

これらの要素のメモリ位置が必要なので、私はバイソンの構造体の構造体の構造体へのポインタを使用するのにいくつかの問題がありますが、すべてseem to point to the same union positionです。わからない場合は、私は正しい方法を使用しています。私のコードは次のようになります。Cおよびbison:%UNION定義の構造体へのポインタ

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 ... 
    ... 
} 

まあ、これだけで動作します。

関連する問題