It's my first time using boos spirit and i want to parse inputs like this to a struct :
fanout_n #(2, 0, 0) FANOUT_2 (c7552_wire_2, {c7552_wire_2_0, c7552_wire_2_1});
or
fanout_n #(2, 0, 0) FANOUT_2 ({wire1,wire2} , {c7552_wire_2_0, c7552_wire_2_1});
my struct is like this:
struct GateStruct { int numberOfInputs; std::string gateName; std::vector<std::string> wireNames_first; std::vector<std::string> wireNames_second; }; for example after parsing, gate has to contain these values :
struct GateStruct { int numberOfInputs = 2; std::string gateName = FANOUT_2; std::vector<std::string> wireNames_first = {wire1 , wire2}; std::vector<std::string> wireNames_second = {c7552_wire_2_0, c7552_wire_2_1}; };gate my grammar is this:
template <typename Iterator> struct gate_parser : qi::grammar<Iterator, GateStruct(), ascii::space_type> { gate_parser() : gate_parser::base_type(start) { using qi::int_; using qi::lit; using qi::double_; using qi::lexeme; using ascii::char_; using qi::_1; //using phoenix::ref; wirenameString %= lexeme[+(char_)]; numberString = lit("(") >> int_ >> *(lit(",") >> lit("0")) >> lit(")"); wireList = -(lit("{")) >> wirenameString >> *(lit(",") >> wirenameString) >> -(lit("}")); //will parse this: {wire1 , wire2 , ...,wiren} start %= lit("fanout_n") >> lit("#") >> numberString >> wirenameString >> lit("(") >> wireList >> lit(",") >> wireList >> lit(");") ; } qi::rule<Iterator , int() , ascii::space_type > numberString; qi::rule<Iterator , std::string(), ascii::space_type > wirenameString; qi::rule<Iterator , std::vector<std::string >() , ascii::space_type> wireList; qi::rule<Iterator , GateStruct(), ascii::space_type > start; }; and use this code to parse inputs into struct:
int main(){ using boost::spirit::ascii::space; typedef std::string::const_iterator iterator_type; typedef client::gate_parser<iterator_type> gate_parser; gate_parser g; std::string str; while (getline(std::cin, str)) { if (str.empty() || str[0] == 'q' || str[0] == 'Q') break; client::GateStruct gate; std::string::const_iterator iter = str.begin(); std::string::const_iterator end = str.end(); bool r = phrase_parse(iter, end, g, space, gate); if (r && iter == end) { std::cout << "-------------------------\n"; std::cout << "Parsing succeeded\n"; std::cout << "\n-------------------------\n"; } else { std::cout << "-------------------------\n"; std::cout << "Parsing failed\n"; std::cout << "Remaining unparsed: '" << std::string(iter,end) << "'\n"; std::cout << "-------------------------\n"; } } std::cout << "Bye... :-) \n\n"; return 0; }
it compiles without any error or warning but unfortunately it doesn't parse my inputs at all.