Kiran
Kiran

Reputation: 757

Expected type-specifier before ';' token in Yacc

I am using yacc to parse one file and store records in RAOperator class object using yacc. I included the corresponding header file in yacc file and defined pointer to RAOperator object in %union directive in yacc file. But when compiling it is giving error as follows:

exp.y:12:28: error: expected type-specifier before ‘;’ token

I am attaching yacc file where union is used with RAoperator class.

%{

#include "RA.h"
#include"y.tab.h"

%}

%union
{
char *strng;
vector<string>  *atr;
RAoperator* asdf;              // This is where error is shown
vector < vector <string> > *table;
}

This is RA.h file where RAoperator is defined.

class RAoperator
{
public:
vector< vector<string> > RArelation;
vector< vector<string> > RAattr;
};

I included all the necessary header files in RA.h file.

I have searched a lot for this error but couldn't find any solution.

Where I went wrong?

Upvotes: 0

Views: 2405

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126203

The problem has to do with where yacc puts the %{ ... %} code from the .y file. It is included in the .tab.c file but NOT in the .tab.h file. So if you have any other code that does #include "y.tab.h", it needs to ALSO #include "RA.h" FIRST, or you'll get errors like this due to RAoperator not being defined (yet)

With bison, you can use %code requires { ... } to specify C code to be copied into BOTH the .tab.c and .tab.h files.

Upvotes: 0

rici
rici

Reputation: 241721

In the line where the error is indicated, is it possible that it actually says:

RAoperator* operator;

instead of asdf? (including the case where you have #define asdf operator or equivalent). asdf seems like an odd tag name; operator would be more logical, but it's a reserved word in C++ and would lead to exactly the error message you provide.

"Expected type-specifier before ';' token" is not an easy error to produce in gcc: this particular use of operator is one of the few cases I know of.

Upvotes: 2

Related Questions