Search code examples
c++parsingboostboost-spiritboost-spirit-qi

Errors compiling very simple boost::spirit grammar


I've been working through the tutorials to Boost::Spirit and I'm trying to compile a super simple parser for adding numbers together, roughly based on the MiniXML example in the docs. I'm having trouble compiling in Xcode 5.1.1.

I've reduced it and found that even the definition of the grammar won't compile. Here is its current state:

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

The errors are long and detailed but uninformative starting with a parse issue 'Declaration of anonymous struct must be a definition'. The first section I'll paste here. (Note the line numbers don't match as I've omitted commented out code)

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

Can anyone spot what I've done wrong?

(Incidentally, I'm using Boost 1.53 due to an external library requirement.)


Solution

  • It seems you are missing some

    • includes (for the semantic actions)

      #include <boost/spirit/include/phoenix.hpp>
      
    • namespace aliases

      namespace ascii = boost::spirit::ascii;
      
    • using statements / qualifiers

          using qi::_val;
          using qi::_1;
          using qi::_2;
      

    See it Live On Coliru

    Note also that the [_val = _1] is redundant (automatic attribute propagation achieves the same).

    #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;
    }