定义一个符号,它可能是 boost spirit 中文字函数的一部分

Defining a symbol which may be part of a literal function in boost spirit

我正在尝试读取取决于符号 t 和 boost::spirit 的数学函数。

在下面的示例中,我正在尝试计算 t=1.2 中的函数 "tan(t)"。 而不是

Exit: 1, value = 2.5721 

我明白了

Exit: 1, value = 1.2 

我理解当我尝试读取函数 "tan(t)" 时,不是计算 t 的正切,而是将 t 的值分配给单词中的第一个字母tan。是否可以在不更改符号 t 的情况下规避此行为?而且,解析不应该失败吗?

#include <string>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <boost/phoenix/stl/cmath.hpp>

namespace qi = boost::spirit::qi;
namespace ascii=boost::spirit::ascii;
using boost::spirit::ascii::space;
using boost::spirit::qi::symbols;


template< typename Iterator >
struct Grammar : public qi::grammar<  Iterator, double(), ascii::space_type >
{

    Grammar() : Grammar::base_type(expression)
    {
        using qi::double_;
        using qi::_val;
        using qi::_1;

        expression = double_                   [_val = _1]
                    | symbol                   [_val = _1]
                    | function                 [_val = _1]
                    | group                    [_val = _1];

        function =  qi::lit("tan")  >> group   [_val = boost::phoenix::tan(_1)];
        group = '(' >> expression   [_val = _1] >> ')' ;
  }

    qi::rule<Iterator, double(), ascii::space_type> expression, function, group;
    qi::symbols<char, double > symbol;
};


int main()
{
    typedef std::string::iterator iterator;
    Grammar<iterator> grammar;
    std::string function = "tan(t)"; //it would work if function = "tan(x)"
    grammar.symbol.add("t",1.2);     // and add("x",1.2)
    double value;
    bool r = qi::phrase_parse(function.begin(), function.end(), grammar, space, value);
    std::cout << "Exit: " << r << ", value = " << value << std::endl;

    return 0;
}

您必须重新排序您的规则。您的符号 (t) 正在吃掉 tan 的第一个字母。所以,你实际上根本没有解析所有输入!

如果启用调试,您会看到以下输出:

<expression>
<try>tan(t)</try>
<success>an(t)</success>
<attributes>[1.2]</attributes>
</expression>
Exit: 1, value = 1.2

解决此问题的 "Royal Way" 是使用 Spirit Repository 中的 Qi Distinct 关键字指令:boost::spirit::qi keywords and identifiers

Live On Coliru

#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi_symbols.hpp>
#include <boost/spirit/repository/include/qi_distinct.hpp>
#include <boost/phoenix/stl/cmath.hpp>

namespace qi = boost::spirit::qi;
namespace ascii=boost::spirit::ascii;
using boost::spirit::ascii::space;
using boost::spirit::qi::symbols;


template< typename Iterator >
struct Grammar : public qi::grammar<  Iterator, double(), ascii::space_type >
{

    Grammar() : Grammar::base_type(expression)
    {
        using qi::double_;
        using qi::_val;
        using qi::_1;
        using boost::spirit::repository::qi::distinct;

        expression = double_
                   | distinct(qi::char_("a-zAZ09_")) [ symbol ]
                   | function
                   | group;

        function =  "tan"  >> group [_val = boost::phoenix::tan(_1)];
        group = '(' >> expression >> ')' ;

        BOOST_SPIRIT_DEBUG_NODES((expression)(function)(group));
  }

    qi::rule<Iterator, double(), ascii::space_type> expression, function, group;
    qi::symbols<char, double > symbol;
};


int main()
{
    typedef std::string::iterator iterator;
    Grammar<iterator> grammar;
    std::string function = "tan(t)";
    grammar.symbol.add("t",1.2);
    double value;
    bool r = qi::phrase_parse(function.begin(), function.end(), grammar, space, value);
    std::cout << "Exit: " << r << ", value = " << value << std::endl;

    return 0;
}

带有调试信息的输出是:

<expression>
  <try>tan(t)</try>
  <function>
    <try>tan(t)</try>
    <group>
      <try>(t)</try>
      <expression>
        <try>t)</try>
        <success>)</success>
        <attributes>[1.2]</attributes>
      </expression>
      <success></success>
      <attributes>[1.2]</attributes>
    </group>
    <success></success>
    <attributes>[2.57215]</attributes>
  </function>
  <success></success>
  <attributes>[2.57215]</attributes>
</expression>
Exit: 1, value = 2.57215