改进替代解析器的使用

发布于 2024-09-25 07:11:09 字数 2277 浏览 7 评论 0原文

我扩展了 Spirit 手册中的 Mini XML 示例。
该语法描述了一个 xml 标签,可以用“/>”结束并且没有子节点,或者像示例中那样用结束标记 '' 关闭,并且可以选择有子节点。

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant.hpp>
#include <boost/variant/recursive_variant.hpp>

struct XmlTree;

typedef boost::variant<boost::recursive_wrapper<XmlTree>, std::string>
    mini_xml_node;

typedef std::vector<mini_xml_node> Children;

struct XmlTree
{
    std::string name;
    Children childs;
};

BOOST_FUSION_ADAPT_STRUCT(
XmlTree,
(std::string, name)
(Children, childs)
)

typedef std::string::const_iterator Iterator;

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

class XmlParserGrammar : public qi::grammar<Iterator, XmlTree(), qi::locals<std::string*>, ascii::space_type>
{
public:
XmlParserGrammar() : XmlParserGrammar::base_type(xml, "xml")
{
    using qi::lit;
    using qi::lexeme;
    using qi::attr;
    using ascii::space;
    using ascii::char_;
    using ascii::alnum;
    using phoenix::val;

    xml %=
        startTag[qi::_a = &qi::_1]  >>
        (
        (
            lit("/>") > attr(Children()) //can i remove this somehow?
        )
        |
        (
            lit(">")
            >> *node_
            > endTag(*qi::_a)
        )
        );

    startTag %= '<' >> !lit('/') >> lexeme[ +(alnum - (space | '>' | "/>")) ] ;

    node_ %= xml | text;

    endTag = "</" > lit(qi::_r1) > '>';

    text %= lexeme[+(char_ - '<')];
}

private:
    qi::rule<Iterator, XmlTree(), qi::locals<std::string*>, ascii::space_type> xml;
    qi::rule<Iterator, std::string(), ascii::space_type> startTag;
    qi::rule<Iterator, mini_xml_node(), ascii::space_type> node_;
    qi::rule<Iterator, void(std::string&), ascii::space_type> endTag;
    qi::rule<Iterator, std::string(), ascii::space_type> text;
};

是否可以在没有 attr(Children()) 标签的情况下编写此规则?我认为这或多或少是性能滞后。我需要它来避免替代解析器的可选属性。 如果没有子标签,则属性只能是空向量。

I extended the Mini XML example from the spirit manual.
The grammar describes a xml tag that can be closed with '/>' and has no child nodes or which is closed like in the example with a closing tag '' and can optionally have children.

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant.hpp>
#include <boost/variant/recursive_variant.hpp>

struct XmlTree;

typedef boost::variant<boost::recursive_wrapper<XmlTree>, std::string>
    mini_xml_node;

typedef std::vector<mini_xml_node> Children;

struct XmlTree
{
    std::string name;
    Children childs;
};

BOOST_FUSION_ADAPT_STRUCT(
XmlTree,
(std::string, name)
(Children, childs)
)

typedef std::string::const_iterator Iterator;

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

class XmlParserGrammar : public qi::grammar<Iterator, XmlTree(), qi::locals<std::string*>, ascii::space_type>
{
public:
XmlParserGrammar() : XmlParserGrammar::base_type(xml, "xml")
{
    using qi::lit;
    using qi::lexeme;
    using qi::attr;
    using ascii::space;
    using ascii::char_;
    using ascii::alnum;
    using phoenix::val;

    xml %=
        startTag[qi::_a = &qi::_1]  >>
        (
        (
            lit("/>") > attr(Children()) //can i remove this somehow?
        )
        |
        (
            lit(">")
            >> *node_
            > endTag(*qi::_a)
        )
        );

    startTag %= '<' >> !lit('/') >> lexeme[ +(alnum - (space | '>' | "/>")) ] ;

    node_ %= xml | text;

    endTag = "</" > lit(qi::_r1) > '>';

    text %= lexeme[+(char_ - '<')];
}

private:
    qi::rule<Iterator, XmlTree(), qi::locals<std::string*>, ascii::space_type> xml;
    qi::rule<Iterator, std::string(), ascii::space_type> startTag;
    qi::rule<Iterator, mini_xml_node(), ascii::space_type> node_;
    qi::rule<Iterator, void(std::string&), ascii::space_type> endTag;
    qi::rule<Iterator, std::string(), ascii::space_type> text;
};

Is it possible to write this rule without the attr(Children()) tag? I think it is more or less a performance lag. I need it to avoid the optional attribute of the alternative parser.
If there are no child tags the attribute should only be an empty vector.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2024-10-02 07:11:09

您应该能够编写:

xml %= startTag[_a = &_1] 
       >> attributes 
       >> (  "/>" >> eps
          |  ">" >> *node > endTag(*_a) 
          )
    ;

这使得矢量属性保持不变(并且为空)。

You should be able to write:

xml %= startTag[_a = &_1] 
       >> attributes 
       >> (  "/>" >> eps
          |  ">" >> *node > endTag(*_a) 
          )
    ;

That leaves the vector attribute unchanged (and empty).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文