创建并写入动态矢量

Create and write to vector on the fly

本文关键字:动态 创建      更新时间:2023-10-16

我想在一个精神规则中创建向量并向其附加值(如果有的话)。有可能吗?我尝试了下面这样的方法,但没有成功。请阅读代码注释了解详细信息。谢谢

typedef std::vector<double> number_array;
typedef std::vector<std::string> string_array;
typedef boost::variant<number_array, string_array> node 
template<typename Iterator>
struct parser 
      : qi::grammar<Iterator, node(), ascii::space_type> {
    parser(parser_impl* p) 
            : parser::base_type(expr_, ""), 
              error_handler(ErrorHandler(p)) {
        // Here I want to create vector on the fly 
        // and append values to newly created vector.
        // but this doesn't compile, specifically phoenix::push_back(...)
        number_array_ = qi::lit('n[')[qi::_val = construct<number_array>()] >> 
                       -(qi::double_ % ',')[phoenix::push_back(phoenix::ref(qi::_val), qi::_1)] >> ']';
        // this doesn't compile too
        string_array_ = qi::lit('s[')[qi::_val = construct<string_array>()] >> 
                       -(quoted_string % ',')[phoenix::push_back(phoenix::ref(qi::_val), qi::_1)] >> ']';                       
        quoted_string %= "'" > qi::lexeme[*(qi::char_ - "'")] > "'";
        expr_ = number_array_[qi::_val =  qi::_1] | string_array_[[qi::_val =  qi::_1]];
    }
    qi::rule<Iterator, number_array(), ascii::space_type> number_array_;
    qi::rule<Iterator, string_array(), ascii::space_type> string_array_;
    qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
    qi::rule<Iterator, node(), ascii::space_type> expr_;    
};

这里最重要的一点是,我认为您可以不使用所有的语义操作。

它们只执行默认属性规则已经执行的操作(基本上,标量属性为_val = _1,conainer属性为insert(_val, end(_val), _1))。

这意味着你可以将整个shebang写为

    number_array_ = "n[" >> -(qi::double_ % ',')   >> ']';
    string_array_ = "s[" >> -(quoted_string % ',') >> ']';
    quoted_string = "'" > qi::lexeme[*(qi::char_ - "'")] > "'";
    expr_         = number_array_ | string_array_;

这会奏效的。注意,我修复了多字节文字'n[''s[n'

另见Boost Spirit:";语义行为是邪恶的"?