Bison Flex链接问题

问题描述 投票:0回答:1

有一个解析器在bisonflex上运行。要构建和制作整个项目,请使用cmake。

因此,从flex文件中,我创建了lexer.cpp文件,并从野牛文件中创建了[[parser.cpp和parser.hpp。代码如下:

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:

] >>%{ #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; }
在编译之前,我从.flex.ypp文件手动创建cpp文件:

flex -o lexer.cpp lexer.flex

野牛-d -o parser.cpp parser.ypp

要构建所有这些混乱,我使用cmake:

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 )
因此,链接时出现问题:

/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

作为解决方案,我尝试添加%noyywrap选项,然后在包含其他类之后将parser.hpp包含到.flex文件中,然后将extern "C" int yylex(void);行放入parser.ypp等。但是现在我不知道如何修理它。您能帮我吗?

[在bison&flex上有一个解析器。要构建和制作整个项目,请使用cmake。因此,我从flex文件创建lexer.cpp文件,并从野牛文件parser.cpp和parser.hpp创建文件。代码...

c++ cmake bison lex
1个回答
0
投票
在我看来,您打算使用C lexer API。毕竟,在您的解析器中,您说
© www.soinside.com 2019 - 2024. All rights reserved.