Boost C ++ - 搜索精神符号表

时间:2013-02-13 19:30:05

标签: c++ boost boost-spirit

如果我有一个符号表,那就是提升精神

struct Foo : boost::spirit::qi::symbols<char, MyEnums::FruitType>
{
    Foo(const std::string& name = std::string("FooTable")
      : boost::spirit::qi::symbols<char, MyEnums::FruitType>(name)
    {
        add("apple", MyEnums::Apple)
           ("orange", MyEnums::Orange)
           ("peach", MyEnums::Peach);
    }
}

稍后在我的代码中,如果我有数据类型,我该如何获得符号?例如:

Foo fruitSymbolTable;
MyEnums::FruitType fruit = MyEnums::Apple;

std::string fruitTypeString = ???? // want it to be equal to "apple"

我如何使用该表来获得&#34; apple&#34;?

另外,我知道我可以有一个像这样的表:

struct Foo : boost::spirit::qi::symbols<char, MyEnums::FruitType>
{
    Foo(const std::string& name = std::string("FooTable")
      : boost::spirit::qi::symbols<char, MyEnums::FruitType>(name)
    {
        add("apple", MyEnums::Apple)
           ("apple2", MyEnums::Apple)
           ("orange", MyEnums::Orange)
           ("peach", MyEnums::Peach);
    }
}

MyEnums :: Apple可以是&#34; apple&#34;或&#34; apple2&#34;在这种情况下,我更喜欢第一个&#39;比赛。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

好吧,您似乎希望从qi::symbols解析器转换为karma::symbols生成器。

我会说这样的事情不存在。既然你可能已经找到了它,我认为可以安全地假设它没有。

我可以看到它是如何有用的,所以你可以考虑将它贡献给Spirit Repository。

我花了一些时间写一个简单的映射器,从qi::symbols转换为karma::symbols

template <typename Char, typename T, typename Lookup, typename Filter>
    karma::symbols<T, std::basic_string<Char> > 
    inverse(qi::symbols<Char,T,Lookup,Filter> const& parser)
{
    karma::symbols<T, std::basic_string<Char> > generator;

    parser.for_each([&generator](std::basic_string<Char> const& symbol, T v) 
        { 
            generator.add(v, symbol); 
        });

    return generator;
}

编辑在C ++ 03风格中:

template <typename Char, typename T, typename Lookup, typename Filter>
    karma::symbols<T, std::basic_string<Char> > 
    inverse(qi::symbols<Char,T,Lookup,Filter> const& parser)
{
    struct _adder { 
        karma::symbols<T, std::basic_string<Char> > result;

        void operator()(std::basic_string<Char> const& symbol, T v) { 
            result.add(v, symbol); 
        }
    } adder;

    parser.for_each(adder);
    return adder.result;
}
  

注意这确实处理解析符号表中可能重复映射的值。

以下是一个简单的示例程序,可以显示它的工作原理: http://liveworkspace.org/code/3uzQKi

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>

namespace qi     = boost::spirit::qi;
namespace karma  = boost::spirit::karma;

template <typename ItType>
struct parser : public qi::grammar<ItType, int()>
{
    parser() : parser::base_type(start)
    {
        _sym.add("aap", 1)("noot", 2)("mies", 3);
        start = _sym;
    }
  // private:
    qi::symbols<char, int> _sym;
    qi::rule<ItType, int()> start;
};

template <typename ItType>
struct generator : public karma::grammar<ItType, int()>
{
    template<typename Sym>
    generator(Sym&& sym) : generator::base_type(start)
    {
        // _sym.add(1, "aap")(2, "noot")(3, "mies");
        _sym = std::move(sym);
        start = _sym;
    }
  private:
    karma::symbols<int, std::string> _sym;
    karma::rule<ItType, int()> start;
};

template <typename Char, typename T, typename Lookup, typename Filter>
    karma::symbols<T, std::basic_string<Char> > 
    inverse(qi::symbols<Char,T,Lookup,Filter> const& parser)
{
    karma::symbols<T, std::basic_string<Char> > generator;

    parser.for_each([&generator](std::basic_string<Char> const& symbol, T v) 
        { 
            generator.add(v, symbol); 
        });

    return generator;
}

void testcase(std::string const& str)
{
    typedef std::string::const_iterator It;

    It begin = str.cbegin();
    It end   = str.cend();

    static const parser<It> p;

    int out;
    bool ok = qi::parse(begin, end, p, out);
    std::cout << std::boolalpha << ok << '\n';
    std::cout << "remains: '" << std::string(begin, end) << "'\n";
    if (ok)
    {
        std::cout << "parsed value: " << out << "\n";
        static const generator<boost::spirit::ostream_iterator> g(inverse(p._sym));
        std::cout << "generator: " << karma::format(g, out) << "\n";
    }

}

int main()
{
    const auto v = std::vector<std::string>{ "aap", "noot", "mies" };
    for (auto it=v.begin(); it!=v.end(); ++it)
        testcase(*it);
}

输出:

true
remains: ''
parsed value: 1
generator: aap
true
remains: ''
parsed value: 2
generator: noot
true
remains: ''
parsed value: 3
generator: mies