【发布时间】:2012-12-23 19:32:33
【问题描述】:
我正在尝试使用C++、flex 和bison 创建一种语法简单的玩具语言。我有四个文件:types.hpp、scanner.l、parser.y 和 Makefile。当我尝试编译时,types.hpp ld 中的每个函数都表示它已经被定义。我猜这个问题出在包含指令中。这是我在每个文件开头的内容(我省略了语法内容,因为我认为这不是原因;如果有需要,我会发布它):
// 扫描仪.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
【问题讨论】:
-
parser.y 和scanner.l 不完整?尝试在 types.hpp 中使用
#pragma once之类的东西。但是当我完成它们时(只需添加 %% 和一些虚拟规则,它就可以编译和链接。 -
感谢您的评论。你什么意思?我在
scanner.l中定义了令牌,在parser.y中定义了一些工作规则。你想看完整的代码吗? -
@ghostmansd:当然我想看完整的代码,因为我想编译它。
-
@DimaRudnik:谢谢,我已经考虑过了,但是将
#ifndef TYPES_HPP、#define TYPES_HPP和#endif添加到types.hpp并不能解决问题。
标签: c++ compiler-errors g++ bison flex-lexer