C++
、flex
およびを使用して、簡単な文法でおもちゃの言語を作成しようとしていますbison
。types.hpp
、、、の 4 つのファイルがscanner.l
ありparser.y
ますMakefile
。コンパイルしようとすると、types.hpp
ld 内の各関数に対して、既に定義されていると表示されます。その問題は 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