468,513 Members | 1,873 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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

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
Jul 15 '06 #1
3 10684
On Sat, 15 Jul 2006 17:24:48 GMT, John Sasso <js******@nospam.com>
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/~a...FAQ-acllc.html
Jul 15 '06 #2

"John Sasso" <js******@nospam.comwrote in message
news:Ax*******************@twister.nyroc.rr.com...
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
Jul 16 '06 #3
John Sasso <js******@nospam.comwrote:
# 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.
Jul 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Lou Pecora | last post: by
3 posts views Thread by Alden Pierre | last post: by
669 posts views Thread by Xah Lee | last post: by
13 posts views Thread by imutate | last post: by
2 posts views Thread by unclefester | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.