gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:37 25 4
gpt4 key购买 nike

我正在尝试使用 boost::spirit 读取依赖于符号 t 的数学函数。

在下面的示例中,我正在尝试计算 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

解决此问题的“皇家之道”是使用来自 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

关于c++ - 定义一个符号,它可能是 boost spirit 中文字函数的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28370711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com