#include #include #include #include #include #include #include #include #include #include namespace client { namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; struct employee { int age; std::string surname; std::string forename; double salary; }; } BOOST_FUSION_ADAPT_STRUCT( client::employee, (int, age) (std::string, surname) (std::string, forename) (double, salary) ) namespace client { /**************************************************************************/ template struct id_grammar : qi::grammar { id_grammar() : id_grammar::base_type(start) { using qi::int_; using qi::lexeme; using ascii::alpha; using ascii::alnum; start %= lexeme[('_' | alpha) >> *(alnum | '_' | '-')]; } qi::rule start; }; /**************************************************************************/ template struct employee_grammar : qi::grammar { employee_grammar() : employee_grammar::base_type(start) { using qi::int_; using qi::double_; using qi::lexeme; using ascii::alpha; using ascii::alnum; /*******************************************************/ //id %= lexeme[('_' | alpha) >> *(alnum | '_' | '-')]; client::id_grammar id; /*******************************************************/ start %= int_ >> id >> id >> double_ ; } /*******************************************************/ qi::rule id; /*******************************************************/ qi::rule start; }; } int main() { std::cout << "Give an age, surname, forename, and salary in this format:\n"; std::cout << "int str str double\n"; using boost::spirit::ascii::space; typedef std::string::const_iterator iterator_type; typedef client::employee_grammar employee_grammar; employee_grammar g; std::string str; while (getline(std::cin, str)) { if (str.empty() || str[0] == 'q' || str[0] == 'Q') break; client::employee emp; std::string::const_iterator iter = str.begin(); std::string::const_iterator end = str.end(); bool r = phrase_parse(iter, end, g, space, emp); if (r && iter == end) { std::cout << boost::fusion::tuple_delimiter(" "); std::cout << "got " << boost::fusion::as_vector(emp) << std::endl; } else { std::cout << "fail\n"; } } return 0; }