5

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 :/

GlinesMome
  • 1,549
  • 1
  • 22
  • 35

3 Answers3

4

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

en4bz
  • 1,092
  • 14
  • 18
3

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

Are you sure the error is from Bison? I would venture it comes from your compiler. And probably when it was trying to compile the scanner. I would suggest that your YYSTYPE is not properly defined in your generated header. Try

%code requires { #include "AnalyseurDTD/CP.h" }

so that dtd.h is self-contained. See the documentation about %code.

And please, always provide logs that are complete enough so that we can try to understand your problem. Here, you don't even show the tool you ran, and I hardly think it is Bison.

akim
  • 8,255
  • 3
  • 44
  • 60
  • Thanks but I it fails, it seems to do not find my CP.h : `AnalyseurDTD/dtd.y:17:29: fatal error: AnalyseurDTD/CP.h: No such file or directory` – GlinesMome Apr 05 '13 at 08:18
  • Well, fix your paths. Pass appropriate -I to your compiler. And again, post complete logs if you want actual help instead of divination. – akim Apr 08 '13 at 09:24