Parsing comma-separated list of ranges and numbers with semantic actions

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 11:03:49

The FAQ of this that semantic actions suppress automatic attribute propagation. The assumption being that the semantic action will take care of it instead.

In general there are two approaches:

  • either use operator%= instead of operator= to assign the definition to the rule

  • or use the third (optional) template argument to the rule<> template, which can be specified as true to force automatic propagation semantics.


Simplified sample

Here, I simplify mostly by removing the semantic action inside the range rule itself. Now, we can drop the ast::range type altogether. No more fusion adaptation.

Instead we use the "naturally" synthesized attribute of numer>>'-'>>number which is a fusion sequence of ints (fusion::deque<int, int> in this case).

Now, all that's left to make it work, is to make sure the branches of | yield compatible types. A simple repeat(1)[] fixes that.

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});
    }
}

Prints:

OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12, 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!