parsing into several vector members

There are several ways 🙂 Custom attribute traits The same using semantic actions Everything in semantic actions, at detail level 1. Custom attribute traits The cleanest, IMO would to replace the Fusion Sequence Adaptation (BOOST_FUSION_ADAPT_STRUCT) by custom container attribute traits for Spirit: namespace boost { namespace spirit { namespace traits { template<> struct is_container<ElemParseData, void> … Read more

Boost::Spirit::QI parser: index of parsed element

There are a number of approaches. What I’d usually recommend instead, is using well thought out repeat(n) expressions with directly exposed container attributes (like vector<vector<double> >). What you seem to be looking for is semantic actions with state. (This is common practice coming from lex/yacc). I treat these approaches in three full demos below (1., … Read more

How to benchmark Boost Spirit Parser?

I have given things a quick scan. My profiler quickly told me that constructing the grammar and (especially) the lexer object took quite some resources. Indeed, just changing a single line in SpiritParser.cpp saved 40% of execution time1 (~28s down to ~17s): lexer::Lexer lexer; into static const lexer::Lexer lexer; Now, making the grammar static involves … Read more

How to parse csv using boost::spirit

For a background on parsing (optionally) quoted delimited fields, including different quoting characters (‘, “), see here: Parse quoted strings with boost::spirit For a very, very, very complete example complete with support for partially quoted values and a splitInto(input, output, ‘ ‘); method that takes ‘arbitrary’ output containers and delimiter expressions, see here: How to … Read more

Boost::Spirit Expression Parser

It isn’t entirely clear to me what you are trying to achieve. Most importantly, are you not worried about operator associativity? I’ll just show simple answers based on using right-recursion – this leads to left-associative operators being parsed. The straight answer to your visible question would be to juggle a fusion::vector2<char, ast::expression> – which isn’t … Read more

undefined behaviour somewhere in boost::spirit::qi::phrase_parse

You cannot use auto to store parser expressionsÂą Either you need to evaluate from the temporary expression directly, or you need to assign to a rule/grammar: const qi::rule<std::string::const_iterator, qi::space_type> doubles_parser_local = qi::double_ >> *(‘,’ >> qi::double_); You can have your cake and eat it too on most recent BOost versions (possibly the dev branch) there … Read more

How to provider user with autocomplete suggestions for given boost::spirit grammar?

Out of curiosity, I decided to try the concept. Here’s my attempt. Plan Let’s parse arithmetic expressions with function calls. Now, we want to parse (partial) expression with possible unknown identifiers. In case of incomplete expressions, we want to “imply” the minimal token to complete it (and potentially continue parsing). In case of unknown identifiers, … Read more

Understanding Boost.spirit’s string parser

It’s not the string matcher per se. It’s [attribute propagation] + [backtracking] in action. A string attribute is a container attribute and many elements could be assigned into it by different parser subexpressions. Now for efficiency reasons, Spirit doesn’t rollback the values of emitted attributes on backtracking. Often this is no problem at all, but … Read more