Yacc/Bison: Trouble using struct in %union - "incomplete type" error

Discussion in 'C Programming' started by John Sasso, Jul 15, 2006.

  1. John Sasso

    John Sasso Guest

    In my Yacc .y file I defined:

    %union {
    int value;
    struct Symbol Sym;
    }


    The Symbol struct I defined in a header file I #included in the Prologue
    section of the .y file as:

    struct Symbol {
    char *SymName; /* Name of symbol/token */
    int SymType; /* Symbol type */
    int SymValue;
    };

    However, on build I get:

    bison -d prog.y
    flex -t prog.l > prog.lex.c
    gcc -g -w -c prog.lex.c
    In file included from prog.l:10:
    prog.y:22: error: field `Sym' has incomplete type
    make: *** [prog.lex.o] Error 1

    If in the %union I make Sym a pointer to struct Symbol the build error
    goes away. Can someone explain what is going wrong here and how I can
    correct this to Sym is still just a struct?

    --john
     
    John Sasso, Jul 15, 2006
    #1
    1. Advertising

  2. John Sasso

    Jack Klein Guest

    On Sat, 15 Jul 2006 17:24:48 GMT, John Sasso <>
    wrote in comp.lang.c:

    > In my Yacc .y file I defined:
    >
    > %union {
    > int value;
    > struct Symbol Sym;
    > }


    [snip]

    This is not C, and not a C language issue, therefore off-topic in
    comp.lang.c, followups set. I have no idea whether it is topical or
    not in comp.os.linux.development.apps.

    comp.lang.c removed from follow up.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 15, 2006
    #2
    1. Advertising

  3. "John Sasso" <> wrote in message
    news:Ax9ug.24038$...
    > In my Yacc .y file I defined:
    >
    > %union {
    > int value;
    > struct Symbol Sym;
    > }
    >
    >
    > The Symbol struct I defined in a header file I #included in the Prologue
    > section of the .y file as:
    >
    > struct Symbol {
    > char *SymName; /* Name of symbol/token */
    > int SymType; /* Symbol type */
    > int SymValue;
    > };
    >
    > However, on build I get:
    >
    > bison -d prog.y
    > flex -t prog.l > prog.lex.c
    > gcc -g -w -c prog.lex.c
    > In file included from prog.l:10:
    > prog.y:22: error: field `Sym' has incomplete type
    > make: *** [prog.lex.o] Error 1
    >
    > If in the %union I make Sym a pointer to struct Symbol the build error
    > goes away. Can someone explain what is going wrong here and how I can
    > correct this to Sym is still just a struct?
    >


    I can't help very much from what you posted. All I can do is review how you
    should include the file.

    Let's say your struct:

    struct Symbol {
    char *SymName; /* Name of symbol/token */
    int SymType; /* Symbol type */
    int SymValue;
    };

    is in "symbol.h".

    Then, the .y Bison grammar file, should have this:

    %{
    #include "symbol.h"
    %}
    %union {
    int value;
    struct Symbol Sym;
    }

    And, the .l Flex grammar file, should have this:

    %{
    #include "symbol.h"
    #include "y.tab.h"
    %}


    Notice that "y.tab.h" is included in the .l Flex grammar and it comes after
    "symbol.h". That's about all I can help you with.


    Rod Pemberton
     
    Rod Pemberton, Jul 16, 2006
    #3
  4. John Sasso

    SM Ryan Guest

    John Sasso <> wrote:
    # In my Yacc .y file I defined:
    #
    # %union {
    # int value;
    # struct Symbol Sym;
    # }
    #
    #
    # The Symbol struct I defined in a header file I #included in the Prologue
    # section of the .y file as:
    #
    # struct Symbol {
    # char *SymName; /* Name of symbol/token */
    # int SymType; /* Symbol type */
    # int SymValue;
    # };
    #
    # However, on build I get:
    #
    # bison -d prog.y
    # flex -t prog.l > prog.lex.c
    # gcc -g -w -c prog.lex.c
    # In file included from prog.l:10:
    # prog.y:22: error: field `Sym' has incomplete type
    # make: *** [prog.lex.o] Error 1

    Are you also #including the header file in prog.l? The C files generated
    by bison and lex are separate and should not be expected to known about
    the other's includes. While bison might write out an interface file with
    its unions and symbol definitions, you should only expect that to have
    the declarations bison creates and not transitively declarations from
    other include files.

    # If in the %union I make Sym a pointer to struct Symbol the build error
    # goes away. Can someone explain what is going wrong here and how I can
    # correct this to Sym is still just a struct?

    All struct pointers look the same; the compile can allocate the union
    because it will know the size of the struct pointer even if it doesn't
    know the size of the struct.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    You hate people.
    But I love gatherings. Isn't it ironic.
     
    SM Ryan, Jul 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    672
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    880
    Chris Torek
    Apr 26, 2004
  3. gk245
    Replies:
    2
    Views:
    1,211
    Christopher Benson-Manica
    May 6, 2006
  4. Mohitz

    yacc trouble

    Mohitz, Jun 4, 2007, in forum: C Programming
    Replies:
    2
    Views:
    356
    Mohitz
    Jun 4, 2007
  5. thomas

    yacc/bison help

    thomas, Feb 19, 2008, in forum: C Programming
    Replies:
    7
    Views:
    366
    SM Ryan
    Feb 21, 2008
Loading...

Share This Page