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

烂漫一生 提交于 2019-12-10 19:03:17

问题


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

回答1:


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'


来源:https://stackoverflow.com/questions/22120842/using-boost-tokenizer-to-display-delimiter-and-to-not-tokenize-a-string-in-quote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!