Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 提升精神选择无与伦比的结果_C++_Boost Spirit_Boost Spirit Qi - Fatal编程技术网

C++ 提升精神选择无与伦比的结果

C++ 提升精神选择无与伦比的结果,c++,boost-spirit,boost-spirit-qi,C++,Boost Spirit,Boost Spirit Qi,我有一个以下格式的文件 metal 1 1.2 2.2 wire 1.1 2.3 metal 2 3.2 12.2 ... 这是一种非常简单的格式。“金属”和“电线”是关键词。“金属”后面跟着1个uint和2个double,而“电线”后面跟着2个double。 我尝试使用Boost::Qi来解析它,但结果非常奇怪,我不知道为什么 #include <iostream> #include <string> #include <boost/spirit/include

我有一个以下格式的文件

metal 1 1.2 2.2
wire 1.1 2.3
metal 2 3.2 12.2
...
这是一种非常简单的格式。“金属”和“电线”是关键词。“金属”后面跟着1个uint和2个double,而“电线”后面跟着2个double。 我尝试使用Boost::Qi来解析它,但结果非常奇怪,我不知道为什么

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
using std::cout;
using std::endl;
using std::string;

using namespace boost::spirit;

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

    // grammar 
    template <typename Iterator>
    struct TimingLibGrammar : 
        qi::grammar<Iterator, ascii::space_type>
    {
        qi::rule<Iterator, ascii::space_type> expression;

        TimingLibGrammar() : TimingLibGrammar::base_type(expression)
        {
            using qi::uint_;
            using qi::int_;
            using qi::double_;
            using qi::char_;
            using qi::_1;
            using qi::_2;
            using qi::_3;
            using qi::_val;
            using qi::lexeme;
            using qi::lit;

            expression = 
                +(
                    ((
                      "metal" 
                    >> uint_
                    >> double_
                    >> double_)[cout << "metal" << " "<< _1 << " " << _2 << " " << _3 << endl])
                        | 
                    ((
                      "wire"
                    >> double_
                    >> double_)[cout << "wire" << " "<< _1 << " " << _2 << endl])
                );

        }
    };
}


int main()
{
    using boost::spirit::ascii::space;
    using namespace client;
    string str = "metal 3 1.0 2.0";
    TimingLibGrammar<string::const_iterator> tlg;
    string::const_iterator iter = str.begin();
    string::const_iterator end = str.end();

    client::qi::phrase_parse(iter, end, tlg, space);

            return 0;
}
解析器给了我如下结果:

wire metal 3 1 2
这个结果是不正确的。它应该输出“金属3112”,但我不知道这个“电线”从哪里来。我还尝试遵循boostlibs中的几个示例代码。但它仍然没能做到正确。 代码是用g++4.7.2编译的,带有-std=c++11标志


任何建议都会有帮助。我是个精神振奋的新手,所以我希望能学到一些东西。提前感谢。

罪魁祸首是这样的台词:

cout << "metal" << " "<< _1 << " " << _2 << " " << _3 << endl

cout << "wire" << " "<< _1 << " " << _2 << endl

顺便说一句,如果我想让“metal”不区分大小写,你知道怎么做吗?在flex&bison,我知道就像[Mm][Ee][Tt][Aa][Ll],我只是按照你的建议做了同样的事情,得到了我想要的。非常感谢。
cout << "metal" << " "<< _1 << " " << _2 << " " << _3 << endl

cout << "wire" << " "<< _1 << " " << _2 << endl
cout << phoenix::val("metal") << " "<< _1 << " " << _2 << " " << _3 << endl

cout << phoenix::val("wire") << " "<< _1 << " " << _2 << endl