使用Boost标记生成器显示分隔符,不标记化的引号的字符串 [英] Using BOOST Tokenizer to display delimiter and to not tokenize a string in quotes

查看:196
本文介绍了使用Boost标记生成器显示分隔符,不标记化的引号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用BOOST标记生成器到字符串分解为toekn。基本上令牌将被用于创建基于C / C ++ VSL的编译器。我想问的是有可能的分隔符使用定义的创建

  char_separator<焦炭>月(;&所述;&下;);

也可以显示
例如,如果我使用升压标记生成器上串

 字符串s =COUT<<你好;

它应该做如下标记

  COUT
<<
你好
;

此外,我怎么能保证它不会转换在引号标记化的东西

 字符串s =你好我的\\的名字是\\哈桑

应转换为下列标记

 你好
我的
名字是
哈桑


解决方案

我建议提振精神:住在Coliru

修改查看 http://www.boost.org/doc/libs/1_55_0/libs/spirit/example/qi/compiler_tutorial

 的#include<升压/精神/有/ qi.hpp>命名空间补气=的boost ::精神::补气;INT主(INT ARGC,字符** argv的)
{
    的typedef的std ::字符串::为const_iterator它;
    标准::字符串常量输入=COUT<<你好,我的\\的名字是\\哈桑    齐::规则<它,标准::字符串()>分隔符=齐:: char_(;)|齐::字符串(<<);
    齐::规则<它,标准::字符串()>引述=''>> *〜补气:: char_('')> '';
    齐::规则<它,标准::字符串()>字= +((报价|齐:: char_) - 分隔符);    的std ::矢量<标准::字符串>令牌;
    如果(气::解析(input.begin(),input.end()*(字GT;>分隔符),令牌))
    {
        为(自动&安培;令牌:令牌)
            性病::法院LT&;< '&所述;&下;令牌LT;< '\\ n;
    }
}

输出:

 COUT
'<<'
'你好'
';'
我的'
''
'名字是'
''
哈桑

I am using BOOST Tokenizer to break a string into toekn. Basically the tokens will be used to create a compiler for VSL based on c/c++. What i wanted to ask that is it possible that the delimiter defined created using

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

be also displayed for example if i use Boost tokenizer on string

string s= "cout<<hello;"

it should make the following tokens

cout
<<
hello
;

Also how can i ensure that it does not convert tokenize something in quotes like

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

should be converted to following tokens

hello
my
name is
Hassan

解决方案

I suggest boost spirit: Live On Coliru

Edit See also 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";
    }
}

Output:

'cout'
'<<'
'hello'
';'
'my'
' '
'name is'
' '
'Hassan'

这篇关于使用Boost标记生成器显示分隔符,不标记化的引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆