使用Boost :: Spirit的自定义跳过解析器 [英] Custom Skip Parser with Boost::Spirit

查看:87
本文介绍了使用Boost :: Spirit的自定义跳过解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准的ascii::space_type船长当然不会跳过我的评论.文档提到您可以创建自己的跳过解析器,但实际上没有任何示例.

The standard ascii::space_type skipper does of course not skip my comments. The docs mention you can make your own skip parser but there is no example of actually how to do it.

我只需要一个示例代码或其他任何东西,我已经谷歌搜索了2个小时.

I'd just need an example code or anything, I've been googling for 2 hours now.

请不要将我指向示例,其中一些链接工作与Spirit 1.6绝望地过时了.

Please don't point me to the examples, the few links that work are hopelessly outdated, dealing with Spirit 1.6.

推荐答案

经过一些实验,我找到了一种指定自定义船长的方法,并将在此处进行概述:

After some experimentation, I have found a way to specify a custom skipper and will outline it here:

template<typename Iterator>
struct pl0_skipper : public qi::grammar<Iterator> {

    pl0_skipper() : pl0_skipper::base_type(skip, "PL/0") {
        skip = ascii::space | ('{' >> *(qi::char_ - '}') >> '}');
    }
    qi::rule<Iterator> skip;
};

template<typename Iterator, typename Skipper = pl0_skipper<Iterator>>
struct pl0_grammar : public qi::grammar<Iterator, Skipper> {

    /* The rules use our skipper */
    qi::rule<Iterator, Skipper> start;
    qi::rule<Iterator, Skipper> block;
    qi::rule<Iterator, Skipper> statement;

};

秘密在于解析器的调用.由于某些原因,当您想使用parse_phrase进行解析时,必须提供一个skipper语法对象.我不知道这一点:

The secret lies in the call of the parser. For some reason, when you want to parse this using parse_phrase, you have to give a skipper grammar object. I was not aware of this:

typedef std::string::const_iterator iterator_t;
typedef parser::pl0_grammar<iterator_t> grammar;
typedef parser::pl0_skipper<iterator_t> skipper;

grammar g;
skipper ws;

iterator_t iter = str.begin();
iterator_t end = str.end();
bool r = phrase_parse(iter, end, g, ws);

这有效.

这篇关于使用Boost :: Spirit的自定义跳过解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆