这是一个后续问题,源自之前的问题。
我可以从我的语法中解析出字符串向量,但无法似乎解析成指向字符串的共享指针的向量;即
我的编译头文件:
文本被正确解析,但生成的向量长度为0,而应该是长度为3。不知何故,“std :: shared_ptr<string>”未被推入从规则“genericvargp_”产生的向量的末尾。我尝试了许多方法,包括阅读测试解析的所有调试信息,并放置规则定义的“%=”符号,这应该用于那些没有分配“_val”的语义动作的规则,除非我弄错了。我也整夜整天地使用“phx::bind”手动推送到“_val”的末尾,但是一无所获。我还进一步验证了sehe在另一个答案中提供的“make_shared_”确实对std :: shared_ptr懒惰。另外,我还努力使“unencountered_symbol_”的结果添加到“encountered_variables”,以便强制执行变量名称的唯一性...问题似乎在于将“new_variable_”规则的结果传播到“genericvargp_”规则中所需的共享指针向量上。
我可以从我的语法中解析出字符串向量,但无法似乎解析成指向字符串的共享指针的向量;即
std::vector<std::shared_ptr<std::string> >
,需要一点帮助。我的编译头文件:
#define BOOST_SPIRIT_USE_PHOENIX_V3 1
#include <boost/spirit/include/qi_core.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/phoenix/bind/bind_member_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
// this solution for lazy make shared comes from the SO forum, user sehe.
// https://dev59.com/E3zaa4cB1Zd3GeqPKwkv
// post found using google search terms `phoenix construct shared_ptr`
// changed from boost::shared_ptr to std::shared_ptr
namespace {
template <typename T>
struct make_shared_f
{
template <typename... A> struct result
{ typedef std::shared_ptr<T> type; };
template <typename... A>
typename result<A...>::type operator()(A&&... a) const {
return std::make_shared<T>(std::forward<A>(a)...);
}
};
template <typename T>
using make_shared_ = boost::phoenix::function<make_shared_f<T> >;
}
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template<typename Iterator, typename Skipper = ascii::space_type>
struct SystemParser : qi::grammar<Iterator, std::vector<std::shared_ptr<std::string> >(), Skipper>
{
SystemParser() : SystemParser::base_type(variable_group_)
{
namespace phx = boost::phoenix;
using qi::_1;
using qi::_val;
using qi::eps;
using qi::lit;
var_counter = 0;
declarative_symbols.add("variable_group",0);
variable_group_ = "variable_group" > genericvargp_ > ';';
genericvargp_ = new_variable_ % ','; //
new_variable_ = unencountered_symbol_ [_val = make_shared_<std::string>() (_1)];
unencountered_symbol_ = valid_variable_name_ - ( encountered_variables | declarative_symbols );
valid_variable_name_ = +qi::alpha >> *(qi::alnum | qi::char_("[]_") );
// debug(variable_group_); debug(unencountered_symbol_); debug(new_variable_); debug(genericvargp_);
// BOOST_SPIRIT_DEBUG_NODES((variable_group_) (valid_variable_name_) (unencountered_symbol_) (new_variable_) (genericvargp_))
}
// rule declarations. these are member variables for the parser.
qi::rule<Iterator, std::vector<std::shared_ptr<std::string> >(), Skipper > variable_group_;
qi::rule<Iterator, std::vector<std::shared_ptr<std::string> >(), Skipper > genericvargp_;
qi::rule<Iterator, std::shared_ptr<std::string()> > new_variable_;
qi::rule<Iterator, std::string()> unencountered_symbol_;
qi::rule<Iterator, std::string()> valid_variable_name_;
unsigned var_counter;
qi::symbols<char,int> encountered_variables;
qi::symbols<char,int> declarative_symbols;
};
使用驱动程序代码:
int main(int argc, char** argv)
{
std::vector<std::shared_ptr<std::string> > V;
std::string str = "variable_group x, y, z; ";
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
SystemParser<std::string::const_iterator> S;
bool s = phrase_parse(iter, end, S, boost::spirit::ascii::space, V);
if (s)
{
std::cout << "Parse succeeded: " << V.size() << " variables\n";
for (auto& s : V)
std::cout << " - '" << s << "'\n";
}
else
std::cout << "Parse failed\n";
if (iter!=end)
std::cout << "Remaining unparsed: '" << std::string(iter, end) << "'\n";
return 0;
}
文本被正确解析,但生成的向量长度为0,而应该是长度为3。不知何故,“std :: shared_ptr<string>”未被推入从规则“genericvargp_”产生的向量的末尾。我尝试了许多方法,包括阅读测试解析的所有调试信息,并放置规则定义的“%=”符号,这应该用于那些没有分配“_val”的语义动作的规则,除非我弄错了。我也整夜整天地使用“phx::bind”手动推送到“_val”的末尾,但是一无所获。我还进一步验证了sehe在另一个答案中提供的“make_shared_”确实对std :: shared_ptr懒惰。另外,我还努力使“unencountered_symbol_”的结果添加到“encountered_variables”,以便强制执行变量名称的唯一性...问题似乎在于将“new_variable_”规则的结果传播到“genericvargp_”规则中所需的共享指针向量上。