4

假设我有一个std::string属性,但为了便于解析,我想使用qi::int_or qi::double_

有没有一种简单的方法可以将转换作为语义操作

我试过这样的事情:

 std::stringstream ss; 
 my_int_as_str = qi::int_ [ ref(ss)<<_1; _val=ss.str() ];

但这甚至不会编译。

编辑 - 尝试下面的 sehe 答案

#include <iostream>
#include <string>
#include <vector>

#include <boost/spirit/include/qi.hpp>

namespace qi=boost::spirit::qi;
namespace phx=boost::phoenix;

int main(int argc, char* argv[])
{
    std::string test="123";
    std::string result;

    // 1. qi::raw[ qi::int_ ]       works
    // 2. qi::lexeme[ qi::int_ ]    doesn't
    // 3. qi::as_string[ qi::int_ ] doesn't
    qi::rule<std::string::const_iterator, std::string()> my_int_as_str = 
                   qi::raw[ qi::int_ ];
    parse( test.cbegin(), test.cend(), my_int_as_str, result );

    std::cout << result << std::endl;

    // -------------------------------------------------------------------------

    std::string test_vector="456 789";
    std::vector<std::string> result_vector;

    // 4. qi::raw[ qi::int_ ]       won't compile
    // 5. qi::lexeme[ qi::int_ ]    won't compile
    // 6. qi::as_string[ qi::int_ ] doesn't
    qi::rule<std::string::const_iterator,std::vector<std::string>(),qi::space_type> 
            my_int_as_str_vector = qi::lexeme[ qi::int_ ];

    phrase_parse(test_vector.cbegin(),test_vector.cend(),
            my_int_as_str_vector,qi::space,result_vector);

    for(auto& string: result_vector)
        std::cout << string << std::endl;

    return 0;
}
4

2 回答 2

6

Builtin way:

qi::rule<Iterator, std::string()> my_int_as_str = 
       qi::as_string [ qi::int_ ];

For this particular simple case, qi::raw or qi::lexeme would have worked equally well since they both expose source-iterator pairs, which have builtin assignment-to-attribute for std::string

Elaborating:

my_int_as_str = qi::lexeme [ qi::int_ ]; // equivalent
my_int_as_str = qi::raw [ qi::int_ ];    // idem
于 2012-11-22T23:01:43.023 回答
6

您可以attr_cast通过专门transform_attribute针对您的类型来使用。

#include <iostream>
#include <string>
#include <vector>

#include <boost/spirit/include/qi.hpp>

namespace qi=boost::spirit::qi;
namespace phx=boost::phoenix;


namespace boost { namespace spirit { namespace traits
{

    template <>
    struct transform_attribute<std::string, int, qi::domain>
    {
        typedef int type;
        static int pre(std::string& d) { return 0; }//not useful in this case but required to avoid compiler errors
        static void post(std::string& val, int const& attr) //`val` is the "returned" string, `attr` is what int_ parses
        {
            std::stringstream ss;
            ss << attr;
            val= ss.str();
        }
        static void fail(std::string&) {}
    };
}}}

int main(int argc, char* argv[])
{
    std::string test="123";
    std::string test_vector="456 789";

    qi::rule<std::string::const_iterator,std::string()> my_int_as_str = qi::attr_cast(qi::int_);
    qi::rule<std::string::const_iterator,std::vector<std::string>(),qi::space_type> my_int_as_str_vector= *qi::attr_cast(qi::int_);

    std::string result;
    std::vector<std::string> result_vector;

    parse(test.cbegin(),test.cend(),my_int_as_str,result);
    phrase_parse(test_vector.cbegin(),test_vector.cend(),my_int_as_str_vector,qi::space,result_vector);


    std::cout << result << std::endl;
    for(auto& string: result_vector)
        std::cout << string << std::endl;


    return 0;
}

如果您确实需要/想要使用语义操作,最简单的替代方法是定义一个以 int 作为参数并返回字符串的函数(这比attr_cast替代方法更简单,但也更慢,几乎是我非常简单的基准测试的两倍):

std::string semantic_transform(int i)
{
    std::stringstream ss;
    ss<<i;
    return ss.str();
}
...
std::string string_semantic;
qi::parse(test.cbegin(),test.cend(),qi::int_[&semantic_transform],string_semantic);
std::cout << string_semantic << std::endl;
于 2012-11-22T16:18:42.353 回答