#include #include #include #include #include // line: "AT" commandlist? // commandlist: // simplecmd commandlist? // | gsmcmd ( ";" commandlist )? // simplecmd: // "S" \d+ "?" // | "S" \d+ "=" \d+ // | "D" [0-9+W-]+ // | "H" // | "A" // | "BCEFGI-RT-Z" \d* // | "&[A-Z]" \d* // gsmcmd: gsmquery | gsminfo | gsmset | gsmbase // // gsmquery: /[+%]\w+/ "?" // gsminfo: /[+%]\w+/ "=?" // gsmset: /[+%]\w+/ "=" paramlist // gsmbase: /[+%]\w+/ // // paramlist: param ( "," paramlist )? // param: /[0-9a-zA-Z_]+/ // | "\"" [^"]+ "\"" using namespace boost::spirit::classic; typedef const char* iterator_t; typedef ast_match_policy > match_policy_t; typedef scanner > scanner_t; struct hayescommands : public grammar { template struct definition { definition(const hayescommands& self) { line = as_lower_d["at"] >> !commandlist; commandlist = simplecmd >> *commandlist | gsmcmd >> *( str_p(";") >> commandlist ) | dial >> str_p(";"); simplecmd = dial | sreg | other; other = ( (ch_p('&') >> alpha_p) | (alpha_p - chset_p("DdSs") ) ) >> !uint_p; dial = chset_p("Dd") >> !chset_p("TPtp") >> *chset_p("+0-9*#ABCD,W!@abcdw-") >> *chset_p("IiGg"); sreg = set_sreg | query_sreg; set_sreg = chset_p("Ss") >> uint_p >> ch_p('=') >> uint_p; query_sreg = chset_p("Ss") >> uint_p >> !ch_p('?'); gsmcmd = gsmquery | gsminfo | gsmset | gsmbase ; gsmquery = cmd >> str_p("?"); gsminfo= cmd >> str_p("=?"); gsmset= cmd >> str_p("=") >> paramlist; gsmbase= cmd; cmd = chset_p("+%") >> +alpha_p; paramlist = param >> *( ch_p(',') >> paramlist ); param = empty | word | quotedstring; empty = str_p(""); word = +chset_p("a-zA-Z0-9") ; quotedstring = ch_p('"') >> *( anychar_p - ch_p('"') ) >> ch_p('"'); } rule line, commandlist, simplecmd, other, dial, sreg, set_sreg, query_sreg, gsmcmd, gsmquery, gsminfo, gsmset, gsmbase, paramlist, param, empty, word, quotedstring, cmd; const rule& start() const { return line; } }; }; const char *lines[]= { "ATdt+12313123", "atd123123;", "AT+FCLASS=1;H0S0=0", NULL }; int main(int, char**) { hayescommands p; for (unsigned i= 0 ; lines[i] ; i++) { scanner_t scanner(lines[i], lines[i]+strlen(lines[i])); p.parse(scanner); } }