一般情况:我无法弄清楚为什么我的Spirit语法/语义动作没有编译。
有时,编译器会抱怨分配或类型不兼容,我不知道什么是错的。问题出现在两个主要方面:
编译器错误并不完全易处理,文档是错误的,或者我误解了它。
有没有办法找出完全什么精神进入我的语义行为?
struct mybase { int a,b; };
struct myderived : mybase { int c,d; };
BOOST_FUSION_ADAPT_STRUCT(mybase, (int,a)(int,b));
BOOST_FUSION_ADAPT_STRUCT(myderived, (int,a)(int,b)(int,c)(int,d));
auto base_expr = int_ >> int_; // avoids assigning to struct attribute
rule<decltype(f), mybase() , space_type> base_ = int_ >> int_;
rule<decltype(f), myderived(), space_type> derived_ = base_ >> int_ >> int_;
myderived data;
bool ok = phrase_parse(f,l,derived_,space,data);
此代码无法编译,存在大量难以理解的错误。
(从spirit-general列表中的帖子中轻松改编)
答案 0 :(得分:10)
我可以解决这个特殊情况的问题(事实上we discussed options on the list),但实际上,这种情况 使用Boost Spirit,'神秘'错误会更频繁地出现,这会很好 掌握一般问题。
您的第一个资源应该是优秀的精神文档,其中 详细说明给定解析器的合成属性 原语,运算符或指令。见the Reference section 到Spirit Qi Docs。
在某些情况下,我已经把注意力从“试图撬开”转移到了 来自编译器错误列表的信息'to'主动查询Spirit for the 它通过的类型'。我使用的技术是Polymorphic Callable Type (参见Spirit / Fusion文档)。
这是一个使用GCC特定的API来漂亮[原文如此]打印它检测到的类型:
what_is_the_attr
#include <cxxabi.h>
#include <stdlib.h>
#include <string>
#include <iostream>
template <typename T> std::string nameofType(const T& v) {
int status;
char *realname = abi::__cxa_demangle(typeid(v).name(), 0, 0, &status);
std::string name(realname? realname : "????");
free(realname);
return name;
}
struct what_is_the_attr {
template <typename> struct result { typedef bool type; };
template <typename T> bool operator()(T& attr) const {
std::cerr << "what_is_the_attr: " << nameofType(attr) << std::endl;
return true;
}
};
您可以使用它来准确检测合成属性类型的类型 解析器表达式实际上最终成为:
template <typename Exp>
void detect_attr_type(const Exp& exp)
{
using namespace boost::spirit::qi;
const char input[] = "1 2 3 4";
auto f(std::begin(input)), l(std::end(input)-1);
bool dummy = phrase_parse(
f, l,
exp [ what_is_the_attr() ],
space);
}
(注意: 这显示了该方法的局限性 - 该技术假设您 有一个'否则'工作语法,你知道如何传递输入 满足表达式足以触发语义动作。在多数情况下, 当你正在攻击你的灵魂解析器时,这将是真的,但)
我们来试试吧。例如。让我们看看它们之间的区别是什么
中等复杂度,同样包含在qi::raw[]
指令中:
int main()
{
detect_attr_type( -(int_ >> *int_) );
detect_attr_type( raw [ -(int_ >> *int_) ] );
}
输出:
what_is_the_attr: boost::optional<boost::fusion::vector2<int, std::vector<int, std::allocator<int> > > >
what_is_the_attr: boost::iterator_range<char const*>
在底部,我们会将其应用于OP中的问题。
我们可以使用相同的一元函数对象(what_is_the_attr
)来检测
但是,这些语义动作可以采用任意数量的参数,因此我们需要
概括。如果不是 可变参数,这将是乏味的工作
模板 (woot!for c ++ 0x):
struct what_are_the_arguments {
template <typename...> struct result { typedef bool type; };
template <typename... T> bool operator()(const T&... attr) const {
std::vector<std::string> names { nameofType(attr)... };
std::cerr << "what_are_the_arguments:\n\t";
std::copy(names.begin(), names.end(), std::ostream_iterator<std::string>(std::cerr, "\n\t"));
std::cerr << '\n';
return true;
}
};
对于上述测试案例的重复显示,Spirit实际上试图调用 如果可能,使用三个参数进行语义操作(如documented):
what_are_the_arguments:
boost::optional<boost::fusion::vector2<int, std::vector<int, std::allocator<int> > > >
boost::spirit::unused_type
bool
what_are_the_arguments:
boost::iterator_range<char const*>
boost::spirit::unused_type
bool
但好的是,您现在可以将其应用于任何语义操作:
template <typename ExpWSA> void test(const ExpWSA& exp)
{
const char input[] = "1 2 3 4";
auto f(std::begin(input)), l(std::end(input)-1);
qi::phrase_parse(f, l, exp, qi::space);
}
int main()
{
test(-(-double_ >> *int_) [ phx::bind(what_are_the_arguments(), _1, _2, _0, phx::ref(std::cout), 42) ]);
}
打印,为此(对不起)非常人为的例子:
what_are_the_arguments:
boost::optional<double>
std::vector<int, std::allocator<int> >
boost::fusion::vector2<boost::optional<double>, std::vector<int, std::allocator<int> > >
std::ostream
int
derived
规则的合成属性不与int_>>int_>>int_>>int_
相同:
auto base_expr = int_ >> int_; // avoids assigning to struct attribute
rule<const char*, mybase(), space_type> base_ = base_expr;
test(base_ >> int_ >> int_ [ what_is_the_attr() ] );
test(base_expr >> int_ >> int_ [ what_is_the_attr() ] );
将打印
what_is_the_attr: boost::fusion::vector3<mybase, int, int>
what_is_the_attr: boost::fusion::vector4<int, int, int, int>
有你的问题。我们在original thread中讨论了基于此诊断的一些变通方法(并在此处查看其他答案)。但这篇文章应该有助于回答一般案例问题。
以集成形式,使用gcc 4.6.1 --std = c ++ 0x和boost 1_48编译:
#include <cxxabi.h>
#include <iostream>
#include <iterator>
#include <stdlib.h>
#include <string>
#include <vector>
template <typename T> std::string nameofType(const T& v)
{
int status;
char *realname = abi::__cxa_demangle(typeid(v).name(), 0, 0, &status);
std::string name(realname? realname : "????");
free(realname);
return name;
}
struct what_is_the_attr {
template <typename> struct result { typedef bool type; };
template <typename T> bool operator()(T& attr) const {
std::cerr << "what_is_the_attr: " << nameofType(attr) << std::endl;
return true;
}
};
struct what_are_the_arguments {
template <typename...> struct result { typedef bool type; };
template <typename... T> bool operator()(const T&... attr) const {
std::vector<std::string> names { nameofType(attr)... };
std::cerr << "what_are_the_arguments:\n\t";
std::copy(names.begin(), names.end(), std::ostream_iterator<std::string>(std::cerr, "\n\t"));
std::cerr << '\n';
return true;
}
};
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
struct mybase { int a,b; };
struct myderived : mybase { int c,d; };
BOOST_FUSION_ADAPT_STRUCT(mybase, (int,a)(int,b));
BOOST_FUSION_ADAPT_STRUCT(myderived, (int,a)(int,b)(int,c)(int,d));
template <typename ExpWSA>
void test(const ExpWSA& exp)
{
using namespace boost::spirit::qi;
const char input[] = "1 2 3 4";
auto f(std::begin(input)), l(std::end(input)-1);
bool dummy = phrase_parse(f, l, exp, space);
}
int main()
{
using namespace boost::spirit::qi;
// Diagnostics for the OP case
auto base_expr = int_ >> int_; // avoids assigning to struct attribute
rule<const char*, mybase(), space_type> base_ = base_expr;
// Derived rule, different formulations
test((base_ >> int_ >> int_) [ what_is_the_attr() ] );
test((base_expr >> int_ >> int_) [ what_is_the_attr() ] );
// Applied to attribute types
test(raw [ -(int_ >> *int_) ] [ what_is_the_attr() ] );
test(-(int_ >> *int_) [ what_is_the_attr() ] );
// applied to semantic actions - contrived example
namespace phx = boost::phoenix;
test(-(-double_ >> *int_) [ phx::bind(what_are_the_arguments(), _1, _2, _0, phx::ref(std::cout), 42) ]);
return 0;
}
答案 1 :(得分:6)
为清楚起见 - 此处的错误是base_ >> int_ >> int_
被用作创建myderived
的规则的表达式,并且base_
被修复为mybase
类型,我们必须从myderrived
和两个mybase
创建一个int
,但没有什么可以告诉Spirit如何做到这一点。
通过定义一个可以接受任何参数的仿函数,你可以通过解析base_ >> int_ >> int_
来打印出boost所产生的值的类型,并告诉你它们是什么(下面的代码是从一些代码改编而来的) sehe穿上SO聊天):
struct what_is_the_attr
{
template <typename> struct result { typedef bool type; };
template <typename T>
static void print_the_type()
{
std::cout << " ";
std::cout << typeid(T).name();
if(std::is_const<typename std::remove_reference<T>::type>::value)
std::cout << " const";
if(std::is_rvalue_reference<T>::value)
std::cout << " &&";
else if(std::is_lvalue_reference<T>::value)
std::cout << " &";
}
template <typename Th, typename Th2, typename... Tt>
static void print_the_type()
{
print_the_type<Th>();
std::cout << ",\n";
print_the_type<Th2, Tt...>();
}
template <typename... Ts>
void operator()(Ts&&...) const
{
std::cout << "what_is_the_attr(\n";
print_the_type<Ts...>();
std::cout << ")" << std::endl;
}
};
然后使用它,在初始化程序的语义操作中使用上面的actor来处理错误的规则:
std::string input = "1 2 3 4";
auto f(std::begin(input)), l(std::end(input));
rule<decltype(f), mybase() , space_type> base_ = int_ >> int_;
rule<decltype(f), myderived(), space_type> derived_ = (base_ >> int_ >> int_)[what_is_the_attr()];
myderived data;
bool ok = phrase_parse(f,l,derived_,space,data);
注意,您不能将自动属性传播与%=
一起使用(除非您从规则的声明类型中删除公开的属性类型)。
然后运行此应该会产生一个编码类型,可以使用c++filt -t
进行解码: Live On Coliru
$ g++ 9404189.cpp -std=c++0x
$ ./a.out |c++filt -t
what_is_the_attr(
boost::fusion::vector3<mybase, int, int> &,
boost::spirit::context<boost::fusion::cons<boost::spirit::unused_type&, boost::fusion::nil>, boost::fusion::vector0<void> > &,
bool &)
第一行boost::fusion::vector3<mybase, int, int>
,至少告诉您,boost正在尝试从3个mybase
,int
和int
类型的对象创建返回类型。