如何在原生 Node 插件中成功链接 Flex、Bison 和 Node.js?
我正在尝试使用 Flex/Bison 编写一个原生 Node.js 解析器。该插件由三个部分组成:词法分析器、解析器和 Node/v8 接口。词法分析器和解析器一开始运行良好(我使用 g++ 编译词法分析器/解析器,并包含必要的词法分析器函数,如下所示:http://tldp.org/HOWTO/Lex-YACC-HOWTO-5.html)。当我将 node/v8 部分添加到混合中时,我最终得到了词法分析器代码中定义的函数的缺少符号错误。
以下是文件结构的概述:
File A: Lexer file, automatically generated by flex from the lexer rules I wrote.
File B: The parser file, includes the extern "C" { [lexer functions] } and
a function called parse(std::string). parse() calls some yy_functions,
which are defined in the lexer, and returns a c++ data structure
representing the parsed data.
File C: This is the node/v8 part. There is a header file with the prototype for
B's parse() function. When JS calls a certain function, the data is
converted to a c++ string and passed to the parse() function in File B.
当我运行一个简单的测试来解析 JS 字符串时,我收到一条错误消息
node: symbol Lookup error: /home/monty/projects/flex_bison/GetObj_Request_Parser/build/default/GetObjParser .node:未定义符号:yy_scan_string
注意:我使用node-waf来构建插件,并将flex和bison生成的文件从[FILE].c重命名为[FILE].cc,以便构建脚本会拾取它们。
如果您想查看一些代码,可以在这里找到全部内容: https://github.com/IDX -io/GetObj_Request_Parser(查看 src/buildstuffs 目录)。
File A = src/buildstuffs/lex.yy.c[c]
File B = src/buildstuffs/geto.tab.c[c]
File C = src/buildstuffs/GetObjParser.cc
Test = test.js
谢谢!
[编辑] 可以使用 flex++ 和 bison++ 来生成 C++,而不是使用两个不同的编译器并搞乱 node-waf 构建脚本(我的解决方案)。
I'm trying to write a native Node.js parser using Flex/Bison. There are three parts to the addon: the lexer, the parser, and the node/v8 interface. The lexer and the parser were working well in the beginning (I used g++ to compile the lexer/parser and included the necessary lexer functions like this: http://tldp.org/HOWTO/Lex-YACC-HOWTO-5.html). When I added the node/v8 section into the mix, I ended up getting a missing symbol error for a function defined in the lexer code.
Here's an overview of the file structure:
File A: Lexer file, automatically generated by flex from the lexer rules I wrote.
File B: The parser file, includes the extern "C" { [lexer functions] } and
a function called parse(std::string). parse() calls some yy_functions,
which are defined in the lexer, and returns a c++ data structure
representing the parsed data.
File C: This is the node/v8 part. There is a header file with the prototype for
B's parse() function. When JS calls a certain function, the data is
converted to a c++ string and passed to the parse() function in File B.
When I run a simple test to parse a JS string, I get an error saying
node: symbol lookup error: /home/monty/projects/flex_bison/GetObj_Request_Parser/build/default/GetObjParser.node: undefined symbol: yy_scan_string
Note: I'm using node-waf to build the addon and I've renamed the files generated by flex and bison from [FILE].c to [FILE].cc so that the build script picks them up.
If you would like to see some code, you can find it all here: https://github.com/IDX-io/GetObj_Request_Parser (look in the src/buildstuffs dir).
File A = src/buildstuffs/lex.yy.c[c]
File B = src/buildstuffs/geto.tab.c[c]
File C = src/buildstuffs/GetObjParser.cc
Test = test.js
Thanks!
[EDIT] Instead of using two different compilers and messing around with the node-waf build script (my solution), one could probably use flex++ and bison++ to generate C++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将文件重命名为 .cc 通常会导致使用 c++ 而不是 c 构建默认 make 规则。这可能意味着 lex.yy.c 文件中的函数链接错误。我认为您需要找出另一种方法来使节点构建系统识别您的 .c 文件并使用 as c 而不是 c++ 正确构建它们
renaming the files to .cc will generally cause default make rules to build using c++ instead of c. That could mean that the linkage is wrong on the functions in the lex.yy.c file. I think you need to figure out another way to make the node build system recognize your .c files and build them properly with as c, not c++
删除
geto.y
文件顶部的extern "C"
。 Flex 函数不通过 C 链接导出。Get rid of the
extern "C"
at the top of yourgeto.y
file. The flex functions aren't being exported with C-linkage.