Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

c - Include struct in the %union def with Bison/Yacc

I am trying to include a struct as part of the union with Bison, but I get an error on the 'struct node args' in %union:

parser.y:17: error: field ‘args’ has incomplete type

The Code:

struct node {
    char * val;
    struct node * next;
};

%}

%union {
    char * string;
    struct node args;
}

%token <string> CD WORD PWD EXIT

%type <args> arg_list

Anyone know what I am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Even better, use the %code directive with the "requires" option, i.e.:

%code requires {
    struct node {
        char * val;
        struct node * next;
    };
}

%union {
    char * string;
    struct node args;
}

This will include the code in the "requires" block in the tab.h file as well as the parser source file.

From the documentation: http://www.gnu.org/software/bison/manual/html_node/Decl-Summary.html#Decl-Summary

  • requires
    • Purpose: This is the best place to write dependency code required for YYSTYPE and YYLTYPE. In other words, it's the best place to define types referenced in %union directives, and it's the best place to override Bison's default YYSTYPE and YYLTYPE definitions.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...