使用 BOOST 分词器显示分隔符,并且不对引号中的字符串进行分词

Using BOOST Tokenizer to display delimiter and to not tokenize a string in quotes

本文关键字:分词 字符串 显示 BOOST 分隔符 使用      更新时间:2023-10-16

我正在使用BOOST分词器将字符串分解为脚趾。基本上,令牌将用于创建基于 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

我建议提升精神:住在科里鲁

编辑 另请参阅 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'