3

x3::variant現在、ブースト スピリット x3 を使用して に解析しようとしています。バリアントは次のようになります。

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

LambdaType と ClassType は次のようになります。

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、またはこれらの構造体の 1 つに解析しようとすると、コンパイラ エラーが発生しTypeますconst boost::spirit::x3::char_classここにライブの例があります。これにはパーサーがあり、コンパイルしようとすると問題とエラーが表示されます。私は一日中この問題を解決しようとしていますが、なぜこれがうまくいかないのか分かりません。これについて何か助けがあれば、私は感謝しています。

4

1 に答える 1

3

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

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

x3::phrase_parseスキッパーをパスしたい場合に使用します。

PS書かれているように、パーサーには追加の問題があります。タイプ/ラムダタイプに左再帰があります。これは、識別子の解析 ( ) をスタブ化したためだと思います。x3::string("foo")したがって、入力を次のように修正した場合は次のようになります(foo, foo) => 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>
于 2016-10-09T20:39:30.430 に答える