c++ - 无法编译用 Boost::Spirit 库编写的简单解析器

标签 c++ boost

以下是我无法在 Boost::spirit 库中编译的最小程序。

    #include <iostream>
    #include <string>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/lexical_cast.hpp>
    using namespace boost::spirit;

    template <typename Iterator>
    struct z3_exp_grammars : qi::grammar<Iterator, std::string(), ascii::space_type>
    {
        z3_exp_grammars() : z3_exp_grammars::base_type(text)
        {


            text = qi::int_[qi::_val = boost::lexical_cast<std::string>(qi::_1)];
         }

        qi::rule<Iterator, std::string(), ascii::space_type> text;

    };


    int main(){

        std::string test("3");
            std::string result;
            std::string::const_iterator beg = test.begin();
            std::string::const_iterator end = test.end();
            typedef z3_exp_grammars<std::string::const_iterator> z3_exp_grammars;
            z3_exp_grammars par;
            qi::phrase_parse(beg,end,par,result);
            std::cout<<"Result is "<<result<<std::endl;
}

我希望在变量结果中看到字符串 3,但代码未编译。如果有人能在代码中发现我的错误,而不是查看错误日志(由于模板而非常危险),那就太好了。感谢您的帮助。

用编译 T.C 给出的相同代码后得到的编译错误更新问题

Test.cpp:9:11: error: expected nested-name-specifier before ‘result_type’
Test.cpp:9:11: error: using-declaration for non-member at class scope
Test.cpp:9:23: error: expected ‘;’ before ‘=’ token
Test.cpp:9:23: error: expected unqualified-id before ‘=’ token
In file included from /home/jaganmohini/Downloads/boost_1_58_0/boost/proto/proto_fwd.hpp:28:0,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/phoenix/core/limits.hpp:26,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/include/phoenix_limits.hpp:11,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/support/meta_compiler.hpp:16,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi/action/action.hpp:14,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi/action.hpp:14,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi.hpp:14,
                 from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/include/qi.hpp:16,
                 from Test.cpp:3:
/home/jaganmohini/Downloads/boost_1_58_0/boost/utility/result_of.hpp: In instantiation of ‘boost::detail::result_of_nested_result<const to_string_, const to_string_(int&)>’:
/home/jaganmohini/Downloads/boost_1_58_0/boost/utility/result_of.hpp:197:1:   instantiated from ‘boost::detail::tr1_result_of_impl<const to_string_, const to_string_(int&), false>’

最佳答案

两个独立的问题:

  1. phrase_parse 期望 Skipper 作为其第四个参数。因此,您应该使用 qi::phrase_parse(beg, end, par, ascii::space, result);

  2. 您不能在这样的凤凰占位符上使用 boost::lexical_cast。它被热切地评估。你需要 create a lazy function :

    struct to_string_ {
        using result_type = std::string;
        template<class T>
        std::string operator()(const T& arg) const {
            return boost::lexical_cast<std::string>(arg); 
        }
    };
    
    boost::phoenix::function<to_string_> to_string;
    

    text = qi::int_[qi::_val = to_string(qi::_1)];
    

Demo .

关于c++ - 无法编译用 Boost::Spirit 库编写的简单解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30798446/

相关文章:

c++ - 如果我声明a = 5然后返回&a,则在哪里分配内存?

c++ - 用 C++ 编写和检查自己的概念

c++ - 在成员dynamic_bitset上使用boost::from_block_range时出错,但在本地dynamic_bitset上不这样做

c++ - LNK2038 : mismatch detected for 'boost_log_abi' : value 'v2s_mt_nt5' doesn't match value 'v2s_mt_nt6'

c++ - labeled_graph 中的权重

c++ - 具有内存顺序的原子负载存储

android - 在 Android 上安装 GStreamer 插件

c++ - 在命名空间中转发声明类抛出编译器错误

c++ - 如果 `atomic<T>` 是无锁的并且大小与 `T` 相同,内存布局是否相同?

c++ - 在 XCode 4.1 中禁用有关 Boost header 的警告