增强精神气与多种元素的搭配 [英] boost spirit qi match multiple elements

查看:117
本文介绍了增强精神气与多种元素的搭配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于Boost Spirit Qi创建一个解析器,该解析器将能够解析整数值列表.这显然非常容易,并且有很多示例.该列表比逗号分隔的列表要聪明一些,它看起来像:

I would like to create a parser based on boost spirit qi that will be able to parse a list of integer values. That is obviously extremely easy and there are tons of examples. The list though is a bit smarter than a comma separated list and it could looks like:

17、5,斐波那契(2、4),71、99,范围(5、7)

17, 5, fibonacci(2, 4), 71, 99, range(5, 7)

解析器的结果应为具有以下值的std :: vector:

the result of the parser should be a std::vector with the following values:

17、5、1、2、3、71、99、5、6、7

17, 5, 1, 2, 3, 71, 99, 5, 6, 7

其中fibonacci(2,4)得出1,2,3而range(5,7)得出5,6,7

Where fibonacci(2, 4) results in 1, 2, 3 and range(5, 7) results in 5, 6, 7

我要寻找的是如果我已经具有属性int(例如int_)的解析器和具有属性std :: vector fibonacci和range的解析器,那么如何将结果组合到单个解析器中.像这样:

What I am looking for is if I already have parsers that have an attribute int (say int_) and parsers that have an attribute std::vector fibonacci and range, how I can combine the results in a single parser. Something like:

list %= *(int_ | elements [ fibonacci | range ] );

其中元素是魔术,将进行必要的魔术处理,结果形成斐波那契以适合列表.

Where elements to be the magic that will do the necessary magic the results form fibonacci to fit in the list.

注意:我不是在寻找包含诸如追加功能之类的解决方案

Note: I am not looking for solution that includes append functions like

list = *(int_[push_back(_val, _1)] | fibonacci[push_back(_val, _1)] | range[push_back(_val, _1)] ] );

推荐答案

以下是清单摘要:

Here's a 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) ];

其中_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)"会导致

Now, note that the 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 

但是您甚至可以变得更加时髦:"range(fibonacci(13, 14))"结果:

But you can even get 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 

如您所见,它打印的范围是 [fib(13)..fib(14)],它是[233..377] (Wolfram Alpha) .

As you can see, it prints the range from [fib(13)..fib(14)] which is [233..377] (Wolfram Alpha).

完整代码(包括fibonaccirange的演示实现):

Full code (including demo implementations of fibonacci and 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))"));
}

这篇关于增强精神气与多种元素的搭配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆