灵气第一解析器

Spirit Qi First Parser

我在这里搞砸了什么?我得到 'start': undeclared identifier,但我非常认真地学习教程,所以我不确定我在哪里打错了字,或者我做错了什么。有什么提示吗?你们看到的都是一样的吧?

#include <iostream>
#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_object.hpp>
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi_no_skip.hpp>
#include <boost/spirit/include/phoenix.hpp>


namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

using qi::lit;
using qi::int_;
using qi::double_;
using ascii::char_;
using boost::spirit::qi::phrase_parse;
using boost::spirit::qi::no_skip;
using qi::eoi;


struct LETTER
{
    char hi;
//  int fourtytwo;
//  char mom;
};

BOOST_FUSION_ADAPT_STRUCT(
    LETTER,
    (char, hi)
//  (int, fourtytwo)
//  (char, mom)
)


template <typename Iterator>
struct LETTERParser : qi::grammar<Iterator, LETTER(), ascii::space_type>
{
    LETTERParser(): LETTERParser::base_type(start)
    {
        start %= lit("LETTER") >> char_;
//          >> char_ 
//          >> int_ 
//          >> char_ 
//          >> eoi
//          ;
    }
};

const std::string wat("Z");
int main()
{
    LETTERParser<std::string::const_iterator> f;
    LETTER example;
    phrase_parse(wat.begin(), wat.end(), f, no_skip, example);
    return 0;
}

有很多问题,其中一个不明显

  1. no_skip 在哪里?为什么要将它传递给需要 ascii::space_type 的语法?
  2. start 规则在哪里声明?
  3. 不要污染全局命名空间 - 它会在通用代码中产生难题
  4. 处理错误
  5. 语法以强制字符序列开头,与输入不匹配
  6. 不明显的一个:单元素结构以不幸的方式干扰 Spirit/Fusion 土地。

简化:

修复上述问题并对融合适应进行现代化 (c++11):

live On Coliru

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>

namespace qi = boost::spirit::qi;

struct LETTER {
    char hi;
    int fourtytwo;
    char mom;
};

BOOST_FUSION_ADAPT_STRUCT(LETTER, hi, fourtytwo, mom)

template <typename Iterator> struct LETTERParser : qi::grammar<Iterator, LETTER(), qi::ascii::space_type> {
    LETTERParser() : LETTERParser::base_type(start) {
        using qi::char_;
        using qi::int_;
        start = "LETTER" >> char_ >> int_ >> char_;
    }
  private:
    qi::rule<Iterator, LETTER(), qi::ascii::space_type> start;
};

int main() {
    const std::string input("LETTER Z 42m");
    using It = std::string::const_iterator;

    LETTERParser<It> parser;
    LETTER example;

    It f = input.begin(), l = input.end();

    if (phrase_parse(f, l, parser, qi::ascii::space, example)) {
        std::cout << "parsed: " << boost::fusion::as_vector(example) << "\n";
    } else {
        std::cout << "couldn't parse '" << input << "'\n";
    }

    if (f != l)
        std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}

版画

parsed: (Z 42 m)

单元素:

你在,幸运的是它没有咬到你的情况:

Live On Coliru

版画

parsed: (Z)
Remaining unparsed input: '42m'

符合预期。如果将来发生,请参考此处,例如Size of struct with a single element


奖金

考虑封装船长的选择。调用者可能永远无法覆盖它 Live On Coliru - see also Boost spirit skipper issues