Reputation: 91
I am trying to write a parser using boost spirit in C++. I want the output of this parser in the form of class objects. I read about semantic actions but I'm not sure how to create my class objects in these actions.
Also, I read that "boost phoenix" is a good library to using for semantic actions. But, did not find extensive examples of its use with boost spirit.
Any help or direction would be appreciated. Thank you.
Upvotes: 0
Views: 1152
Reputation: 393084
Yes. You can. Just start with the tutorial:
https://www.boost.org/doc/libs/1_68_0/libs/spirit/doc/html/spirit/qi/tutorials.html
An important thing to keep in mind is that semantic actions are not the preferred method to create your AST nodes. That seems to be a recurring pattern coming from Flex/Bison style tools.
Instead, prefer to use automatic attribute synthesis and propagation. That way you get the embedded DSL spirit as was intended. That's what reduces amount of work and room for manual error. See also Boost Spirit: "Semantic actions are evil"?
If you
then I'd suggest to go with a more traditional parser generator, that usually require more work out of the gate.
Well, it is "nice" if you're geeky and need the power to quickly make things work, but keep in mind that it is not core language support, so it taxes your compiler heavily, and has limitations.
I'd say that Spirit X3's semantic actions are a much finer approach, where you have all of the C++14 language at your disposal without library heroics. That reduces the learning curve and allows you to use existing code without e.g. wrapping in polymorphic calleables, adapting with BOOST_PHOENIX_ADAPT_* etc.
Just for fun, here's an X3 parser where I implemented a basic subset of Phoenix for X3 in a single header file: https://github.com/sehe/expression-parsers/tree/x3-c++17
I suppose if you search (my) answers on this site, it's an all-you-can-eat buffet. Of course, you can always post concrete questions you have.
Upvotes: 0