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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
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++.
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)
P.S: Be aware that c++ and reentrant options are mutually exclusive in Flex.
I'd try this tutorial for Flex/Bison in C++: http://www.jonathanbeard.io/tutorials/FlexBisonC++
The code repository is up to date and compatible with the latest C++ compilers: https://github.com/jonathan-beard/simple_wc_example
Specifically I'd take a look at the examples
where a string (C++ type) is passed from the parser (.yy file) here:
Basically as long as the C++ binary knows how to handle the object then everything will work nicely. To figure out why this works, I'd definitely check out the GitHub repository and play with the code a bit.
Hope this helps. -J