c++ - boost spirit qi match multiple elements -
i create parser based on boost spirit qi able parse list of integer values. extremely easy , there tons of examples. list though bit smarter comma separated list , looks like:
17, 5, fibonacci(2, 4), 71, 99, range(5, 7)
the result of parser should std::vector following values:
17, 5, 1, 2, 3, 71, 99, 5, 6, 7
where fibonacci(2, 4) results in 1, 2, 3 , range(5, 7) results in 5, 6, 7
edit: looking if have parsers have attribute int (say int_) , parsers have attribute std::vector fibonacci , range, how can combine results in single parser. like:
list %= *(int_ | elements [ fibonacci | range ] );
where elements magic necessary magic results form fibonacci fit in list.
note: not looking solution includes append functions like
list = *(int_[push_back(_val, _1)] | fibonacci[push_back(_val, _1)] | range[push_back(_val, _1)] ] );
here's simplist take: live on coliru
typedef std::vector<int64_t> data_t; value_list = -value_expression % ','; value_expression = macro | literal; literal = int_; macro = (_functions > '(' > value_list > ')') [ _pass = phx::bind(_1, _2, _val) ];
where _functions
qi::symbols
table of functions:
qi::symbols<char, std::function<bool(data_t const& args, data_t& into)> > _functions;
now, note input "17, 5, fibonacci(2, 4), 71, 99, range(5, 7)"
results in
parse success data: 17 5 1 2 3 71 99 5 6 7
but can more funky: "range(fibonacci(13, 14))"
results in:
parse success data: 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
as can see, prints range [fib(13)..fib(14)]
[233..377] (wolfram alpha)
.
full code (including demo implementations of fibonacci
, range
:)):
//#define boost_spirit_debug #define boost_spirit_use_phoenix_v3 #include <boost/fusion/adapted.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/karma.hpp> #include <boost/spirit/include/phoenix.hpp> namespace qi = boost::spirit::qi; namespace karma = boost::spirit::karma; namespace phx = boost::phoenix; typedef std::vector<int64_t> data_t; template <typename it, typename skipper = qi::space_type> struct parser : qi::grammar<it, data_t(), skipper> { parser() : parser::base_type(value_list) { using namespace qi; value_list = -value_expression % ','; value_expression = macro | literal; literal = int_; macro = (_functions > '(' > value_list > ')') [ _pass = phx::bind(_1, _2, _val) ]; _functions.add("fibonacci", &fibonacci); _functions.add("range", &range); boost_spirit_debug_nodes((value_list)(value_expression)(literal)(macro)); } private: static bool fibonacci(data_t const& args, data_t& into) { // unpack arguments if (args.size() != 2) return false; auto f = args[0], l = args[1]; // iterate uint64_t gen0 = 0, gen1 = 1, next = gen0 + gen1; for(auto = 0u; <= l; ++i) { switch(i) { case 0: if (i>=f) into.push_back(gen0); break; case 1: if (i>=f) into.push_back(gen1); break; default: { next = gen0 + gen1; if (i>=f) into.push_back(next); gen0 = gen1; gen1 = next; break; } } } // done return true; } static bool range(data_t const& args, data_t& into) { // unpack arguments if (args.size() != 2) return false; auto f = args[0], l = args[1]; if (l>f) into.reserve(1 + l - f + into.size()); for(; f<=l; ++f) into.push_back(f); // optimize return true; } qi::rule<it, data_t(), skipper> value_list ; qi::rule<it, data_t(), skipper> value_expression, macro; qi::rule<it, int64_t(), skipper> literal; qi::symbols<char, std::function<bool(data_t const& args, data_t& into)> > _functions; }; bool doparse(const std::string& input) { typedef std::string::const_iterator it; auto f(begin(input)), l(end(input)); parser<it, qi::space_type> p; data_t data; try { bool ok = qi::phrase_parse(f,l,p,qi::space,data); if (ok) { std::cout << "parse success\n"; std::cout << "data: " << karma::format_delimited(karma::auto_, ' ', data) << "\n"; } else std::cerr << "parse failed: '" << std::string(f,l) << "'\n"; if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n"; return ok; } catch(const qi::expectation_failure<it>& e) { std::string frag(e.first, e.last); std::cerr << e.what() << "'" << frag << "'\n"; } return false; } int main() { assert(doparse("range(fibonacci(13, 14))")); }
Comments
Post a Comment