提振精神解析器与继承的属性 - 简单的例子就不会编译 [英] Boost Spirit parser with inherited attributes - simple example won't compile

查看:179
本文介绍了提振精神解析器与继承的属性 - 简单的例子就不会编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写与升压::精神类C语言使用继承属性转移有关变量的作用域信息解析器。例如,名称空间中的{VAR B}将通过A为解析器一个属性变种B。

I'm trying to write a parser for a C-like language with boost::spirit that uses inherited attributes to transfer information about variable scope. For instance, "namespace a { var b }" would pass "a" as an attribute to the parser for "var b".

我无法用继承的属性编译此code得到一个基本的解析器:

I'm having trouble getting a basic parser using inherited attributes to compile this code:

#ifndef CPARSER_DEF_HPP
#define CPARSER_DEF_HPP

#include <string>

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

namespace encoding = boost::spirit::ascii;
using boost::spirit::unused_type;
using boost::spirit::qi::rule;

template <typename Iterator>
struct cparser : boost::spirit::qi::grammar<
                    Iterator,
                    std::string(std::string),
                    encoding::space_type
                    >
{
    rule<Iterator, std::string(std::string), encoding::space_type> start;
    rule<Iterator, std::string(std::string), encoding::space_type> sym_list;

    cparser() :
        cparser::base_type(start)
    {
        sym_list         = encoding::string(boost::spirit::qi::_r1);
        start            = sym_list(boost::spirit::qi::_r1);
    }

};

#endif

标准::字符串::为const_iterator&GT;

这分析器与 cparser&LT实例在main()解析器

This parser is instantiated in main() with cparser<std::string::const_iterator> parser.

我相信这个解析器应该接受一个std ::字符串作为其继承属性,解析输入这个字符串匹配,则返回的字符串为合成属性。这个例子code不编译,我想不通为什么。我一直在用编译GCC和与锵C ++ 11功能。无论从编译器的输出是巨大的(约1000线),我不能做出任何意义。是否与使用的boost ::精神问题::齐:: _ R1 ?与性病的问题::字符串(标准::字符串)在规则中声明?

I believe this parser should accept a std::string as its inherited attribute, parse input matching this string, then return the string as a synthesised attribute. This example code does not compile, and I cannot figure out why. I have been compiling with GCC and Clang with C++11 enabled. The output from either compiler is huge (approx. 1000 lines), and I cannot make any sense of it. Is there a problem with the use of boost::spirit::qi::_r1? A problem with std::string(std::string) in the rule declarations?

在此先感谢您的帮助。

推荐答案

我试图在这里<构建一个例子 /一>。我不知道我面对你有同样的问题(因为你没有提供一种方式来重现出来),但我想有两个不同的问题:第一个是很常见的,就是PhoenixV2不工作现代编译,因为在升压1.52的重大更改,使得 BOOST_RESULT_OF_USE_DECLTYPE 默认值。
有两种方法来解决这个问题(至少与coliru用途铛版本)或者通过定义使用V3 BOOST_SPIRIT_USE_PHOENIX_V3 (这是我推荐的,我认为它应该是默认的),或者通过定义 BOOST_RESULT_OF_USE_TR1 中的result_of协议返回行为V2的期望。第二个问题,其中我没有那么熟悉那么借此与一粒盐或只是等待来自一个人的回答较有经验的,是它似乎不能直接传递字符串作为继承属性,你需要(使用PHX :: VAL(富)也似乎工作),他们作为一个字符串传递。

I've tried to construct an example here. I'm not sure I've faced the same issues you have(since you didn't provide a way to reproduce them) but I think there were two different problems: The first one, very common, is that PhoenixV2 does not work with "modern compilers" since the breaking change in Boost 1.52 that made BOOST_RESULT_OF_USE_DECLTYPE the default. There are two ways to solve this (at least with the clang version that coliru uses) either use V3 by defining BOOST_SPIRIT_USE_PHOENIX_V3(this is what I would recommend, and I think it should be the default) or return the result_of protocol to the behaviour V2 expects by defining BOOST_RESULT_OF_USE_TR1. The second problem, of which I'm not as familiarized so take this with a grain of salt or simply wait for an answer from "someone" more experienced, is that it seems that you can't pass string literals directly as an inherited attribute, you need to pass them as a string (using phx::val("foo") also seems to work).

短版:定义BOOST_SPIRIT_USE_PHOENIX_V3并通过您继承的属性为std :: string而不是作为一个字符串

Short version: Define BOOST_SPIRIT_USE_PHOENIX_V3 and pass your inherited attributes as a std::string not as a string literal.

这篇关于提振精神解析器与继承的属性 - 简单的例子就不会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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