gpt4 book ai didi

c++ - Boost.Spirit Qi : different entry points to the same grammar?

转载 作者:太空狗 更新时间:2023-10-29 23:01:03 26 4
gpt4 key购买 nike

我有一个递归语法,想从它的不同规则开始解析。是否可以不多次重写相同的语法?

示例:我有 json 解析器:

template <typename It, typename Skipper = qi::space_type>
struct grammar : qi::grammar<It, value (), Skipper>
{
grammar () : grammar::base_type (value_)
{
using namespace qi;

static auto const null_ = proto::deep_copy ("null" >> qi::attr (null {}));

static auto const bool_ = proto::deep_copy (
"true" >> qi::attr (true) | "false" >> qi::attr (false));

static auto const text_ = proto::deep_copy (
'"' >> qi::raw [*('\\' >> qi::char_ | ~qi::char_('"'))] >> '"');

value_ = null_ | bool_ | text_ | double_ | object_ | array_;
member_ = text_ >> ':' >> value_;
object_ = '{' >> -(member_ % ',') >> '}';
array_ = '[' >> -(value_ % ',') >> ']';

BOOST_SPIRIT_DEBUG_NODES((value_)(member_)(object_)(array_))
}

private:
qi::rule<It, json:: value (), Skipper> value_;
qi::rule<It, json::member (), Skipper> member_;
qi::rule<It, json::object (), Skipper> object_;
qi::rule<It, json:: array (), Skipper> array_;
};

通常我需要将输入解析为json ,但有时我需要将其解析为json 数组或json 对象。如果这些语法之间的唯一区别是入口点,我是否可以在不一遍又一遍地重写相同语法的情况下做到这一点?

最佳答案

我找到的最接近的解决方案是将语法拆分为基类和派生类,并在派生类中使用不同的开始规则。它并没有太多重复源代码,但看起来仍然是超重解决方案。

template <typename It, typename Data, typename Skipper = qi::space_type>
struct base : qi::grammar<It, Data (), Skipper>
{
using jbase_type = base;

template <typename Member>
base (Member& member) : base::base_type (member)
{
using namespace qi;
using namespace Json;

value_ = null_ | bool_ | text_ | double_ | object_ | array_;
member_ = text_ >> ':' >> value_;
object_ = '{' >> -(member_ % ',') >> '}';
array_ = '[' >> -(value_ % ',') >> ']';

BOOST_SPIRIT_DEBUG_NODES((value_)(member_)(object_)(array_))
}

protected:
escaped_string_grammar<It> text_;

qi::rule<It, Json:: Value (), Skipper> value_;
qi::rule<It, Json::Member (), Skipper> member_;
qi::rule<It, Json::Object (), Skipper> object_;
qi::rule<It, Json:: Array (), Skipper> array_;
};

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <typename It, typename Skipper = qi::space_type>
struct value : base<It, Json::Value, Skipper>
{
value () : value::jbase_type (value::jbase_type::value_) {}
};

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <typename It, typename Skipper = qi::space_type>
struct array : base<It, Json::Array, Skipper>
{
array () : array::jbase_type (array::jbase_type::array_) {}
};

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <typename It, typename Skipper = qi::space_type>
struct object : base<It, Json::Object, Skipper>
{
object () : object::jbase_type (object::jbase_type::object_) {}
};

关于c++ - Boost.Spirit Qi : different entry points to the same grammar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31953100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com