提升使用功能齐撰写规则 [英] Boost Qi Composing rules using Functions

查看:159
本文介绍了提升使用功能齐撰写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图定义为以最小的code重复语言的多个子集部分的boost ::精神::气分析器。要做到这一点,我创建了一些基本规则建设职能。原来的解析器工作正常,但一旦我开始使用排版功能,我的解析器似乎不再工作。

I'm trying to define some Boost::spirit::qi parsers for multiple subsets of a language with minimal code duplication. To do this, I created a few basic rule building functions. The original parser works fine, but once I started to use the composing functions, my parsers no longer seem to work.

一般语言的形式是:

A B: C

有语言的子集,其中 A B C 必须是具体的类型,如 A 是一个int,而 B C 是浮动。下面是我用该子语言解析器:

There are subsets of the language where A, B, or C must be specific types, such as A is an int while B and C are floats. Here is the parser I used for that sub language:

using entry = boost::tuple<int, float, float>;

template <typename Iterator>
struct sublang : grammar<Iterator, entry(), ascii::space_type>
{
   sublang() : sublang::base_type(start)
   {
       start = int_ >> float_ >> ':' >> float_;
   }
   rule<Iterator, entry(), ascii::space_type> start;
};

不过,因为有许多子集,我试图创建建立我的语法规则的函数:

But since there are many subsets, I tried to create a function to build my parser rules:

template<typename AttrName, typename Value>
auto attribute(AttrName attrName, Value value)
{
    return attrName >> ':' >> value;
}

所以,我可以为每个子集更轻松地构建解析器不重复信息:

So that I could build parsers for each subset more easily without duplicate information:

// in sublang
start = int_ >> attribute(float_, float_);

然而,这失败,我不知道为什么。在我的测试铿锵,解析只是失败。在G ++,它似乎是程序崩溃。

This fails however and I'm not sure why. In my clang testing, parsing just fails. In g++, it seems the program crashes.

下面是完整的例子code: http://coliru.stacked-crooked.com / A / 8636f19b2e9bff8d

Here's the full example code: http://coliru.stacked-crooked.com/a/8636f19b2e9bff8d

什么是错的当前code和会是什么这个问题的正确的做法?我想避免指定每个子语言解析器的属性和其他元素的语法。

What is wrong with the current code and what would be the correct approach for this problem? I would like to avoid specifying the grammar of attributes and other elements in each sublanguage parser.

推荐答案

很简单:使用汽车与精神(或任何EDSL基于升压原和Boost凤凰城)是最有可能的未定义Behaviour¹

Quite simply: using auto with Spirit (or any EDSL based on Boost Proto and Boost Phoenix) is most likely Undefined Behaviour¹

现在,您可以使用通常解决这个问题。

Now, you can usually fix this using


  • BOOST_SPIRIT_AUTO

  • 的boost ::原:: DEEP_COPY

  • 是在最新版的Boost(TODO添加链接)
  • 即将到来的新工厂
  • BOOST_SPIRIT_AUTO
  • boost::proto::deep_copy
  • the new facility that's coming in the most recent version of Boost (TODO add link)

在这种情况下,

template<typename AttrName, typename Value>
auto attribute(AttrName attrName, Value value) {
    return boost::proto::deep_copy(attrName >> ':' >> value);
}

修复它: <大骨节病> 住在Coliru


  1. 您可以使用气懒:: [] 与继承的属性。

我在 prop_key 规则非常类似的事情在<一href=\"http://stackoverflow.com/questions/27746109/reading-json-file-with-c-and-boost/27760376#27760376\">Reading JSON文件,C ++和Boost 。

I do very similar things in the prop_key rule in Reading JSON file with C++ and BOOST.

你可以看看在 <一个href=\"http://www.boost.org/doc/libs/1_57_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/operators/keyword_list.html\"相对=nofollow>关键字列表操作 从精神资源库。它的设计允许像语法更容易建设:

you could have a look at the Keyword List Operator from the Spirit Repository. It's designed to allow easier construction of grammars like:

no_constraint_person_rule %=
    kwd("name")['=' > parse_string ]
  / kwd("age")   ['=' > int_]
  / kwd("size")   ['=' > double_ > 'm']
  ;


  • 这你可能用 Nabialek把戏结合。我搜索的SO为例子的答案。 (一个是语法平衡问题

  • This you could potentially combine with the Nabialek Trick. I'd search the answers on SO for examples. (One is Grammar balancing issue)

    ¹除了完全无国籍者(<一个href=\"http://stackoverflow.com/questions/10144948/static-functions-from-boost-lambda-or-boost-phoenix/12326204#12326204\">Eric Niebler这个)和前pression占位符。例如参见


    ¹ Except for entirely stateless actors (Eric Niebler on this) and expression placeholders. See e.g.

    • Assigning parsers to auto variables
    • undefined behaviour somewhere in boost::spirit::qi::phrase_parse
    • C++ Boost qi recursive rule construction
    • boost spirit V2 qi bug associated with optimization level

    一些例子

    • Define parsers parameterized with sub-parsers in Boost Spirit
    • Generating Spirit parser expressions from a variadic list of alternative parser expressions

    这篇关于提升使用功能齐撰写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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