Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiling flex and bison into cpp

Usually, when we compile .l and .y files, we get .tab.h,.tab.c and .yy.c files. However, I need to use these in a C++ environment with types that are only available in C++. How do you modify your code so as to do this?

like image 819
Robin Thuran Malhotra Avatar asked Jan 10 '23 13:01

Robin Thuran Malhotra


2 Answers

You can compile the generated files using your trustworthy C++ compiler, so you don't have to rewrite the whole thing.

If you want to go "full c++", keep reading.

For Flex, you need to specifiy %option c++ in the file. You can also change the generated C++ lexer name using --yyclass=NAME command line argument.

For Bison, just follow these instructions (excerpt taken from Bison manual)

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.

P.S: Be aware that c++ and reentrant options are mutually exclusive in Flex.

like image 174
Matteo Pacini Avatar answered Jan 19 '23 18:01

Matteo Pacini


You can specify output filenames with command-line flags, so you don't need to modify your code at all to produce .cc files, which will save you some complication with makefiles or equivalent.

As long as your actions and other code blocks are valid C++ code and you don't include non-POD types in the semantic union directly (pointers are fine; smart pointers, not), you shouldn't need to modify anything.

You could also use the C++ templates in flex and bison, but I've generally found it easier to stick with the C templates, which will compile just fine with C++.

like image 41
rici Avatar answered Jan 19 '23 16:01

rici