How to build a default boost grammar?

I am analyzing some kind of scientific text, the format of which is similar to

Keyword
{ 1.0  22.2  59.6 'cm' 'yes' }

I am new to the spirit, and after studying the document, I can use the spirit to solve a fixed-format keyword.

But in the following format, I do not know how to build a grammar. My question is: In the scientific keyword that I come across, some data items may be defaulted to default for the built-in default. The keyword description indicates when default values ​​can be applied. There are two ways to set values ​​to default values. First, by terminating data ahead of time with a slash '}', the remaining unspecified values ​​are set to default values. Secondly, the selected quantities located before '}' can be defaulted by entering n *, where n is the number of consecutive quantities to be used by default. For example, 3 * causes the following three values ​​in the keyword data to get their default values.

For instance,

Person
{ 'Tom' 188 80 'male' 32 }

say "male" "32" - , :

Person
{ 'Tom' 188 88 2* }

Person
{ 'Tom' 188 88 'male' 1* }

Person
{ 'Tom' 188 88 }

, , n *?

+5
3

, , , :

  • "2 *"
  • ,

qi::attr -:

  • :

    qi::int_ | qi::attr(180)
    

    . , 180

  • "2 *" ( @vines):

    "2*" >> qi::attr(attr2)
    

    . 2* , attr2 ( fusion::vector).

, , , , ( ):

#include <string>
#include <iostream>

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/vector.hpp>

int main()
{
    namespace qi = boost::spirit::qi;
    namespace fusion = boost::fusion;

    // the attribute passed to the parser has to match (in structure) the 
    // parser, requiring to create nested fusion::vector's
    typedef fusion::vector<std::string, int>              attribute1_type;
    typedef fusion::vector<int, attribute1_type>          attribute2_type;
    typedef fusion::vector<int, attribute2_type>          attribute3_type;

    // overall attribute type
    typedef fusion::vector<std::string, attribute3_type>  attribute_type;

    // initialize attributes with default values
    attribute1_type attr1("male", 32);
    attribute2_type attr2(80, attr1);
    attribute3_type attr3(180, attr2);

    qi::rule<std::string::iterator, std::string()> quoted_string =
        "'" >> *~qi::char_("'") >> "'";

    qi::rule<std::string::iterator, attribute_type(), qi::space_type> data =
        qi::lit("Person") >> "{" 
            >>  quoted_string 
            >> -(   ("4*" >> qi::attr(attr3))
                |   (qi::int_ | qi::attr(180))
                    >> -(   ("3*" >> qi::attr(attr2))
                        |   (qi::int_ | qi::attr(80))
                            >> -(   ("2*" >> qi::attr(attr1))
                                |   (quoted_string | qi::attr("male"))
                                    >> -(   "1*"  
                                        |   qi::int_ 
                                        |   qi::attr(32)
                                        )
                                )
                        )
                )
        >> "}";

    std::string in1 = "Person\n{ 'Tom' 188 80 'male' 32 }";
    attribute_type fullattr1;
    if (qi::phrase_parse(in1.begin(), in1.end(), data, qi::space, fullattr1))
        std::cout << fullattr1 << std::endl;

    std::string in2 = "Person\n{ 'Tom' 188 80 'male' }";
    attribute_type fullattr2;
    if (qi::phrase_parse(in2.begin(), in2.end(), data, qi::space, fullattr2))
        std::cout << fullattr2 << std::endl;

    std::string in3 = "Person\n{ 'Tom' 188 3* }";
    attribute_type fullattr3;
    if (qi::phrase_parse(in3.begin(), in3.end(), data, qi::space, fullattr3))
        std::cout << fullattr3 << std::endl;

    return 0;
}

( @vines) , , .

+4

, =)
" ", . :

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

#include <iostream>
#include <string>


namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;

struct numbers { int i1, i2, i3, i4; };

BOOST_FUSION_ADAPT_STRUCT
(numbers,
    (int, i1)
    (int, i2)
    (int, i3)
    (int, i4)
)

template <typename Iterator, typename Skipper>
struct Grammar : public qi::grammar <Iterator, numbers(), Skipper>
{
    Grammar() : Grammar::base_type(start, "numbers")
    {
    using qi::int_;

    // This rule resets the skip counter:
    init_skip = qi::eps[ph::ref(skp) = 0];

    // This rule parses the skip directive ("n*") and sets the skip counter:
    skip_spec = qi::omit[ (qi::lexeme[ int_ >> "*" ])[ph::ref(skp) = qi::_1] ];

    // This rule checks if we should skip the field, and if so, decrements
    // the skip counter and returns the value given to it (the default one).
    // If not, it tries to parse the int.
    // If int fails to parse, the rule resorts the default value again,
    // thus handling the "premature brace" case.
    int_dflt %= qi::eps(ph::ref(skp) > 0)[--ph::ref(skp)] >> qi::attr(qi::_r1) | int_ | qi::attr(qi::_r1);

    // And this is the grammar:
    start %= init_skip >>
             "{" >> -skip_spec >> int_dflt(-1)
                 >> -skip_spec >> int_dflt(-1)
                 >> -skip_spec >> int_dflt(-1)
                 >> -skip_spec >> int_dflt(-1)
                 >> "}";
    }

    // the skip counter itself:
    int skp;

    qi::rule <Iterator, numbers(), Skipper> start;
    qi::rule <Iterator, Skipper> skip_spec, init_skip;
    qi::rule <Iterator, int(int), Skipper> int_dflt;
};




int main (int argc, char **argv)
{
    using std::cout;
    using std::endl;

    std::string s = argv[1];

    numbers result;

    std::string::iterator ib = s.begin();
    std::string::iterator ie = s.end();
    bool r = qi::phrase_parse(ib, ie, Grammar<std::string::iterator, qi::space_type>(), qi::space, result );

    if (r && ib == ie)
    {
        cout << boost::fusion::tuple_open('[');
        cout << boost::fusion::tuple_close(']');
        cout << boost::fusion::tuple_delimiter(", ");

        cout << "Parsing succeeded\n";
        cout << "got: " << boost::fusion::as_vector(result) << endl;
    }
    else
    {
        cout << "Parsing failed\n";
        cout << "err: " << std::string(ib, ie) << endl;
    }

    return 0;
}

PS: , Skipper - , .

+4

At first I can think:

If your structure does not have too many members, you can simply describe * n as a kind of syntax, for example:

struct_full = "{" >> a >> b >> c >> "}";
struct_reduced_1 = "{" >> a >> b >> "1*" >> attr(c_default) >> "}"
struct_reduced_2 = "{" >> a >> "2*" >> attr(b_default) >> attr(c_default) >> "}";
struct_reduced_3 = "{" >> "3*" >> attr(a_default) >> attr(b_default) >> attr(c_default) >> "}";

Of course, this is not the most beautiful way.

+1
source

All Articles