语义操作未禁用属性兼容性

时间:2012-07-16 14:07:40

标签: boost-spirit boost-spirit-qi boost-spirit-lex

我的语法规则如下:

rule<Iterator, utree()> expr, factor;

expr =
    (
       +(
           (
              toks.symbol // toks.symbol attribute type is an std::string
              [
                 // I'm trying to force the attribute type to be utree
                 _val = construct<utree::list_type>()
                 // some code here
              ]
           |  (
                 factor >>
                +(
                    // the 3 tokens here have unsigned long attribute
                    toks.arrow >> factor
                 |  toks.squiggleArrow >> factor
                 |  toks.dot >> factor
                 )
              )
              [

                 // I'm trying to force the attribute type to be utree
                 _val = construct<utree::list_type>()
                 // some code here
              ]
           ) >> toks.assign // toks.assign is token_def<omit>
        ) >> factor
     ) [ _val = ternary(_1, _2) ]
   ;

据我所知:

http://boost-spirit.com/home/articles/attribute_handling/attribute-propagation-and-attribute-compatibility/

在这种情况下,应该禁用

属性兼容性,因为语义 有行动。尽管如此,我发现内部存在编译错误 ternary()表明_1的类型不是I的向量 会期待,但它是:

vector<
    variant<std::string,
            fusion::vector2<utree,
                            fusion::vector2<long unsigned int, utree>
                           >
           >
       >

这意味着由于某种原因,语义动作没有起作用!

任何暗示为什么会发生这种情况?

注意:我粘贴了一个显示问题的最小化示例:

http://pastebin.com/rgiy2QBW

谢谢!

1 个答案:

答案 0 :(得分:3)

编译器抱怨的赋值在ternaryImpl::operator()体内,这意味着显然语义操作确实起作用了!

现在,尽管SA是正确的,SA会阻止自动属性传播(除非运算符%=用于规则分配),但意味着原始解析器公开的类型会神奇地改变。< / p>

您在问题中列出的类型,准确反映了解析器表达式/运算符将返回的内容:

  • 或(|)解析变体
  • 序列(>>)解析为fusion::vector2<...>等。

现在,这是我进行编译的简单,最小的变化。诀窍是,通过使用显式属性类型将分割出,使属性赋值 for 。这允许Spirit为您进行属性转换。

struct Parser: public qi::grammar<Iterator, utree()>
{
    template <typename Tokens>
        Parser(const Tokens &toks):
            Parser::base_type(expression)
    {
        chain = +(
            (
               toks.symbol
               [
                  _val = construct<utree::list_type>()
                  // some code here
               ]
            |  (
                  factor >>
                 +(
                     toks.arrow >> factor
                  |  toks.dot >> factor
                  )
               )
               [
                  _val = construct<utree::list_type>()
                  // some code here
               ]
            ) >> toks.assign
         );
        expression = factor
            | (chain >> factor) [ _val = ternary(_1, _2) ]
            ;
    }

   rule<Iterator, utree::list_type()> chain;
   rule<Iterator, utree()> expression, factor, test;
};
  

注意如果您愿意,您应该能够在没有额外规则定义的情况下执行相同操作(使用qi::attr_cast<>qi::as<>例如),但我怀疑它会可读/可维护。

PS。类似的点可以从calc_utree_naive.cpp中删除,它必然会比使用SA的calc_utree_ast.cpp版本更明确的规则属性类型。

以下是完整的编译版本,其中包含注释中的一些内联注释:

// #define BOOST_SPIRIT_USE_PHOENIX_V3
// #define BOOST_RESULT_OF_USE_DECLTYPE
#include <algorithm>
#include <string>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>

namespace lex    = boost::spirit::lex;
namespace qi     = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace phx    = boost::phoenix;

using lex::token_def;
using qi::rule;
using qi::_1;
using qi::_2;
using qi::_val;
using spirit::utree;
using phx::construct;

// base iterator type
typedef std::string::iterator BaseIteratorT;

// token type
typedef lex::lexertl::token<BaseIteratorT, boost::mpl::vector</*double, int, */std::string> > TokenT;

// lexer type
typedef lex::lexertl::actor_lexer<TokenT> LexerT;

template <typename LexerT>
struct Tokens: public lex::lexer<LexerT>
{
   Tokens()
   {
      using lex::_pass;
      using lex::pass_flags;

      // literals
      symbol = "[a-zA-Z_?](\\w|\\?)*|@(\\w|\\?)+";
      arrow  = "->";
      dot    = '.';
      assign = "=";

      // literal rules
      this->self += symbol;
      this->self += arrow;
      this->self += dot;
      this->self += assign;
   }

   ~Tokens() {}

   // literal tokens
   token_def<std::string> symbol;
   token_def<> arrow, dot; // HINT: lex::omit here? 
   /*
    * ^ Otherwise, expect these to be all exposed as Qi attributes as well, so
    * _1, _2, _3, _4 a bit more than you'd expect
    */
   token_def<lex::omit> assign;
};

struct ternaryImpl
{
   template <typename Expr1Type, typename Expr2Type>
   struct result { typedef utree type; };

   template <typename Expr1Type, typename Expr2Type>
   utree operator()(Expr1Type &vec, Expr2Type &operand) const {
      utree ret;

      for (typename Expr1Type::iterator it = vec.begin(); it != vec.end(); ++it) {
         // some code
         ret = *it;
         // more code
      }

      // some code here

      return ret;
   }
};

phx::function<ternaryImpl> ternary = ternaryImpl();

template <typename Iterator>
struct Parser: public qi::grammar<Iterator, utree()>
{
    template <typename Tokens>
        Parser(const Tokens &toks):
            Parser::base_type(expression)
    {
        chain = +(
            (
               toks.symbol
               [
                  _val = construct<utree::list_type>()
                  // some code here
               ]
            |  (
                  factor >>
                 +(
                     toks.arrow >> factor
                  |  toks.dot >> factor
                  )
               )
               [
                  _val = construct<utree::list_type>()
                  // some code here
               ]
            ) >> toks.assign
         );
        expression = factor
            | (chain >> factor) [ _val = ternary(_1, _2) ]
            ;
    }

   rule<Iterator, utree::list_type()> chain;
   rule<Iterator, utree()> expression, factor, test;
};

int main()
{
   typedef Tokens<LexerT>::iterator_type IteratorT;

   Tokens<LexerT> l;
   Parser<IteratorT> p(l);
}