boost-spirit

Parentheses in template parameters in Boost Spirit rules and grammars

夙愿已清 提交于 2019-12-04 10:27:30
Looking at this example for implementing a Spirit parser, something caught me out when I was trying to write something similar. The attribute template parameter of the grammar ( std::map<std::string, std::string>() ) and the signature template parameter of the rules (e.g. qi::rule<Iterator, std::string()> key, value ) contain parentheses. namespace qi = boost::spirit::qi; template <typename Iterator> struct keys_and_values : qi::grammar<Iterator, std::map<std::string, std::string>()> // <- parentheses here { keys_and_values() : keys_and_values::base_type(query) { query = pair >> *((qi::lit(';'

boost::spirit::qi

丶灬走出姿态 提交于 2019-12-04 10:06:56
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? C++0x/C++11 lambdas aren't yet supported by Boost Spirit 1 Edit Apparently support improved (I tested with an older boost version earlier today). Using Boost 1_48 and g++ 4.6.1 now, the following appears to just_work. Yay! qi::as

How can I simply consume unrecognized characters?

走远了吗. 提交于 2019-12-04 09:32:27
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::string, white_move), (std::string, black_move), (std::string, result) ) BOOST_FUSION_ADAPT_STRUCT(

Using boost spirit for a stack based language

时间秒杀一切 提交于 2019-12-04 09:26: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 suppose shouldn't be too hard). Would using boost spirit for a simple stack based language like the one

Boost.Spirit.Qi: How to return attributes with Nabialek trick

两盒软妹~` 提交于 2019-12-04 09:17:09
问题 Following several tutorials (e.g. http://boost-spirit.com/home/articles/qi-example/nabialek-trick/) I want to use the Nabialek trick to have a dynamic parser. Parsing already works fine, but I don't get the attributes transported. Explanations like https://stackoverflow.com/a/9109972/2524462 suggest, that attributes should be possible but not arguments. This is just a small example parsing a string and a number into a struct. It is just for showcasing my problem; this method should be used in

Qi Symbols slow performance?

半腔热情 提交于 2019-12-04 08:07:37
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 took me a while to figure out it was a IIFE in C++ style, and was used to initialize ext while declaring

parsing into std::vector<string> with Spirit Qi, getting segfaults or assert failures

两盒软妹~` 提交于 2019-12-04 05:03:03
问题 I am using Spirit Qi as my parser, to parse mathematical expressions into an expression tree. I keep track of such things as the types of the symbols which are encountered as I parse, and which must be declared in the text I am parsing. Namely, I am parsing Bertini input files, a simple-ish example of which is here, a complicated example is here, and for completeness purposes, as below: %input: our first input file variable_group x,y; function f,g; f = x^2 - 1; g = y^2 - 4; END; The grammar I

How future-safe is it to write a parser with Boost Spirit X3?

别来无恙 提交于 2019-12-04 01:42:38
问题 I'm considering writing what is essentially my first parser since forever (= since the compiler class at Uni which I've forgotten mostly). Since I use C++, I was thinking of using Boost Spirit. Then I noticed there's the "regular" 2.5.2 and there's something magical subset of the code named Spirit X3. I' also ve noticed that Boost Spirit X3 was announced/discussed/pre-released already 2 years ago, yet Boost Spirit's official version is 2.5.2. Finally, I read: Where is boost-spirit 3? Is it

Spirit X3, Is this error handling approach useful?

自作多情 提交于 2019-12-03 23:17:45
问题 After reading the the Spirit X3 tutorial on error handling and some experimentation. I was drawn to a conclusion. I believe there is some room for improvement on the topic of error handing in X3. An important goal from my perspective is to provide a meaningful error message. First and foremost adding a semantic action that will set the _pass(ctx) member to false wouldn’t do it because X3 will try to match something else. Only throwing an x3::expectation_failure will quit the parse function

X3 parse rule doesn't compile

白昼怎懂夜的黑 提交于 2019-12-03 21:14:36
I'm learning Boost Spirit by writing a parser that parses two variants of hex number used by NAMS: Hex number with either suffix of 0x / 0h or prefix of h / x . Hex number with prefix of $ and must be followed by a decimal digit. Here is what I have come up so far and with Coliru Session : //#define BOOST_SPIRIT_X3_DEBUG #include <iostream> #include <boost/spirit/home/x3.hpp> #include <boost/spirit/home/x3/support/ast/variant.hpp> #include <boost/spirit/include/support_extended_variant.hpp> namespace x3 = boost::spirit::x3; namespace ast { struct hex_data : std::string {}; struct pascal_hex