1

私は次のコードを持っています (floatDecs と intDecs はシンボルパーサーです):

// Definition of the value parser:
typedef boost::variant<double,int64_t> value_type;
typedef boost::fusion::vector<std::string, value_type> dec_type;
rule<std::string::const_iterator, boost::variant<double,int64_t>(std::string)> value;
value   =   real_parser<double, strict_real_policies<double>>() [ boost::phoenix::bind(boost::lambda::unlambda(floatDecs.add), _r1, _1) ] |
            int_parser<int64_t, 10>()                           [ boost::phoenix::bind(boost::lambda::unlambda(intDecs.add), _r1, _1)   ];

rule<std::string::const_iterator, std::string()> ident;
ident %= lexeme[ alpha >> *alnum ];

rule<std::string::const_iterator, dec_type(), boost::spirit::qi::locals<std::string>, space_type> dec;
ident %= ident [_a = _1] >> lit('=') >> value(_a);

boost::spirit::qi::phrase_parse(testing.cbegin(), testing.cend(), dec, space);

問題:各ルールで space_type を削除し、最後の行を次のように置き換えた場合にのみ機能します

boost::spirit::qi::parse(testing.cbegin(), testing.cend(), dec);
4

1 に答える 1

1

あなたが尋ねている問題が何であるかは私にはわかりません。とにかく、投稿したコードのいくつかの問題を修正し、スキッパーで解析が正常に機能することを示すバージョンを次に示します。

http://liveworkspace.org/code/6GVK4$0で実際にご覧ください

出力

phrase_parse: true
allo1 = 213.13f

コード

#include <boost/fusion/adapted.hpp>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/lambda/lambda.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;

int main()
{
    typedef std::string::const_iterator It;
    typedef boost::variant<double,int64_t> value_type;
    typedef std::pair<std::string, value_type> dec_type;

    qi::rule<It, value_type(std::string)> value = 
        qi::real_parser<double, qi::strict_real_policies<double>>() /*[ phx::bind(boost::lambda::unlambda(floatDecs.add), qi::_r1, qi::_1) ]*/ |
        qi::int_parser<int64_t, 10>()                               /*[ phx::bind(boost::lambda::unlambda(intDecs.add), qi::_r1, qi::_1)   ]*/;

    qi::rule<It, std::string()> ident = qi::lexeme[ qi::alpha >> *qi::alnum ];

    qi::rule<It, dec_type(), qi::space_type, qi::locals<std::string> > declaration;
    declaration %= ident [qi::_a = qi::_1] >> '=' >> value(qi::_a);

    std::string testing("allo1 = 213.13");
    dec_type parsed;
    bool ok = qi::phrase_parse(testing.cbegin(), testing.cend(), declaration, qi::space, parsed);
    std::cout << "phrase_parse: " << std::boolalpha << ok << "\n";

    using namespace karma;
    std::cout << format(auto_ << " = " << (double_ << 'f' | int_) << eol, parsed);
}
于 2013-01-17T01:05:30.243 に答える