我建议提振精神:Live On Coliru
编辑另见http://www.boost.org/doc/libs/1_55_0/libs/spirit/example/qi/compiler_tutorial
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
int main(int argc, char** argv)
{
typedef std::string::const_iterator It;
std::string const input = "cout<<hello;my \"name is\" Hassan";
qi::rule<It, std::string()> delimiter = qi::char_("; ") | qi::string("<<");
qi::rule<It, std::string()> quoted = '"' >> *~qi::char_('"') > '"';
qi::rule<It, std::string()> word = +((quoted | qi::char_) - delimiter);
std::vector<std::string> tokens;
if (qi::parse(input.begin(), input.end(), *(word >> delimiter), tokens))
{
for(auto& token : tokens)
std::cout << "'" << token << "'\n";
}
}
输出:
'cout'
'<<'
'hello'
';'
'my'
' '
'name is'
' '
'Hassan'