2

I want to search for a specific characters position from a string. Specific characters are given by vectors. So, I want to combine ascii::string rules from vector.

I used "|=" operands. But it not works.

#include <iostream>
#include <string>
#include <vector>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

namespace qi    = boost::spirit::qi;
namespace ascii    = boost::spirit::ascii;

int main()
{
    std::string test = "abc def aaa ffew";
    auto itr = test.begin(), end = test.end();

    std::vector<std::string> find_list = {"des", "ffe", "asaa"};
    std::string result;

    {
        std::string result;
        qi::rule<std::string::iterator, std::string()> rule;
        rule = ascii::string(find_list[0]) | ascii::string(find_list[1]) | ascii::string(find_list[2]);
        // this works
        if (qi::parse(itr, end, (qi::omit[*(qi::char_ - rule)] >> rule), result)) {
            std::cout << result << std::endl;
        } else std::cout << "failed." << std::endl;
    }

    itr = test.begin();

    // Sample that what I want to do
    {
        std::string result;
        qi::rule<std::string::iterator, std::string()> rule;
        for (auto && str : find_list) {
            // this is not equals rule = ascii::string(find_list)[0] | ascii::string(find_list)[1] | .... [n] ??
            rule |= ascii::string(str); 
        }
        // this not works
        if (qi::parse(itr, end, (qi::omit[*(qi::char_ - rule)] >> rule), result)) {
            std::cout << result << std::endl;
        } else std::cout << "failed." << std::endl;

    }

    return 0;
}

output:

ffe
failed.
0

What should I do? Thanks for the help in advance.

Riyaaaaa
  • 61
  • 4
  • https://stackoverflow.com/questions/20158897/boost-spirit-qi-crashes-when-assigning-rule-to-a-sequence-including-itself and https://stackoverflow.com/questions/17000897/boost-spirit-qi-dynamically-create-difference-parser-at-parse-time/17003832 may be relevant. – llonesmiz Feb 15 '18 at 07:14

1 Answers1

2

Don't reinvent the wheel. Spirit already has a - highly optimized - parser for this: qi::symbols<>:

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>

namespace qi = boost::spirit::qi;
namespace qr = boost::spirit::repository::qi;

int main() {
    std::string const test = "abc def aaa ffew";

    qi::symbols<char> find_list;
    find_list += "des", "ffe", "asaa";

    std::string result;
    if (qi::parse(test.begin(), test.end(), qr::seek[qi::raw[find_list]], result)) {
        std::cout << result << std::endl;
    } else {
        std::cout << "failed." << std::endl;
    }
}

Prints

ffe

Without seek[] from the Spirit Repository it gets shorter but less efficient: Live On Coliru

sehe
  • 374,641
  • 47
  • 450
  • 633