Is it possible to re-use boost::spirit::qi grammar in another grammar definition?

前端 未结 1 998
無奈伤痛
無奈伤痛 2021-02-07 01:51

Is it possible to reuse boost::spirit:qi grammar in another grammar (as a rule for example)?

For example if I define a grammar to parse lin

相关标签:
1条回答
  • 2021-02-07 02:20

    Of course you can. In your case, just put address_grammar<iter> address_; in your code.

    Let me show you another example. You can find a compilable code here: http://ideone.com/GW4jO (see also below)

    AFAIK, unlike qi::grammar, qi::rule is hard to reuse.


    Full Sample

    #include <string>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/fusion/include/adapt_struct.hpp>
    
    struct Date {
        int year, month, day;
    };
    struct Time {
        int hour, minute, second;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(
        Date,
        (int, year)
        (int, month)
        (int, day)
    )
    
    BOOST_FUSION_ADAPT_STRUCT(
        Time,
        (int, hour)
        (int, minute)
        (int, second)
    )
    
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;
    typedef std::string::const_iterator Iterator;
    
    class DateParser:
        public qi::grammar < Iterator, Date() > {
            qi::rule < Iterator, Date() > main;
        public:
            DateParser(): base_type(main) {
                main %= qi::int_ >> '-' >> // Year
                        qi::int_ >> '-' >> // Month
                        qi::int_;          // Day
            }
    };
    
    class TimeParser:
        public qi::grammar < Iterator, Time() > {
            qi::rule < Iterator, Time() > main;
        public:
            TimeParser(): base_type(main) {
                main %= qi::int_ >> ':' >> // Hour
                        qi::int_ >> ':' >> // Minute
                        qi::int_;          // Second
            }
    };
    
    class DateTimeParser:
        public qi::grammar < Iterator, boost::variant<Date, Time>() > {
            qi::rule < Iterator, boost::variant<Date, Time>()> main;
        public:
            DateTimeParser(): base_type(main) {
                main %= date_parser | time_parser;
            }
            DateParser date_parser;
            TimeParser time_parser;
    };
    
    #include<iostream>
    #include<cstdio>
    
    struct Printer : public boost::static_visitor<> {
        void operator()(Date a) const {
            printf("Year: %d, Month: %d, Day: %d\n", a.year, a.month, a.day);
        }
        void operator()(Time a) const {
            printf("Hour: %d, Minute: %d, Second: %d\n", a.hour, a.minute, a.second);
        }
    };
    
    int main() {
        std::string s;
        std::getline(std::cin, s);
        Iterator beg = s.begin(), end = s.end();
        boost::variant<Date, Time> ret;
        phrase_parse(beg, end, DateTimeParser(), ascii::space, ret);
        if (beg != end)
            puts("Parse failed.");
        else
            boost::apply_visitor(Printer(), ret);
    }
    
    0 讨论(0)
提交回复
热议问题