我一直在研究Boost :: Spirit的教程,我正在尝试编译一个超级简单的解析器,用于将数字加在一起,大致基于文档中的MiniXML example。我在Xcode 5.1.1中编译时遇到了麻烦。
我减少了它,发现即使是语法的定义也无法编译。这是它目前的状态:
Syntax.h:
#pragma once
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
template <typename Iterator>
struct ExpressionGrammer : qi::grammar<Iterator, double(), ascii::space_type>
{
qi::rule<Iterator, double(), ascii::space_type> expression;
qi::rule<Iterator, double(), ascii::space_type> addsub;
ExpressionGrammer()
: ExpressionGrammer::base_type(expression)
{
using qi::lit;
addsub = (expression >> '+' >> expression)[_val = _1 + _2];
expression = (addsub | qi::double_)[_val = _1];
}
};
Syntax.cpp:
#include "Syntax.h"
// Nothing else
错误很长且详细,但从解析问题开始是无法提供信息“匿名结构声明必须是一个定义”。第一节我将粘贴在这里。 (注意行号不匹配,因为我省略了注释掉的代码)
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:9:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.h:12:
In file included from ../../../Cinder/boost/boost/spirit/include/qi.hpp:16:
In file included from ../../../Cinder/boost/boost/spirit/home/qi.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/action.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/action/action.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/meta_compiler.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/support/meta_compiler.hpp:19:
In file included from ../../../Cinder/boost/boost/proto/proto.hpp:12:
In file included from ../../../Cinder/boost/boost/proto/core.hpp:21:
In file included from ../../../Cinder/boost/boost/proto/fusion.hpp:22:
In file included from ../../../Cinder/boost/boost/fusion/include/intrinsic.hpp:10:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/back.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/end.hpp:17:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/detail/segmented_end.hpp:10:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/detail/segmented_end_impl.hpp:14:
../../../Cinder/boost/boost/fusion/container/list/cons_fwd.hpp:13:5: error: declaration of anonymous struct must be a definition
struct nil;
^
../../../Cinder/boost/boost/fusion/container/list/cons_fwd.hpp:13:5: warning: declaration does not declare anything [-Wmissing-declarations]
struct nil;
^~~~~~
../../../Cinder/boost/boost/fusion/container/list/cons_fwd.hpp:15:44: error: expected a type
template <typename Car, typename Cdr = nil>
^
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:1:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/SpiritTest_Prefix.pch:8:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/AppBasic.h:27:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/App.h:27:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/Renderer.h:32:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:38:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h:68:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/MacTypes.h:90:17: note: expanded from macro 'nil'
#define nil NULL
^
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:1:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/SpiritTest_Prefix.pch:6:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/Cinder.h:92:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/chrono:280:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:203:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cstddef:44:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include/stddef.h:72:18: note: expanded from macro 'NULL'
# define NULL __null
^
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:9:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.h:12:
In file included from ../../../Cinder/boost/boost/spirit/include/qi.hpp:16:
In file included from ../../../Cinder/boost/boost/spirit/home/qi.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/action.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/action/action.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/meta_compiler.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/support/meta_compiler.hpp:19:
In file included from ../../../Cinder/boost/boost/proto/proto.hpp:12:
In file included from ../../../Cinder/boost/boost/proto/core.hpp:21:
In file included from ../../../Cinder/boost/boost/proto/fusion.hpp:22:
In file included from ../../../Cinder/boost/boost/fusion/include/intrinsic.hpp:10:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/back.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/end.hpp:17:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/detail/segmented_end.hpp:10:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/detail/segmented_end_impl.hpp:14:
../../../Cinder/boost/boost/fusion/container/list/cons_fwd.hpp:15:44: error: expected ',' or '>' in template-parameter-list
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:1:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/SpiritTest_Prefix.pch:8:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/AppBasic.h:27:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/App.h:27:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/Renderer.h:32:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:38:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h:68:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/MacTypes.h:90:17: note: expanded from macro 'nil'
#define nil NULL
^
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:1:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/SpiritTest_Prefix.pch:6:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/Cinder.h:92:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/chrono:280:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:203:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/cstddef:44:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include/stddef.h:72:18: note: expanded from macro 'NULL'
# define NULL __null
^
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:9:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.h:12:
In file included from ../../../Cinder/boost/boost/spirit/include/qi.hpp:16:
In file included from ../../../Cinder/boost/boost/spirit/home/qi.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/action.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/action/action.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/qi/meta_compiler.hpp:14:
In file included from ../../../Cinder/boost/boost/spirit/home/support/meta_compiler.hpp:19:
In file included from ../../../Cinder/boost/boost/proto/proto.hpp:12:
In file included from ../../../Cinder/boost/boost/proto/core.hpp:21:
In file included from ../../../Cinder/boost/boost/proto/fusion.hpp:22:
In file included from ../../../Cinder/boost/boost/fusion/include/intrinsic.hpp:10:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/back.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/end.hpp:17:
In file included from ../../../Cinder/boost/boost/fusion/sequence/intrinsic/detail/segmented_end.hpp:11:
In file included from ../../../Cinder/boost/boost/fusion/iterator/segmented_iterator.hpp:10:
In file included from ../../../Cinder/boost/boost/fusion/iterator/detail/segmented_iterator.hpp:18:
../../../Cinder/boost/boost/fusion/iterator/detail/segmented_equal_to.hpp:16:5: error: declaration of anonymous struct must be a definition
struct nil;
^
../../../Cinder/boost/boost/fusion/iterator/detail/segmented_equal_to.hpp:16:5: warning: declaration does not declare anything [-Wmissing-declarations]
struct nil;
^~~~~~
../../../Cinder/boost/boost/fusion/iterator/detail/segmented_equal_to.hpp:35:43: error: expected unqualified-id
struct segmented_equal_to<fusion::nil, fusion::nil>
^
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/Syntax.cpp:1:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/SpiritTest_Prefix.pch:8:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/AppBasic.h:27:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/App.h:27:
In file included from /Users/timmb/Documents/Programming/demos/SpiritTest/xcode/../../../Cinder/include/cinder/app/Renderer.h:32:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/ApplicationServices.framework/Headers/ApplicationServices.h:23:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreServices.framework/Headers/CoreServices.h:19:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h:38:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFBase.h:68:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/MacTypes.h:90:17: note: expanded from macro 'nil'
#define nil NULL
有人能发现我做错了吗?
(顺便说一句,由于外部库的要求,我使用的是Boost 1.53。)
答案 0 :(得分:1)
好像你错过了一些
包括(用于语义操作)
#include <boost/spirit/include/phoenix.hpp>
命名空间别名
namespace ascii = boost::spirit::ascii;
使用陈述/限定词
using qi::_val;
using qi::_1;
using qi::_2;
另请注意[_val = _1]
是冗余的(自动属性传播实现相同)。
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template <typename Iterator>
struct ExpressionGrammer : qi::grammar<Iterator, double(), ascii::space_type>
{
qi::rule<Iterator, double(), ascii::space_type> expression;
qi::rule<Iterator, double(), ascii::space_type> addsub;
ExpressionGrammer()
: ExpressionGrammer::base_type(expression)
{
using qi::lit;
using qi::_val;
using qi::_1;
using qi::_2;
addsub = (expression >> '+' >> expression)[_val = _1 + _2];
expression = (addsub | qi::double_)[_val = _1];
}
};
int main()
{
ExpressionGrammer<char*> g;
}