gpt4 book ai didi

c++ - 使用自定义表达式类 boost Spirit 表达式解析器

转载 作者:行者123 更新时间:2023-11-30 05:44:45 25 4
gpt4 key购买 nike

<分区>

我正在开发一个测试项目,理想情况下,该项目将来会构建到一个更大的项目中(为了好玩而想要构建我自己的脚本语言),该项目使用我的自定义类将表达式解析为树结构。

我的预期结果是一个可以计算的 Expression,但实际结果是一个具有空 AbstractExpression 指针的表达式。

表达类

注意:State 是我计划用来保存变量和其他评估所需信息的结构

class AbstractExpression
{
public:
AbstractExpression()
{
}

virtual double const Evaluate(State &state) = 0;
virtual ~AbstractExpression() {}
};

class BinaryExpression : public AbstractExpression
{
public:
typedef double(*OperatorFunction)(double, double);

BinaryExpression(char op, AbstractExpression *leftOperand, AbstractExpression *rightOperand)
{
this->left = leftOperand;
this->right = rightOperand;
this->operatorFunction = GetFunction(std::string(1, op));
}

BinaryExpression(std::string op, AbstractExpression *leftOperand, AbstractExpression *rightOperand)
{
this->left = leftOperand;
this->right = rightOperand;
this->operatorFunction = GetFunction(op);
}

~BinaryExpression()
{
delete this->left;
delete this->right;
}

double const Evaluate(State &state)
{
return this->operatorFunction(this->left->Evaluate(state), this->right->Evaluate(state));
}

private:
AbstractExpression *left;
AbstractExpression *right;
OperatorFunction operatorFunction;

static double Add(double left, double right) { return left + right; }
//<... A bunch of definitions like the above ...>
static double NoOp(double left, double right) { return 0; }

static BinaryExpression::OperatorFunction GetFunction(std::string op)
{
if (op.compare("+") == 0)
{
return &Add;
}
//<... Lost of else if statements ...>
else
{
return &NoOp;
}
}
};

class ConstantExpression : public AbstractExpression
{
public:
ConstantExpression(double value)
{
this->value = value;
}

ConstantExpression()
{
this->value = 0;
}

double const Evaluate(State &state)
{
return this->value;
}

private:
double value;
};

class Expression : public AbstractExpression
{
private:
AbstractExpression *expression;

public:
Expression()
{
}

Expression(AbstractExpression *expression)
{
this->expression = expression;
}

~Expression()
{
//delete this->expression;
}

double const Evaluate(State &state)
{
return this->expression->Evaluate(state);
}
};

解析器

namespace Test
{
template <typename Iterator>
struct StatementGrammar : qi::grammar < Iterator, Expression(), ascii::space_type >
{
StatementGrammar() : StatementGrammar::base_type(expression)
{
expression =
constant[qi::_val = phoenix::construct<Expression>(qi::_1)]
;

constant =
qi::double_[qi::_val = &(phoenix::construct<ConstantExpression>(qi::_1))]
;
}

qi::rule < Iterator, Expression(), ascii::space_type > expression;
//qi::rule < Iterator, BinaryExpression*(), ascii::space_type> binaryExpression;
qi::rule < Iterator, ConstantExpression*(), ascii::space_type> constant;
};
}

下面是我用来调用解析器的代码:

std::string string;
Expression exp;

std::getline(std::cin, string);

using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;

Test::StatementGrammar<iterator_type> grammar;

std::string::const_iterator iter = string.begin();
std::string::const_iterator end = string.end();

bool result = qi::phrase_parse(iter, end, grammar, space, epx);

if (result)
{
State s;
double foo = exp.Evaluate(s);
}
else
{
std::cout << "No Match!" << std::endl;
}

return 0;

我主要是一名 C# 开发人员(阅读:我是一名 C++/Boost 新手)请纠正我在 C++ 中做错的所有事情(我需要了解我的错误所在!)。

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