這裏的功能是一個simplist看法: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) ];
凡_functions
是函數的qi::symbols
表:
qi::symbols<char, std::function<bool(data_t const& args, data_t& into)> > _functions;
現在,請注意,輸入"17, 5, fibonacci(2, 4), 71, 99, range(5, 7)"
結果
parse success
data: 17 5 1 2 3 71 99 5 6 7
但是,你甚至可以得到更多的時髦:"range(fibonacci(13, 14))"
結果:
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
正如你可以看到,它打印從[fib(13)..fib(14)]
which is [233..377] (Wolfram Alpha)
範圍。
的完整代碼(包括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 i = 0u; i <= 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); // to 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))"));
}
這是很酷,我可以告訴。我需要一些時間來吸收它 – gsf
@gsf它真的很小,整個「技巧」在'phx :: bind(_1,...)'中用'_functions'調用函數'registered'。 – sehe
現在,這是快速和骯髒。如果你進一步發展了一些東西,請參閱[本答案](http://stackoverflow.com/a/17013713/85371),它解析包含函數評估的表達式語法並使用AST表示法。請參閱[其他答案](http://stackoverflow.com/a/17014063/85371)跳過AST創建的方法(如在此答案中) – sehe