0

C++flexおよびを使用して、簡単な文法でおもちゃの言語を作成しようとしていますbisontypes.hpp、、、の 4 つのファイルがscanner.lありparser.yますMakefile。コンパイルしようとすると、types.hppld 内の各関数に対して、既に定義されていると表示されます。その問題は include ディレクティブにあると思います。各ファイルの冒頭にあるのは次のとおりです(文法の内容は、それが理由ではないと思うので省略します。必要があれば公開します)。

// スキャナー.l

%{
#include "parser.hpp"
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <string>
using namespace std;

extern "C" {
  int yylex(void);
} /* extern "C" */

char BUFFER[32768];
int POSITION;

%}

%option noyywrap
%x COMMENT
%x BYTESMODE
%x indent
%s normal

// parser.y

%{
#include <iostream>
using namespace std;

extern "C" {
  int yylex(void);
  int yyparse(void);
  int yywrap() { return 1; }
} /* extern "C" */

void yyerror(const char *error) {
  cerr << error << endl;
} /* error handler */

%}

/*============================================================================*/
/* Create Bison union and stack */
/*============================================================================*/
%code requires {
#include "types.hpp"
}

%union {
  object_type* pointer;
  type_type*   type_buffer;
  none_type*   none_buffer;
  bool_type*   bool_buffer;
  int_type*    int_buffer;
  float_type*  float_buffer;
  bytes_type*  bytes_buffer;
} /* union */

// types.hpp

#include <iostream>
#include <typeinfo>
#include <sstream>
#include <string>
using namespace std;

//============================================================================//
// Declare classes
//============================================================================//
class object_type;
class none_type;
class type_type;
class bool_type;
class int_type;
class float_type;
class bytes_type;

type_type  type_function(object_type* object);
bytes_type name_function(object_type* object);
bytes_type repr_function(object_type* object);
bool_type  bool_function(object_type* object);
int_type   int_function(object_type* object);
float_type float_function(object_type* object);
bytes_type bytes_function(object_type* object);

// メイクファイル

caesar: scanner.l parser.y types.hpp
    clear && clear && clear
    bison -d parser.y -o parser.cpp --graph
    flex -o scanner.cpp scanner.l
    g++ -Wall -g -o $@ parser.cpp scanner.cpp -lfl

エラーはどこにありますか? 些細なことだと思いますが、私は C++ の初心者なので、見つけるのはかなり困難です。前もって感謝します!必要に応じて、コード全体を投稿します。

エラーメッセージの例を次に示します。

/home/ghostmansd/lang/types.hpp:559: multiple definition of `repr_function(object_type*)'
/tmp/ccv2zJdS.o:/home/ghostmansd/lang/types.hpp:559: first defined here

リポジトリ: http://github.com/ghostmansd/caesar

4

2 に答える 2

1

問題は、ヘッダーファイルに実装があることです。

type_type::~type_type(void) { /* destructor */ }

これはtypes.cppに移動する必要があります。types.hppでは、クラス定義のみが必要です(インライン化しない限り、それが2番目のステップになります)。これは、types.hppが複数回含まれている場合、2つの実装がぶら下がっていて、リンカーが気に入らないためです。

また、types.hppで変数を宣言しないでください。

于 2012-12-23T20:25:51.347 に答える
1

ヘッダー ファイルで定数を定義します。

const none_type NONE_TYPE;
const bool_type BOOL_TYPE;
const int_type INT_TYPE;
const float_type FLOAT_TYPE;
const bytes_type BYTES_TYPE;

同じシンボルが複数存在するため、リンク中に問題が発生します。

その問題を回避する 1 つの方法:

これらはヘッダーで外部としてのみ宣言する必要があります。

extern const none_type NONE_TYPE;
extern const bool_type BOOL_TYPE;
extern const int_type INT_TYPE;
extern const float_type FLOAT_TYPE;
extern const bytes_type BYTES_TYPE;

そして、1 つの cpp ファイルでのみ定義します。

const none_type NONE_TYPE;
const bool_type BOOL_TYPE;
const int_type INT_TYPE;
const float_type FLOAT_TYPE;
const bytes_type BYTES_TYPE;

また、ヘッダーの関数定義でも同様の問題が発生するようです。

于 2012-12-23T20:28:54.663 に答える