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

J

John Sasso

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
 
J

Jack Klein

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

Rod Pemberton

John Sasso said:
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
 
S

SM Ryan

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top