5

Boost.Spirit X3を使用して、範囲と個々の数字 (例: 1-4、6、7、9-12) のカンマ区切りのリストを単一の に解析したいと考えていますstd::vector<int>。これが私が思いついたものです:

namespace ast {
    struct range 
    {
        int first_, last_;    
    };    

    using expr = std::vector<int>;    
}

namespace parser {        
    template<typename T>
    auto as_rule = [](auto p) { return x3::rule<struct _, T>{} = x3::as_parser(p); };

    auto const push = [](auto& ctx) { 
        x3::_val(ctx).push_back(x3::_attr(ctx)); 
    };  

    auto const expand = [](auto& ctx) { 
        for (auto i = x3::_attr(ctx).first_; i <= x3::_attr(ctx).last_; ++i) 
            x3::_val(ctx).push_back(i);  
    }; 

    auto const number = x3::uint_;
    auto const range  = as_rule<ast::range> (number >> '-' >> number                   ); 
    auto const expr   = as_rule<ast::expr>  ( -(range [expand] | number [push] ) % ',' );
} 

入力を考えると

    "1,2,3,4,6,7,9,10,11,12",   // individually enumerated
    "1-4,6-7,9-12",             // short-hand: using three ranges

Live On Coliruこれは ( )として正常に解析されます。

OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 

質問expand: セマンティック アクションをパーツに適用する必要があることは理解していると思いますrangeが、なぜパーツにもセマンティック アクションを適用する必要があるpushnumberですか? それがなければ (つまり、 の単純な( -(range [expand] | number) % ',')ルールでexprは、個々の数値は AST ( ) に伝播されませんLive On Coliru:

OK! Parsed: 
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 

おまけの質問: これを行うにはセマンティック アクションが必要ですか? Spirit X3 のドキュメントは彼らを思いとどまらせているようです。

4

1 に答える 1

3

セマンティック アクションが属性の自動伝播を抑制するという FAQ です。セマンティックアクションが代わりにそれを処理するという前提があります。

一般に、次の 2 つのアプローチがあります。

  • operator%=代わりに使用operator=して、定義をルールに割り当てます

  • または、テンプレートに 3 番目 (オプション) のテンプレート引数を使用します。これは、自動伝播セマンティクスを強制rule<>するように指定できます。true


簡易サンプル

ここでは、範囲ルール自体の中のセマンティック アクションを削除することで、ほとんど単純化しています。ast::rangeこれで、タイプを完全に削除できます。これ以上の融合適応はありません。

numer>>'-'>>number代わりに、int の融合シーケンスである「自然に」合成された属性を使用します (fusion::deque<int, int>この場合)。

さて、それを機能させるために残っているのは、|互換性のある型のブランチが生成されることを確認することだけです。それを簡単repeat(1)[]に修正します。

Live On Coliru

#include <boost/spirit/home/x3.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

namespace ast {
    using expr = std::vector<int>;    

    struct printer {
        std::ostream& out;

        auto operator()(expr const& e) const {
            std::copy(std::begin(e), std::end(e), std::ostream_iterator<expr::value_type>(out, ", "));;
        }
    };    
}

namespace parser {        
    auto const expand = [](auto& ctx) { 
        using boost::fusion::at_c;

        for (auto i = at_c<0>(_attr(ctx)); i <= at_c<1>(_attr(ctx)); ++i) 
            x3::_val(ctx).push_back(i);  
    }; 

    auto const number = x3::uint_;
    auto const range  = x3::rule<struct _r, ast::expr> {} = (number >> '-' >> number) [expand]; 
    auto const expr   = x3::rule<struct _e, ast::expr> {} = -(range | x3::repeat(1)[number]  ) % ',';
} 

template<class Phrase, class Grammar, class Skipper, class AST, class Printer>
auto test(Phrase const& phrase, Grammar const& grammar, Skipper const& skipper, AST& data, Printer const& print)
{
    auto first = phrase.begin();
    auto last = phrase.end();
    auto& out = print.out;

    auto const ok = phrase_parse(first, last, grammar, skipper, data);
    if (ok) {
        out << "OK! Parsed: "; print(data); out << "\n";
    } else {
        out << "Parse failed:\n";
        out << "\t on input: " << phrase << "\n";
    }
    if (first != last)
        out << "\t Remaining unparsed: '" << std::string(first, last) << '\n';    
}

int main() {
    std::string numeric_tests[] =
    {
        "1,2,3,4,6,7,9,10,11,12",   // individually enumerated
        "1-4,6-7,9-12",             // short-hand: using three ranges
    };

    for (auto const& t : numeric_tests) {
        ast::expr numeric_data;
        test(t, parser::expr, x3::space, numeric_data, ast::printer{std::cout});
    }
}

版画:

OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
于 2016-01-04T21:00:54.770 に答える