将flex和bison编译为cpp

Rob*_*tra 3 c++ bison flex-lexer

通常,当我们编译.l和.y文件时,我们会获得.tab.h,.tab.c和.yy.c文件。但是,我需要在C ++环境中使用仅在C ++中可用的类型。您如何修改代码才能做到这一点?

Mat*_*ini 5

您可以使用值得信赖的C ++编译器来编译生成的文件,因此您不必重写整个内容。

如果您想使用“完整的c ++”,请继续阅读。

对于Flex,您需要在文件中指定%option c ++。您也可以使用--yyclass = NAME命令行参数更改生成的C ++词法分析器名称。

对于Bison,只需遵循以下说明(摘录自Bison手册)

if you need to put  C++  code  in  the
input  file, you can end his name by a C++-like extension (.ypp or .y++), then bison will follow your exten-
sion to name the output file (.cpp or .c++).  For instance, a grammar description file named parse.yxx would
produce the generated parser in a file named parse.tab.cxx, instead of yacc y.tab.c or old Bison version's
parse.tab.c.
Run Code Online (Sandbox Code Playgroud)

PS:请注意,c ++可重入选项在Flex中是互斥的。