C++ Boost::spirit::qi解析器未使用整个字符串

C++ Boost::spirit::qi解析器未使用整个字符串,c++,boost,boost-spirit-qi,C++,Boost,Boost Spirit Qi,我正在为一个简单的计算器创建一个语法,但是我很难找出一个特定测试用例不起作用的原因。下面是我的解析器的一个功能示例: #include <iostream> #include <vector> #include <string> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi_char.hpp> #include <boost/spi

我正在为一个简单的计算器创建一个语法,但是我很难找出一个特定测试用例不起作用的原因。下面是我的解析器的一个功能示例:

#include <iostream>
#include <vector>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
using namespace boost::spirit;
using namespace boost::phoenix;
using std::endl;
using std::cout;
using std::string;
using std::vector;

void fPushOp(const string& op){
  cout << "PushOp: " << op << endl;
}

void fPushInt(string& my_str){
  cout << "PushInt: " << my_str << endl;
}

template<class Iterator>
struct Calculator : public qi::grammar<Iterator> {

    qi::rule<Iterator>  
      expression, logical_or_expression, logical_and_expression, negate_expression, series_expression,
      single_expression, inclusive_or_expression, exclusive_or_expression, and_expression, equality_expression, 
      relational_expression, shift_expression, additive_expression, multiplicative_expression, 
      term, complement_factor, factor, number, integer, variable, variable_combo, word, result;

    Calculator() : Calculator::base_type(result)
    {
          number = 
              lexeme[
                qi::as_string[
                    ("0x" >> +qi::char_("0-9a-fA-F"))     
                  | ("0b" >> +qi::char_("0-1"))
                  | ("0" >>  +qi::char_("0-7"))
                  | +qi::char_("0-9")
                ] [bind(&fPushInt, qi::_1)]
              ] 
             ;

          complement_factor = number
              | ('~' >> number)[bind(&fPushOp, "OP_COMPLEMENT")]
              | ('!' >> number)[bind(&fPushOp, "OP_NEGATE")];
              ;
          term = complement_factor
            >> *( (".." >> complement_factor)[bind(&fPushOp, "OP_LEGER")]
                | ('\\' >> complement_factor)[bind(&fPushOp, "OP_MASK")]
                ); 
          multiplicative_expression = term
            >> *( ('/' >> term)[bind(&fPushOp, "OP_DIV")]
                | ('%' >> term)[bind(&fPushOp, "OP_MOD")]
                | ('*' >> term)[bind(&fPushOp, "OP_MUL")]
                );
          additive_expression = multiplicative_expression
            >> *( ('+' >> multiplicative_expression)[bind(&fPushOp, "OP_ADD")]
                | ('-' >> multiplicative_expression)[bind(&fPushOp, "OP_SUB")]
                );
          shift_expression = additive_expression
            >> *( (">>" >> additive_expression)[bind(&fPushOp, "OP_SRL")]
                | ("<<" >> additive_expression)[bind(&fPushOp, "OP_SLL")]
                );
          relational_expression = shift_expression
            >> *( ('<' >> shift_expression)[bind(&fPushOp, "OP_LT")]
                | ('>' >> shift_expression)[bind(&fPushOp, "OP_GT")]
                | ("<=" >> shift_expression)[bind(&fPushOp, "OP_LET")]
                | (">=" >> shift_expression)[bind(&fPushOp, "OP_GET")]
                );
          equality_expression = relational_expression 
            >> *( ("==" >> relational_expression)[bind(&fPushOp, "OP_EQ")]
                | ("!=" >> relational_expression)[bind(&fPushOp, "OP_NEQ")] 
                );
          and_expression = equality_expression 
            >> *(('&' >> equality_expression)[bind(&fPushOp, "OP_AND")]); 
          exclusive_or_expression = and_expression 
            >> *(('^' >> and_expression)[bind(&fPushOp, "OP_XOR")]); 
          inclusive_or_expression = exclusive_or_expression 
            >> *(('|' >> exclusive_or_expression)[bind(&fPushOp, "OP_OR")]); 
          single_expression = inclusive_or_expression;
          series_expression = inclusive_or_expression 
            >> *((',' >> inclusive_or_expression)[bind(&fPushOp, "OP_SERIES")]);
          logical_and_expression = series_expression
            >> *(("&&" >> series_expression)[bind(&fPushOp, "OP_LOGICAL_AND")]); 
          logical_or_expression = logical_and_expression 
            >> *(("||" >> logical_and_expression)[bind(&fPushOp, "OP_LOGICAL_OR")]);
          expression = logical_or_expression;

          result = expression;
    }
};

int main(){
  Calculator<string::const_iterator> calc;
  const string expr("!3 && 0,1");
  string::const_iterator it = expr.begin();
  parse(it, expr.end(), calc, qi::space);
  cout << "Remaining: " << (string(it,expr.end())) << endl;

  return 0;
}
expr
时的电流输出!3&&0,1
似乎表明
和&0,1
没有被消费:

PushInt: 3
PushOp: OP_NEGATE
Remaining:  && 0,1

如果
expr
!3&&0,1
,那么它就可以正常工作了。调用
qi::parse
时使用了
qi::space
跳过程序,我看不出这两个字符串有什么不同。有人能告诉我这个问题吗?

您的规则没有声明船长:

qi::rule<Iterator>  
  • 传递skipper类型的实例时需要使用
    短语\u parse

    phrase_parse(it, expr.end(), calc, qi::space);
    
  • 固定代码 进一步说明:

    • 清理了包含项(更喜欢包含完整的
      phoenix.hpp
      ,因为如果缺少细微的位,您将被无法解释的错误所困扰。当然,如果您知道哪些位,可以通过选择性地包含子标题来减少编译时间)
    • 我强烈建议不要使用命名空间
      ,除非绝对必要。在这种情况下,您可以很容易地解决许多品牌的
      bind
      之间的混淆。而且,不,仅仅说使用boost::phoenix::ref
      是不够的,因为

      using boost::phoenix::ref;
      std::string s;
      bind(foo, ref(s))(); 
      
      由于ADL,最终使用的是
      std::ref
      ,而不是
      boost::phoenix::ref

    #包括
    #包括
    #包括
    #包括
    名称空间qi=boost::spirit::qi;
    名称空间phx=boost::phoenix;
    void fPushOp(const std::string&op){
    标准::cout+qi::字符(0-7)
    |+qi::字符(0-9)
    ][phx::bind(&fPushInt,qi::_1)]
    ] 
    ;
    补码系数=数字
    |('~'>>number)[phx::bind(&fPushOp,“OP_补码”)]
    |(“!”>>number)[phx::bind(&fPushOp,“OP_否定”)];
    ;
    术语=补码系数
    >>*((“.”>>补码因子)[phx::bind(&fPushOp,“OP_LEGER”)]
    |('\\'>>补码因子)[phx::bind(&fPushOp,“OP\u掩码”)]
    ); 
    乘法表达式=项
    >>*(('/'>>术语)[phx::bind(&fPushOp,“OP_DIV”)]
    |('%'>>术语)[phx::bind(&fPushOp,“OP_MOD”)]
    |('*'>>术语)[phx::bind(&fPushOp,“OP_MUL”)]
    );
    加法表达式=乘法表达式
    >>*(('+'>>乘法_表达式)[phx::bind(&fPushOp,“OP_ADD”)]
    |('-'>>乘法_表达式)[phx::bind(&fPushOp,“OP_SUB”)]
    );
    移位表达式=加法表达式
    >>*((“>>”>>加法表达式)[phx::bind(&fPushOp,“OP\u SRL”)]
    |(“=”>>shift_表达式)[phx::bind(&fPushOp,“OP_-GET”)]
    );
    等式表达式=关系表达式
    >>*((“=”>>关系表达式)[phx::bind(&fPushOp,“OP_EQ”)]
    |(!=“>>关系表达式)[phx::bind(&fPushOp,“OP\u NEQ”)]
    );
    和_表达式=相等_表达式
    >>*(“&'>>等式表达式)[phx::bind(&fPushOp,“OP_和”)];
    排他性_或_表达式=和_表达式
    >>*(“^'>>和_表达式)[phx::bind(&fPushOp,“OP_XOR”)];
    inclusive_或_表达式=exclusive_或_表达式
    >>*((“|”>>独占_或_表达式)[phx::bind(&fPushOp,“OP_或”)];
    单个_表达式=包含性_或_表达式;
    级数表达式=包含式表达式或表达式
    >>*((','>>包含式_或_表达式)[phx::bind(&fPushOp,“OP_系列”)];
    逻辑_和_表达式=系列_表达式
    >>*((“&&“>>系列_表达式)[phx::bind(&fPushOp,“OP_逻辑_和”)];
    逻辑_或_表达式=逻辑_和_表达式
    >>*((“||”>>逻辑_和_表达式)[phx::bind(&fPushOp,“OP|u逻辑_或”)];
    表达式=逻辑_或_表达式;
    结果=表达式;
    }
    };
    int main(){
    计算器计算器;
    常量std::字符串表达式(“!3&&0,1”);
    std::string::const_迭代器it=expr.begin();
    短语_parse(it,expr.end(),calc,qi::space);
    
    std::我能想到
    parse(it,expr.end(),calc,qi::space)中的
    qi::space
    吗
    做了同样的事情?它只是告诉解析器API要传递哪个Skipper实例,但语法忽略了它!我已经解释了需要修复的步骤,并发布了我似乎无法让您的工作示例编译的帖子?
    boost/spirit/home/qi/nonterminal/rule.hpp:303:错误:调用不匹配'(const Booo::FrimeI只能假设你没有复制/粘贴所有:)考虑再次复制,因为我改进了更多的东西。
    
    phrase_parse(it, expr.end(), calc, qi::space);
    
    using boost::phoenix::ref;
    std::string s;
    bind(foo, ref(s))(); 
    
    #include <iostream>
    #include <string>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    namespace qi = boost::spirit::qi;
    namespace phx = boost::phoenix;
    
    void fPushOp(const std::string& op){
        std::cout << "PushOp: " << op << std::endl;
    }
    
    void fPushInt(std::string& my_str){
        std::cout << "PushInt: " << my_str << std::endl;
    }
    
    template<class Iterator, typename Skipper = qi::space_type>
    struct Calculator : public qi::grammar<Iterator, Skipper> {
    
        qi::rule<Iterator, Skipper>  
          expression, logical_or_expression, logical_and_expression,
            negate_expression, series_expression, single_expression,
            inclusive_or_expression, exclusive_or_expression, and_expression,
            equality_expression, relational_expression, shift_expression,
            additive_expression, multiplicative_expression, term,
            complement_factor, factor, result;
    
        qi::rule<Iterator>  
            number, integer, variable, variable_combo, word;
    
        Calculator() : Calculator::base_type(result)
        {
            number = 
                qi::lexeme[
                  qi::as_string[
                      ("0x" >> +qi::char_("0-9a-fA-F"))     
                    | ("0b" >> +qi::char_("0-1"))
                    | ("0" >>  +qi::char_("0-7"))
                    | +qi::char_("0-9")
                  ] [phx::bind(&fPushInt, qi::_1)]
                ] 
               ;
    
            complement_factor = number
                | ('~' >> number)[phx::bind(&fPushOp, "OP_COMPLEMENT")]
                | ('!' >> number)[phx::bind(&fPushOp, "OP_NEGATE")];
                ;
            term = complement_factor
              >> *( (".." >> complement_factor)[phx::bind(&fPushOp, "OP_LEGER")]
                  | ('\\' >> complement_factor)[phx::bind(&fPushOp, "OP_MASK")]
                  ); 
            multiplicative_expression = term
              >> *( ('/' >> term)[phx::bind(&fPushOp, "OP_DIV")]
                  | ('%' >> term)[phx::bind(&fPushOp, "OP_MOD")]
                  | ('*' >> term)[phx::bind(&fPushOp, "OP_MUL")]
                  );
            additive_expression = multiplicative_expression
              >> *( ('+' >> multiplicative_expression)[phx::bind(&fPushOp, "OP_ADD")]
                  | ('-' >> multiplicative_expression)[phx::bind(&fPushOp, "OP_SUB")]
                  );
            shift_expression = additive_expression
              >> *( (">>" >> additive_expression)[phx::bind(&fPushOp, "OP_SRL")]
                  | ("<<" >> additive_expression)[phx::bind(&fPushOp, "OP_SLL")]
                  );
            relational_expression = shift_expression
              >> *( ('<' >> shift_expression)[phx::bind(&fPushOp, "OP_LT")]
                  | ('>' >> shift_expression)[phx::bind(&fPushOp, "OP_GT")]
                  | ("<=" >> shift_expression)[phx::bind(&fPushOp, "OP_LET")]
                  | (">=" >> shift_expression)[phx::bind(&fPushOp, "OP_GET")]
                  );
            equality_expression = relational_expression 
              >> *( ("==" >> relational_expression)[phx::bind(&fPushOp, "OP_EQ")]
                  | ("!=" >> relational_expression)[phx::bind(&fPushOp, "OP_NEQ")] 
                  );
            and_expression = equality_expression 
              >> *(('&' >> equality_expression)[phx::bind(&fPushOp, "OP_AND")]); 
            exclusive_or_expression = and_expression 
              >> *(('^' >> and_expression)[phx::bind(&fPushOp, "OP_XOR")]); 
            inclusive_or_expression = exclusive_or_expression 
              >> *(('|' >> exclusive_or_expression)[phx::bind(&fPushOp, "OP_OR")]); 
            single_expression = inclusive_or_expression;
            series_expression = inclusive_or_expression 
              >> *((',' >> inclusive_or_expression)[phx::bind(&fPushOp, "OP_SERIES")]);
            logical_and_expression = series_expression
              >> *(("&&" >> series_expression)[phx::bind(&fPushOp, "OP_LOGICAL_AND")]); 
            logical_or_expression = logical_and_expression 
              >> *(("||" >> logical_and_expression)[phx::bind(&fPushOp, "OP_LOGICAL_OR")]);
            expression = logical_or_expression;
    
            result = expression;
        }
    };
    
    int main(){
      Calculator<std::string::const_iterator> calc;
    
      const std::string expr("!3 && 0,1");
      std::string::const_iterator it = expr.begin();
    
      phrase_parse(it, expr.end(), calc, qi::space);
    
      std::cout << "Remaining: " << std::string(it,expr.end()) << std::endl;
    
      return 0;
    }