【发布时间】:2015-03-03 04:49:42
【问题描述】:
我在编译我的 flex 和 bison 代码时遇到了问题。更具体地说是我的 parser.yy 文件。在这个文件中,我包含了 MathCalc.h 和 BaseProg.h,它们是我创建的类。问题是当我实例化类时,它在编译时给了我一个“多重定义”错误。任何帮助,将不胜感激!谢谢!!
Parser.yy (sn-p):
%code requires {
#include <iostream>
#include <cmath>
#include "MathCalc.h"
#include "BaseProg.h"
/* Parser error reporting routine */
void yyerror(const char *msg);
/* Scannar routine defined by Flex */
int yylex();
using namespace std;
BaseProg bprog;
MathCalc calc;
enum Type { INT, FLT};
}
/* yylval union type */
%union {
double dval;
int ival;
char* name;
Type type;
}
错误:
bison -d parser.yy
g++ -c -o scanner.o scanner.cc
g++ -c -o parser.tab.o parser.tab.cc
g++ scanner.o parser.tab.o BaseProg.o MathCalc.o -lfl -o ../Proj2
parser.tab.o:(.bss+0x0): multiple definition of `bprog'
scanner.o:(.bss+0x28): first defined here
parser.tab.o:(.bss+0x1): multiple definition of `calc'
scanner.o:(.bss+0x29): first defined here
collect2: ld returned 1 exit status
【问题讨论】:
-
仅供参考,“flex”标签指的是 Apache Flex。对于您的问题,“flex-lexer”标签是合适的。
标签: c++ parsing bison yacc flex-lexer