Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bison and doesn't name a type error

Tags:

c++

linker

bison

I have the following files:

CP.h

#ifndef CP_H_
#define CP_H_

class CP {
public:
        enum Cardinalite {VIDE = '\0', PTINT = '?', AST = '*', PLUS = '+'};

        CP(Cardinalite myCard);
        virtual ~CP();
private:
        Cardinalite card;
};

#endif /* CP_H_ */

And dtd.y

%{

using namespace std;
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include "AnalyseurDTD/DtdDocument.h"
#include "AnalyseurDTD/CP.h"

void yyerror(char *msg);
int yywrap(void);
int yylex(void);

DtdDocument * doc = new DtdDocument();
%}

%union { 
        char *s;
        DtdElement * dtdelt;
        CP *cpt;
        CP::Cardinalite card;
}

And the following strange error:

AnalyseurDTD/dtd.y:20:2: error: ‘CP’ does not name a type
AnalyseurDTD/dtd.y:21:2: error: ‘CP’ does not name a type

The stange thing is that if I put CP *cpt; after DtdDocument * doc = new DtdDocument(); I have no error :/

like image 339
GlinesMome Avatar asked Feb 16 '23 22:02

GlinesMome


2 Answers

Include the header in your scanner file before you include *.tab.h

// scanner.l file

%{
#include "myheader.h" 
#include "yacc.tab.h"

// other C/C++ code

%}

// helper definitions

%%

// scanner rules 

%%

%union is defined in yacc.tab.h so when you compile you need to make sure the compiler sees your new type definitions before it process yacc.tab.h

like image 177
en4bz Avatar answered Feb 27 '23 05:02

en4bz


Recently I am working with flex and bison. There are several advises that may be helpful for you to locate the problem:

  1. Compile the files one by one to make sure there is no obvious compiling error in your code.
  2. Check the header file generated by bison (something like *.tab.h). Find the definition of YYSTYPE. There is a high probability that it doesn't include your header file AnalyseurDTD/CP.h.
  3. If it's just the case, you should always include AnalyseurDTD/CP.h before you include the *.tab.h. Add it wherever needed to make sure the class CP is defined before YYSTYPE.
  4. If you still cannot locate the problem, try use void *cpt; in the union, then add type conversion in the rest of your code.
like image 33
user3126394 Avatar answered Feb 27 '23 04:02

user3126394