c++ - Spirit Boost real_p 解析器行为异常

标签 c++ boost-spirit

我有以下问题:我正在重用使用 real_p 解析器解析实数的旧代码。我想捕获每个数字,将其转换为字符串,并将其放入字符串 vector 中。 我正在使用以下代码,其中 l_double 是 double 类型的变量,convertdouble 函数将 double 转换为字符串,result.m_literalValues 是字符串 vector 。 但是,代码不会将已解析的值分配给 l_double。

rule<> alternative3 =   +(real_p        [assign_a(l_double)]
                                        [push_back_a(result.m_LiteralValues, convertdouble(l_double))]
                                        >>  str_p(",")
                            )

有谁知道我做错了什么吗?

注意:我不会重新设计旧代码,它比给出的示例复杂得多。我只想提取所有已解析值的字符串,并将它们放入字符串 vector 中。

最佳答案

问题好像在push_back_a(result.m_LiteralValues, convertdouble(l_double)),具体在convertdouble(l_double)push_back_a requires它的第二个参数是要存储在“策略持有者参与者”中的引用,因此使用那里的函数调用会导致错误。如果您不需要存储 l_double 并且只是将其用作临时对象,那么实现您想要的功能的一种方法是创建您自己的 phoenix 函数,其行为类似于 push_back_a正如解释的那样 here (完整示例 here)。 您可以这样定义 phoenix 函数:

struct push_back_impl
{
    template <typename Container, typename Item>
    struct result
    {
        typedef void type;
    };

    template <typename Container, typename Item>
    void operator()(Container& c, Item const& item) const
    {
        c.push_back(convertdouble(item));
    }
};

function<push_back_impl> const push_back = push_back_impl();

然后像这样定义你的规则:

rule<> alternative3 =   +( real_p[push_back(var(result.m_LiteralValues),arg1)] >>  str_p(",") );

完整的可编译代码(如果您不能/不想使用 c++11,请更改 for 循环以显示结果):

#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_operators.hpp>
#include <boost/spirit/include/phoenix1_functions.hpp>
#include <boost/spirit/include/phoenix1_primitives.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace boost::spirit::classic;
using namespace phoenix;

std::string convertdouble(const double& d)
{   
    std::stringstream ss;
    ss<<d;
    return ss.str();
}

struct push_back_impl
{
    template <typename Container, typename Item>
    struct result
    {
        typedef void type;
    };

    template <typename Container, typename Item>
    void operator()(Container& c, Item const& item) const
    {
        c.push_back(convertdouble(item));
    }
};

function<push_back_impl> const push_back = push_back_impl();

struct Results
{
    std::vector<std::string> m_LiteralValues;
};


int main()
{
    Results result;
    char const* test="2.5,3.6,4.8,";
    rule<> alternative3 =   +( real_p[push_back(var(result.m_LiteralValues),arg1)] >>  str_p(",") );

    if(parse(test,alternative3,space_p).full)
    {
        std::cout << "success" << std::endl;
        for(auto& str :result.m_LiteralValues)
            std::cout << str << std::endl;
    }
    else
    {
        std::cout << "failure" << std::endl;
    }

    return 0;                       
}

关于c++ - Spirit Boost real_p 解析器行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14276989/

相关文章:

c++ - boost 日期时间解析字符串

c++ - 在 C++ Builder 中使用编码写入文件

c++ - 如何在依赖属性更改时重新评估绑定(bind)到 qml 属性的 C++ 函数?

c++ - Boost spirit : how to parse string of value pairs into map<string, string> 然后返回?

c++ - 在 boost::spirit::karma 中使用 boost::fusion::map

c++ - Boost Spirit 不检查 int 溢出

c++ - 如何在 boost::spirit::qi 中将某些语义 Action 排除在 AST 之外

C++ 模板化构造函数 : Do they prevent certain types from being inferred?

c++ - 尝试在 C++ 中删除对象时发生内存泄漏

c++ - 如何将原始输入捕获到 spirit 语法的合成输出中?