3

我正在使用 BOOST Tokenizer 将字符串分解为 toekn。基本上,这些令牌将用于为基于 c/c++ 的 VSL 创建编译器。我想问的是,定义的分隔符有可能是使用创建的

char_separator<char> sep("; << "); 

例如,如果我在字符串上使用 Boost 标记器,也会显示

string s= "cout<<hello;"

它应该制作以下标记

cout
<<
hello
;

另外,我如何确保它不会在引号中转换标记,例如

string s= "hello my \"name is\" Hassan"

应转换为以下标记

hello
my
name is
Hassan
4

1 回答 1

3

我建议提振精神: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'
于 2014-03-01T22:59:28.090 回答