Boost.Spirit x3学习笔记

来源:互联网 发布:软件开发质量考核指标 编辑:程序博客网 时间:2024/05/15 08:27

为了能够在Visual Studio 2015 RC上运行,需要做如下修改

1、修改boost/spirit/home/x3/nonterminal/detail/rule.hpp中的has_on_error和has_on_success的定义为:

template <typename ID, typename Iterator, typename Context>struct do_has_on_error{template<typename U> static auto test(int) -> decltype(sizeof(declval<U>().on_error(  std::declval<Iterator&>(), std::declval<Iterator>(), std::declval<expectation_failure<Iterator>>(), std::declval<Context>())) >= 0, mpl::true_());template<typename> static mpl::false_ test(...);using type = decltype(test<ID>(0));};template <typename ID, typename Iterator, typename Context>using has_on_error = typename do_has_on_error<ID, Iterator, Context>::type;template <typename ID, typename Iterator, typename Attribute, typename Context>struct do_has_on_success {template<typename U> static auto test(int) -> decltype(sizeof(declval<U>().on_success(  std::declval<Iterator&>(), std::declval<Iterator>(), std::declval<Attribute&>(), std::declval<Context>())) >= 0, mpl::true_());template<typename> static mpl::false_ test(...);using type = decltype(test<ID>(0));};template<typename ID, typename Iterator, typename Attribute, typename Context>using has_on_success = typename do_has_on_success<ID, Iterator, Attribute, Context>::type;

2、修改boost/spirit/home/x3/nonterminal/rule.hpp中BOOST_SPIRIT_DEFINE_的定义为:

#define BOOST_SPIRIT_DEFINE_(r, data, rule_name)                                \    using BOOST_PP_CAT(rule_name, _t) = decltype(rule_name);                    \    template <typename Iterator, typename Context, typename Attribute>          \    inline bool parse_rule(                                                     \        BOOST_PP_CAT(rule_name, _t) rule_                                       \      , Iterator& first, Iterator const& last                                   \      , Context const& context, Attribute& attr)                                \    {                                                                           \        using boost::spirit::x3::unused;                                        \        static auto const def_ = (rule_name = BOOST_PP_CAT(rule_name, _def));   \        return def_.parse(first, last, context, unused, attr);                  \    }                                                                           \    /***/

接口的区别

phrase_parse 自动跳过空格

parse 不自动跳过空格

定义Rule

1、提供Rule的名称

rule<ID, Attribute> const r = "some-name";
注:ID不需要定义。

2、提供Rule的定义,

auto const r_def = double_ >> *(',' >> double_);
注:必须是“名称_def”的形式。

3、定义parse_rule函数

BOOST_SPIRIT_DEFINE(r);
4、使用r

Spirit x3设计者提供的示例

/*=============================================================================Copyright (c) 2001-2015 Joel de GuzmanCopyright (c) 2015 Ahmed CharlesDistributed under the Boost Software License, Version 1.0. (See accompanyingfile LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)=============================================================================*////////////////////////////////////////////////////////////////////////////////////  A Roman Numerals Parser (demonstrating the symbol table). This is//  discussed in the "Symbols" chapter in the Spirit User's Guide.////  [ JDG August 22, 2002 ] spirit1//  [ JDG March 13, 2007 ]  spirit2//  [ JDG May 13, 2015 ]    spirit X3/////////////////////////////////////////////////////////////////////////////////#include <boost/config/warning_disable.hpp>#include <boost/spirit/home/x3.hpp>#include <iostream>#include <string>namespace client{namespace x3 = boost::spirit::x3;namespace ascii = boost::spirit::x3::ascii;/////////////////////////////////////////////////////////////////////////////////  Parse roman hundreds (100..900) numerals using the symbol table.//  Notice that the data associated with each slot is the parser's attribute//  (which is passed to attached semantic actions).///////////////////////////////////////////////////////////////////////////////struct hundreds_ : x3::symbols<unsigned>{hundreds_(){add("C", 100)("CC", 200)("CCC", 300)("CD", 400)("D", 500)("DC", 600)("DCC", 700)("DCCC", 800)("CM", 900);}} hundreds;/////////////////////////////////////////////////////////////////////////////////  Parse roman tens (10..90) numerals using the symbol table.///////////////////////////////////////////////////////////////////////////////struct tens_ : x3::symbols<unsigned>{tens_(){add("X", 10)("XX", 20)("XXX", 30)("XL", 40)("L", 50)("LX", 60)("LXX", 70)("LXXX", 80)("XC", 90);}} tens;/////////////////////////////////////////////////////////////////////////////////  Parse roman ones (1..9) numerals using the symbol table.///////////////////////////////////////////////////////////////////////////////struct ones_ : x3::symbols<unsigned>{ones_(){add("I", 1)("II", 2)("III", 3)("IV", 4)("V", 5)("VI", 6)("VII", 7)("VIII", 8)("IX", 9);}} ones;/////////////////////////////////////////////////////////////////////////////////  roman (numerals) grammar////      Note the use of the || operator. The expression//      a || b reads match a or b and in sequence. Try//      defining the roman numerals grammar in YACC or//      PCCTS. Spirit rules! :-)///////////////////////////////////////////////////////////////////////////////namespace parser{using x3::eps;using x3::lit;using x3::_val;using x3::_attr;using ascii::char_;auto set_zero = [](auto& ctx) { _val(ctx) = 0; };auto add1000 = [](auto& ctx) { _val(ctx) += 1000; };auto add = [](auto& ctx) { _val(ctx) += _attr(ctx); };x3::rule<class roman, unsigned> const roman = "roman";auto const roman_def =eps[set_zero]>>(-(+lit('M')[add1000])>> -hundreds[add]>> -tens[add]>> -ones[add]);BOOST_SPIRIT_DEFINE(roman);}}/////////////////////////////////////////////////////////////////////////////////  Main program///////////////////////////////////////////////////////////////////////////////intmain(){std::cout << "/////////////////////////////////////////////////////////\n\n";std::cout << "\t\tRoman Numerals Parser\n\n";std::cout << "/////////////////////////////////////////////////////////\n\n";std::cout << "Type a Roman Numeral ...or [q or Q] to quit\n\n";typedef std::string::const_iterator iterator_type;using client::parser::roman; // Our parserstd::string str;unsigned result;while (std::getline(std::cin, str)){if (str.empty() || str[0] == 'q' || str[0] == 'Q')break;std::string::const_iterator iter = str.begin();std::string::const_iterator end = str.end();bool r = parse(iter, end, roman, result);if (r && iter == end){std::cout << "-------------------------\n";std::cout << "Parsing succeeded\n";std::cout << "result = " << result << std::endl;std::cout << "-------------------------\n";}else{std::string rest(iter, end);std::cout << "-------------------------\n";std::cout << "Parsing failed\n";std::cout << "stopped at: \": " << rest << "\"\n";std::cout << "-------------------------\n";}}std::cout << "Bye... :-) \n\n";return 0;}
运行截图


0 0