boost-spirit

Resolve ambiguous boost::spirit::qi grammar with lookahead

☆樱花仙子☆ 提交于 2019-12-06 08:15:41
I want to a list of name-value pairs. Each list is terminated by a '.' and EOL. Each name-value pair is separated by a ':'. Each pair is separated by a ';' in the list. E.g. NAME1: VALUE1; NAME2: VALUE2; NAME3: VALUE3.<EOL> The problem I have is that the values contain '.' and the last value always consumes the '.' at the EOL. Can I use some sort of lookahead to ensure the last '.' before the EOL is treated differently? I have created a sample, that presumably looks like what you have. The tweak is in the following line: value = lexeme [ *(char_ - ';' - ("." >> (eol|eoi))) ]; Note how - ("." >

Why does using a stream in boost spirit penalize performance so much?

邮差的信 提交于 2019-12-06 07:43:16
I have prepared a small benchmark program for measuring different ways of parsing. The problem comes with the huge decrease in performance when using a stream and a custom function for storing a date as a time_t + double. The weird boost spirit trait for std::string is because seek backtracking fills the variable string with all the common parts of non-matching lines until a line that matches is found. Sorry for the source code quality (copy/paste, bad variable names, weak indentation...). I am aware that this benchmark code is not going to be included in Clean Code book, so please ignore this

Recursive x3 parser with results passing around

a 夏天 提交于 2019-12-06 07:41:42
(1) Say we want to parse a simple recursive block surrounded by {} . { Some text. { { Some more text. } Some Text again. {} } } This recursive parser is quite simple. x3::rule<struct idBlock1> const ruleBlock1{"Block1"}; auto const ruleBlock1_def = x3::lit('{') >> *( ruleBlock1 | (x3::char_ - x3::lit('}')) ) >> x3::lit('}'); BOOST_SPIRIT_DEFINE(ruleBlock1) (2) Then the block becomes more complex. It could also be surrounded by [] . { Some text. [ { Some more text. } Some Text again. [] ] } We need somewhere to store what kind of opening bracket that we have. Since x3 does not have locals, we

Parsing with Boost::Spirit (V2.4) into container

旧城冷巷雨未停 提交于 2019-12-06 07:15:36
I just started to dig into Boost::Spirit, latest version by now -- V2.4. The essense of my problem is following: I would like to parse strings like "1a2" or "3b4". So the rule I use is: (double_ >> lit('b') >> double_) | (double_ >> lit('a') >> double_); The attribute of the rule must be "vector <double>". And I'm reading it into the container. The complete code: #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <iostream> #include <algorithm> #include <string> #include <vector> #include

How can i implement const in Boost Spirit?

亡梦爱人 提交于 2019-12-06 05:58:30
I am interested in Boost Spirit nowadays and trying to build something. Can we implement something like a const in C++ using Spirit? For instance, user will define an item like; constant var PROG_LANG="Java"; "constant var" seems weird, I accept but you got the idea. I searched the internet but can't found anything about it. sehe What the BigBoss said :) Only I'd do without the semantic actions - making it far less... verbose (See also Boost Spirit: "Semantic actions are evil"? ): vdef = ("constant" >> attr(true) | attr(false)) >> "var" >> identifier >> '=' >> identifier_value >> ';' ; That's

How can I simply consume unrecognized characters?

試著忘記壹切 提交于 2019-12-06 05:20:55
问题 I managed to parse a pgn file thanks to the Boost Spirit library, but it fails as soon as there is some characters I did not "anticipated". Here is my Spirit grammar : #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/fusion/include/adapt_struct.hpp> BOOST_FUSION_ADAPT_STRUCT( loloof64::pgn_tag, (std::string, key), (std::string, value) ) BOOST_FUSION_ADAPT_STRUCT( loloof64::game_move, (unsigned, move_number), (std::string, move_turn), (std:

boost::spirit::qi

99封情书 提交于 2019-12-06 04:34:47
问题 Consider the following code: (Boost.Spirit 2.5.1) qi::parse(str.begin(), str.end(), (+qi::alpha)[[](const string& s){cout << s<< '\n';}] >> (*(qi::char_(',') | qi::char_('\''))) >> qi::uint_[[](int integer){cout << integer << '\n';}]); The [[](int integer){cout << integer << '\n';}] works, but the analogous code for +qi::alpha doesn't. How can I correct the code? 回答1: C++0x/C++11 lambdas aren't yet supported by Boost Spirit 1 Edit Apparently support improved (I tested with an older boost

Using boost spirit for a stack based language

心不动则不痛 提交于 2019-12-06 03:58:28
问题 I need to parse a rather simple stack based language, e.g. 1 2 add 3 1 sub and I'm facing two choices here: Write my own lexer for the tokens and then proceed parsing it Use boost spirit I have never used boost spirit but from what I've read around (documentation and examples) I can't still make up my mind on whether it would be overkill to use boost spirit to lex and parse this simple language or if it would make sense to use it instead of rolling out my own lexer and parser (thing that I

Qi Symbols slow performance?

痞子三分冷 提交于 2019-12-06 03:20:43
问题 I wanted to raise a subject that just sent me down a rabbit hole and brought up a question about qi::symbols. It all started while I was looking into the new beast library and read a tutorial example It starts with a function that guesses mime types from http path extensions. I started to look more closely and saw this: auto const ext = [&path] { auto const pos = path.rfind("."); if(pos == boost::beast::string_view::npos) return boost::beast::string_view{}; return path.substr(pos); }(); It

Is it possible to attach an action to a boost::spirit::rule parser which assigns the parsed result to a member of a (yet) unknown instance?

扶醉桌前 提交于 2019-12-06 01:25:45
I'm trying to reference a member of a (yet) unknown instance from within a boost::spirit rule definitions' action, so in pseudocode, instead of double_[ref(rN) = _1] I'm looking for something like X** ppx; double_[ref(&X::rN, ppx) = _1] A workaround for it could be a simple "semantic action" with a parameter which knows the instance and would be able to write to it, like qi::rule<Iterator, Skipper> start; my_grammar(DataContext*& dataContext) : my_grammar::base_type(start) , execContext(execContext) { start = qi::double_[ boost::bind(&my_grammar::newValueForXY, dataContext, ::_1) ]; However, I