9

yacc doesn't seem to like when my tokens are of a type that I defined.

At the top of my grammar (.y) file in a %{ ... %} block, I include a header file that defines the following structure:

typedef struct _spim_register {
    spim_register_type type; /* This is a simple enumeration, already defined */
    int number;              
} spim_register;

Before my list of rules, I have:

%token AREG
...
%union {
struct _spim_register reg;
}
...
%type <reg> register AREG

I get

error: field ‘reg’ has incomplete type

at the line in the %union clause while trying to compile the code produced by bison. In my %union statement, trying to declare reg by writing spim_register reg; gives the error:

unknown type name ‘spim_register’

It seems like there's something special about %union { ... }, because I'm able to use the data structures from my header file in the actions for the rules.

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
ArIck
  • 418
  • 3
  • 10
  • 3
    [Possibly related](http://stackoverflow.com/questions/1430390/include-struct-in-the-union-def-with-bison-yacc) – user786653 Oct 03 '11 at 20:18

2 Answers2

11

It would help if my #includes were in the right order...

The answer was, as user786653 hinted, here. I needed to include the header file that defines my custom structure before including the .tab.h file in the .l file.

Community
  • 1
  • 1
ArIck
  • 418
  • 3
  • 10
8

I met the same problem. Because my *.l file was like this:

#include "y.tab.h"
#include "FP.h"

I rewrote it like this:

#include "FP.h"
#include "y.tab.h"

It works.

user16217248
  • 3,119
  • 19
  • 19
  • 37
flyrain
  • 573
  • 6
  • 11
  • Working in xcode I found I needed to delete the Derived Data for my project in order for the reordering to actually happen. For some reason, xcode was not actually including the new file. – Jbryson Jun 27 '13 at 12:46