Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用容器将单个值解析为ast节点_C++_Parsing_C++14_Boost Spirit_Boost Spirit X3 - Fatal编程技术网

C++ 使用容器将单个值解析为ast节点

C++ 使用容器将单个值解析为ast节点,c++,parsing,c++14,boost-spirit,boost-spirit-x3,C++,Parsing,C++14,Boost Spirit,Boost Spirit X3,我的问题如下。我有一个ast节点,定义如下: struct foo_node{ std::vector<std::string> value; } 但这给了我一个编译时错误,因为x3::string(“bar”)返回一个字符串,而不是std::vector。 我的问题是,如何实现x3::string(“bar”)解析器(以及所有其他返回字符串的解析器)解析为向量 解析单个元素并将其作为单个元素容器属性公开的方法是x3::repeat(1)[p]: #include <

我的问题如下。我有一个ast节点,定义如下:

struct foo_node{
    std::vector<std::string> value;
}
但这给了我一个编译时错误,因为
x3::string(“bar”)
返回一个字符串,而不是
std::vector

我的问题是,如何实现
x3::string(“bar”)
解析器(以及所有其他返回字符串的解析器)解析为向量

解析单个元素并将其作为单个元素容器属性公开的方法是
x3::repeat(1)[p]

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

struct foo_node {
    std::vector<std::string> value;
};

BOOST_FUSION_ADAPT_STRUCT(foo_node, value)

namespace rules {
    auto const bar 
        = x3::string("bar");

    auto const foo_node
        = '(' >> +bar >> ')'
        | x3::repeat(1) [ +bar ]
        ;
}

int main() {
    for (std::string const input : {
            "bar",
            "(bar)",
            "(barbar)",
            })
    {
        auto f = input.begin(), l = input.end();

        foo_node data;
        bool ok = x3::parse(f, l, rules::foo_node, data);

        if (ok) {
            std::cout << "Parse success: " << data.value.size() << " elements\n";
        } else {
            std::cout << "Parse failed\n";
        }

        if (f != l)
            std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
    }
}

解析单个元素并将其作为单个元素容器属性公开的方法是
x3::repeat(1)[p]

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

struct foo_node {
    std::vector<std::string> value;
};

BOOST_FUSION_ADAPT_STRUCT(foo_node, value)

namespace rules {
    auto const bar 
        = x3::string("bar");

    auto const foo_node
        = '(' >> +bar >> ')'
        | x3::repeat(1) [ +bar ]
        ;
}

int main() {
    for (std::string const input : {
            "bar",
            "(bar)",
            "(barbar)",
            })
    {
        auto f = input.begin(), l = input.end();

        foo_node data;
        bool ok = x3::parse(f, l, rules::foo_node, data);

        if (ok) {
            std::cout << "Parse success: " << data.value.size() << " elements\n";
        } else {
            std::cout << "Parse failed\n";
        }

        if (f != l)
            std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
    }
}

请始终发布一份我完全同意MCVE/SSCCE要求的报告。在你的情况下,你有不可能的命名冲突和缺少基本的东西(
SPIRIT\u DEFINE
),只有专家才会知道如何“猜测”。我真的很抱歉,我直到现在才在线。请始终发布我完全同意MCVE/SSCCE的要求。在你的例子中,你有不可能的命名冲突和缺少只有专家才知道如何“猜测”的基本内容(
SPIRIT\u DEFINE
)。我真的很抱歉,我直到现在才在线
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/home/x3.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

struct foo_node {
    std::vector<std::string> value;
};

BOOST_FUSION_ADAPT_STRUCT(foo_node, value)

namespace rules {
    auto const bar 
        = x3::string("bar");

    auto const foo_node
        = '(' >> +bar >> ')'
        | x3::repeat(1) [ +bar ]
        ;
}

int main() {
    for (std::string const input : {
            "bar",
            "(bar)",
            "(barbar)",
            })
    {
        auto f = input.begin(), l = input.end();

        foo_node data;
        bool ok = x3::parse(f, l, rules::foo_node, data);

        if (ok) {
            std::cout << "Parse success: " << data.value.size() << " elements\n";
        } else {
            std::cout << "Parse failed\n";
        }

        if (f != l)
            std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
    }
}
Parse success: 1 elements
Parse success: 1 elements
Parse success: 2 elements