[英]Bison Flex linking problems
There is a parser running on bison & flex .有一个解析器在bison & flex上运行。 To build and make the whole project cmake is used.
使用 cmake 来构建和制作整个项目。
So, from flex file I create lexer.cpp file and from bison file parser.cpp & parser.hpp .所以,从 flex 文件我创建lexer.cpp文件和野牛文件parser.cpp & parser.hpp 。 The code looks like this :
代码如下所示:
lexer.flex : lexer.flex :
%{
#include "structures/RedirectionExpr.h"
// and about 8 includes like the upper one
#include <iostream>
#include <bits/stdc++.h>
#include "parcer.hpp"
using namespace std;
%}
%option noyywrap
%option c++
%%
/* Some staff */
%%
parser.ypp :解析器.ypp:
%{
#include "structures/RedirectionExpr.h"
// and about 8 includes like the upper one, like in lexer.flex
#include <bits/stdc++.h>
#include <iostream>
extern "C" int yylex(void);
extern "C" int yyparse();
extern "C" int errors;
void yyerror (char const *s);
using namespace std;
%}
%union {
/* Union types. */
}
// %token definitions
// %type definitions for grammar
%%
/* grammar is here */
%%
void yyerror (char const *s) { /* Some code here */ }
int main(int argc, char *argv[])
{
do {
yyparse();
} while (true);
return 0;
}
Before compilation, I manually create cpp files from .flex
and .ypp
files:在编译之前,我从
.flex
和.ypp
文件手动创建 cpp 文件:
flex -o lexer.cpp lexer.flex
flex -o lexer.cpp lexer.flex
bison -d -o parser.cpp parser.ypp
野牛 -d -o parser.cpp parser.ypp
To build all this mess, I use cmake:为了构建所有这些混乱,我使用 cmake:
CMakeLists.txt CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(parse)
set(CMAKE_CXX_STANDARD 14)
add_executable(parse
src/structures/RedirectionExpr.cpp
# other includes of classes used
src/lexer.cpp
src/parser.cpp src/parser.hpp
)
So, the problem appears, when linking :因此,链接时出现问题:
/usr/sbin/ld: CMakeFiles/parse.dir/src/parser.cpp.o: in function `yyparse':
parser.cpp:(.text+0x31a): undefined reference to `yylex'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/parse.dir/build.make:294: parse] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/parse.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
As a solution I tried to add %noyywrap
option, include parser.hpp
into .flex
file after than I include additional classes, put extern "C" int yylex(void);
作为一种解决方案我试图添加
%noyywrap
选项,包括parser.hpp
成.flex
文件比后我包括附加的类,把extern "C" int yylex(void);
line into parser.ypp
etc. But right now I have no idea how to fix it.进入
parser.ypp
等。但现在我不知道如何修复它。 Can you help me with it?你能帮我解决吗?
I solved the problem by removing extern "C"
and leaving int yylex(void);
我通过删除
extern "C"
并留下int yylex(void);
解决了这个问题int yylex(void);
part in parser.ypp
file. parser.ypp
文件中的一部分。 You can read more about it here .您可以在此处阅读更多相关信息。
It looks to me like you're intending to use the C lexer API.在我看来,您打算使用 C 词法分析器 API。 After all, in your parser you say
毕竟,在你的解析器中你说
extern "C" int yylex(void);
So it's a bit puzzling that you use %option c++
in your flex file.因此,在 flex 文件中使用
%option c++
有点令人费解。 If you do that, you will get the C++ interface, which does not include a "C" yylex()
.如果这样做,您将获得 C++ 接口,其中不包括“C”
yylex()
。
I think your easy option would be to delete that %option
and change the declaration of yylex
to just我认为您的简单选择是删除该
%option
并将yylex
的声明更改为
int yylex();
without the extern "C"
没有
extern "C"
If you really want to use the C++ interfaces, I believe the Bison manual includes an example of using a C++ flex lexer.如果您真的想使用 C++ 接口,我相信 Bison 手册包含使用 C++ flex 词法分析器的示例。 It's more work but I'm sure it pays off.
这是更多的工作,但我相信它会得到回报。
Also, I'm not a CMake user but I'm pretty sure CMake does know how to compile flex/bison projects (see, for example, this answer ).另外,我不是 CMake 用户,但我很确定 CMake 确实知道如何编译 flex/bison 项目(例如,参见这个答案)。 It might get confused by the C++ interfaces, but it looks like it's easy to do a traditional C build.
它可能会被 C++ 接口混淆,但看起来很容易进行传统的 C 构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.