提升精神气规则的属性问题

时间:2011-07-08 16:49:53

标签: c++ boost-spirit-qi

我有以下代码无法编译,但是我不知道问题出在哪里:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/foreach.hpp>

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

void print (std::string& s)
{
    std::cout << "Get string [" << s << ']' << std::endl;
}

namespace client
{
    namespace fusion = boost::fusion;
    namespace phoenix = boost::phoenix;
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    template <typename Iterator>
    struct my_grammar
      : qi::grammar<Iterator, std::string(), ascii::space_type>
    {
        my_grammar()
          : my_grammar::base_type(start)
        {
        using qi::lit;
        using qi::int_;
        using qi::lexeme;
        using ascii::char_;
        using ascii::string;
        using ascii::space_type;
        using namespace qi::labels;


        // Here you can see the rule oct_char with attribute type as char
        // +(char) would be vector<char> which is compactable with string type
        // Why the compiler still told me the type of the attribute is not right?
        start %= lexeme[+(oct_char)] [&print]
            ;

        oct_char =
                   lexeme["//"        
                           >>  char_('0','3') [ _a = ( _1 - '0' ) ]
                     ||     char_('0','7') [ _a = _a << 3 + ( _1 - '0' ) ]
                ||     char_('0','7') [ _a = _a << 3 + ( _1 - '0' ) ]]
                 [ _val = _a ]
                ;
        }
    qi::rule<Iterator, std::string(), ascii::space_type> start;
        qi::rule<Iterator, char(), qi::locals<char>, ascii::space_type> oct_char;

    };
}

int main(int argc, char **argv)
{

    typedef client::my_grammar<std::string::const_iterator> Grammer;
    Grammer grm;

    std::string str;


using boost::spirit::ascii::space;

while (getline(std::cin, str))
{
    if (str.empty() || str[0] == 'q' || str[0] == 'Q')
        break;

    if (phrase_parse(str.begin(), str.end(), grm, space))
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing succeeded\n";
        std::cout << "got: " << str << std::endl;
        std::cout << "\n-------------------------\n";
    }
    else
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing failed\n";
        std::cout << "-------------------------\n";
    }
}

std::cout << "Bye... :-) \n\n";
return 0;


}

以下是错误消息:

Error   2   error C2664: 'void (std::basic_string<_Elem,_Traits,_Ax> )' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::basic_string<_Elem,_Traits,_Ax> ' d:\code\boost_1_46_1\boost\spirit\home\support\action_dispatch.hpp  70

有人可以提出一些建议吗? 感谢

1 个答案:

答案 0 :(得分:2)

这已在其他地方详细讨论过(例如here)。如果您更改打印功能以使用vector<char>它将起作用:

void print (std::vector<char> const& s)
{
    std::cout << "Get string [" 
              << std::string(s.begin(), s.end()) << ']' 
              << std::endl;
}

但是代码中还有一个错误。规则oct_char不需要在lexeme[]内使用的船长:

qi::rule<Iterator, char(), qi::locals<char> > oct_char;
相关问题