2016-10-09 13 views
3

現在、ブーストスピリットx3を使用してx3::variantを解析しようとしています。ブーストスピリットを使用して再帰バリアントに解析する問題x3

LambdaTypeとするClassTypeは以下のように見ている
typedef x3::variant< 
    nil, 
    x3::forward_ast<LambdaType>, 
    x3::forward_ast<ClassType> 
> Type 

struct LambdaType { 
     std::vector<Type> parameters_; 
     Type return_type_; 
    }; 

struct ClassType{ 
    std::vector<std::string> name_; 
    std::vector<Type> template_args_; 
}; 

私はタイプ、またはこれらの構造体のいずれかに解析しようとした場合、私は、コンパイラエラーを取得しています バリアントは次のようになります私にはconst boost::spirit::x3::char_classType、 に割り当てることはできません。私はconst boost::spirit::x3::char_classを割り当てたいとは考えていません。 ライブの例hereがあります。これにはパーサーがあり、コンパイルしようとすると問題とエラーが表示されます。 私はこの問題を1日中解決しようとしています。なぜ私はこの問題がどうして起こっているのか分かりません。 これに関する助けには、私は感謝以上のものになるでしょう。

答えて

3

x3::parseの最後の引数としてx3::spaceを渡すので、それをパーサーの属性にバインドします。

もちろん、あなたはそれを望んでいませんでした。

スキッパーを渡す場合は、x3::phrase_parseを使用してください。

PSあなたのパーサーには追加の問題があります。タイプ/ lambdaTypeに左回帰があります。私はあなたが(foo, foo) => fooする入力を修正した場合ので、ここだ(x3::string("foo"))あなたがパース識別子をスタブアウトするので、これは数える:

Live On Coliru

#define BOOST_SPIRIT_X3_DEBUG 
#include <iostream> 
#include <boost/spirit/home/x3.hpp> 
#include <boost/spirit/home/x3/support/ast/variant.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 

namespace x3 = boost::spirit::x3; 

namespace ast{ 
    struct LambdaType; 
    struct ClassType; 
    struct nil{}; 

    typedef x3::variant< 
     nil, 
     x3::forward_ast<LambdaType>, 
     x3::forward_ast<ClassType> 
    > Type; 

    struct LambdaType { 
     std::vector<Type> parameters_; 
     Type return_type_; 
    }; 

    struct ClassType{ 
     std::vector<std::string> name_; 
     std::vector<Type> template_args_; 
    }; 

} 

BOOST_FUSION_ADAPT_STRUCT(ast::LambdaType, parameters_, return_type_) 
BOOST_FUSION_ADAPT_STRUCT(ast::ClassType, name_, template_args_) 

namespace parser{ 
    typedef x3::rule<struct lambda_type_class, ast::LambdaType> lambda_type_type; 
    typedef x3::rule<struct class_type_class, ast::ClassType> class_type_type; 
    typedef x3::rule<struct type_class,  ast::Type>  type_type; 

    const class_type_type class_type = "class_type"; 
    const lambda_type_type lambda_type = "lambda_type"; 
    const type_type  type_p  = "type"; 

    auto const type_p_def = class_type | lambda_type; 

    auto const lambda_type_def = 
     ("(" >> -(type_p%",") >> ")" >> "=>" >> type_p) 
     | (x3::repeat(1)[type_p%","] >> "=>" >> type_p) 
      ; 

    auto const class_type_def = 
      (x3::string("foo")%"::") >> -("<" >> type_p%"," >> ">") 
      ; 

    BOOST_SPIRIT_DEFINE(
      lambda_type, 
      class_type, 
      type_p 
    ) 
} 

int main() 
{ 
    std::string input = "(foo, foo) => foo"; 
    x3::phrase_parse(input.begin(), input.end(), parser::type_p, x3::space); 
} 

デバッグ出力で

<type> 
    <try>(foo, foo) => foo</try> 
    <class_type> 
    <try>(foo, foo) => foo</try> 
    <fail/> 
    </class_type> 
    <lambda_type> 
    <try>(foo, foo) => foo</try> 
    <type> 
     <try>foo, foo) => foo</try> 
     <class_type> 
     <try>foo, foo) => foo</try> 
     <success>, foo) => foo</success> 
     <attributes>[[[f, o, o]], []]</attributes> 
     </class_type> 
     <success>, foo) => foo</success> 
     <attributes></attributes> 
    </type> 
    <type> 
     <try> foo) => foo</try> 
     <class_type> 
     <try> foo) => foo</try> 
     <success>) => foo</success> 
     <attributes>[[[f, o, o]], []]</attributes> 
     </class_type> 
     <success>) => foo</success> 
     <attributes></attributes> 
    </type> 
    <type> 
     <try> foo</try> 
     <class_type> 
     <try> foo</try> 
     <success></success> 
     </class_type> 
     <success></success> 
    </type> 
    <success></success> 
    </lambda_type> 
    <success></success> 
</type> 
+0

を追加しましたいくつかのヒントと[ライブサンプル](http://coliru.stacked-crooked.com/a/b6563a54ddc9048e) – sehe

+0

thats a joke is it: (私は今、とても馬鹿だと感じています。最後の3時間は何も成功せずにデバッグしていました。 私の目を開けて本当にありがとうございました – Exagon

+0

私はまた、左回帰を認識[ここ](http://stackoverflow.com/questions/39949480/boost-spirit-failing-to-parse-recursive-rule)は私の質問です私はこの問題を以前は扱っていませんでした。私は左回帰を避けるために苦労しています。 – Exagon

関連する問題