Discussion:
Yacc/Bison: Trouble using struct in %union - "incomplete type" error
(too old to reply)
John Sasso
2006-07-15 17:24:48 UTC
Permalink
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
Jack Klein
2006-07-15 21:27:03 UTC
Permalink
Post by John Sasso
%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
Rod Pemberton
2006-07-16 00:38:24 UTC
Permalink
Post by John Sasso
%union {
int value;
struct Symbol Sym;
}
The Symbol struct I defined in a header file I #included in the Prologue
struct Symbol {
char *SymName; /* Name of symbol/token */
int SymType; /* Symbol type */
int SymValue;
};
bison -d prog.y
flex -t prog.l > prog.lex.c
gcc -g -w -c prog.lex.c
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
SM Ryan
2006-07-16 01:52:45 UTC
Permalink
John Sasso <***@nospam.com> 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.

Loading...