2017-04-17 123 views
1

BNF I实施了一个有趣的规则,根据运营商的不同,这些条款可以是链接的或事件不符合此生产规则。因此,我使用同样的AST数据结构,因为仅枚举变化:boost.spirit x3 move_to and list ast member

#include <boost/spirit/home/x3.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 

#include <iostream> 
#include <string> 
#include <list> 


namespace ast 
{ 
    struct identifer { 
     int      name; 
    }; 
    struct expression { 
     struct chunk { 
      char    operator_; 
      ast::identifer  identifer; 
     }; 
     ast::identifer   identifer; 
     std::list<chunk>  chunk_list; 
    }; 
} 

BOOST_FUSION_ADAPT_STRUCT(ast::identifer, 
    name 
) 

BOOST_FUSION_ADAPT_STRUCT(ast::expression::chunk, 
    operator_, identifer 
) 

BOOST_FUSION_ADAPT_STRUCT(ast::expression, 
    identifer, chunk_list 
) 


namespace boost { namespace spirit { namespace x3 { namespace traits { 


void move_to(ast::expression::chunk&& chunk, std::list<ast::expression::chunk>& chunk_list, 
     mpl::identity<container_attribute>) 
{ 
    chunk_list.emplace(chunk_list.end(), std::move(chunk)); 
} 

} } } } 


namespace parser 
{ 
    namespace x3 = boost::spirit::x3; 

    auto const identifier = x3::rule<struct _, int> { "identifier" } = 
     x3::int_; 

    auto const operator_1 = x3::rule<struct _, char> { "operator" } = 
     x3::char_("ABC"); 
    auto const operator_2 = x3::rule<struct _, char> { "operator" } = 
     x3::char_("XYZ"); 

    auto const expression_chunk_1 = x3::rule<struct _, ast::expression::chunk> { "expression" } = 
     operator_1 > identifier 
     ; 
    auto const expression_chunk_2 = x3::rule<struct _, ast::expression::chunk> { "expression" } = 
     operator_2 > identifier 
     ; 
    auto const expression = x3::rule<struct _, ast::expression> { "expression" } = 
      identifier >> *expression_chunk_1 // foo { and foo } 
     // rule below fails to compile 
     | identifier >> expression_chunk_2 // foo [ nand foo ] 
     ; 
} 


struct visitor { 
    visitor(std::ostream& os) : os{ os } { } 
    void operator()(ast::expression const& node) { 
     os << "("; 
     (*this)(node.identifer); 
     for(auto const& chunk : node.chunk_list) { 
      os << "(" << chunk.operator_ << " "; 
      (*this)(chunk.identifer); 
      os << ")"; 
     } 
     os << ")\n"; 
    } 
    void operator()(ast::identifer const& node) { 
     os << "(" << node.name << ")"; 
    } 
    std::ostream& os; 
}; 


int main() 
{ 
    namespace x3 = boost::spirit::x3; 

    for(std::string const str: { 
     "1 X 2", 
     "3 A 4 A 5" 
    }) { 
     auto iter = str.begin(), end = str.end(); 

     ast::expression attr; 
     bool r = x3::phrase_parse(iter, end, parser::expression, x3::space, attr); 

     std::cout << "parse '" << str << "': "; 
     if (r && iter == end) { 
     std::cout << "succeeded:\n"; 
     visitor(std::cout)(attr); 

     } else { 
     std::cout << "*** failed ***\n"; 
     } 
    } 

    return 0; 
} 

这是想法 - 运营商X,Y,Z只增加一个块列出。编译器错误后,我必须专门化x3 :: traits :: move_to,但我没有找到任何解决方案来编译它。什么是要做的?是列表:: emplace()和std :: move()在这里安全吗?

回答

0

我会没有这个特质。相反,要人为地使用repeatvector<T>语法结果:

auto const expression = x3::rule<struct _, ast::expression> { "expression" } = 
     identifier >> *expression_chunk_1 // foo { and foo } 
    | identifier >> x3::repeat(1) [ expression_chunk_2 ] // foo [ nand foo ] 
    ; 
+0

谢谢 - 解决问题pragamtic方式。这些解决方案编译如[链接](http://coliru.stacked-crooked.com/a/898343ea7611a40a)所示,但解析失败。只有很小的变化(标识符现在是字符,规则具有唯一的名称)。通过运行... DEBUG,这是显而易见的,该标识符被消耗,并且expression_chunk_1必须失败。一种(可能的)方法是写 – Olx

+0

如果你告诉我“1×2”的预期结果,你可以在一个新问题中提出这个问题,我想。我看到那不会通过,但看着它,我没有看到为什么它应该通过语法。 – sehe

+0

一个解决问题的实用方法。这些解决方案编译如[链接](http://coliru.stacked-crooked.com/a/898343ea7611a40a)所示,但解析失败。只有很小的变化(标识符现在是字符,规则具有唯一的名称)。通过运行... DEBUG,这是显而易见的,该标识符被消耗,并且expression_chunk_1必须失败。一种(可能的)方式是编写 (标识>> >> expression_chunk_1) | (标识符>> x3 :: repeat(1)[expression_chunk_2]) 但由于回溯/展开标识符的迭代器,这并不聪明。 按照下一条评论... – Olx