网站宣传册,wordpress七牛远程图片,杜集网站建设,wordpress远程写作目录
解释器模式#xff08;Interpreter Pattern#xff09;
实际应用
算术表达式解释器
布尔表达式解释器
总结 解释器模式#xff08;Interpreter Pattern#xff09;
解释器模式是一种行为设计模式#xff0c;它定义了一种语言的文法表示#xff0c;并使用解释器…目录
解释器模式Interpreter Pattern
实际应用
算术表达式解释器
布尔表达式解释器
总结 解释器模式Interpreter Pattern
解释器模式是一种行为设计模式它定义了一种语言的文法表示并使用解释器来解释这些文法。该模式适用于那些有特定语法规则的场景比如编译器、正则表达式引擎和计算器。
实际应用
算术表达式解释器
算术表达式解释器 -- 可以解析和计算包含加法和减法的算术表达式。
#include iostream
#include string
#include stack
#include memory
#include unordered_map// 抽象表达式
class Expression {
public:virtual ~Expression() default;virtual int interpret(const std::unordered_mapchar, int context) 0;
};// 终结符表达式变量
class VariableExpression : public Expression {
private:char name;
public:VariableExpression(char name) : name(name) {}int interpret(const std::unordered_mapchar, int context) override {return context.at(name);}
};// 非终结符表达式加法
class AddExpression : public Expression {
private:std::shared_ptrExpression left, right;
public:AddExpression(std::shared_ptrExpression left, std::shared_ptrExpression right) : left(left), right(right) {}int interpret(const std::unordered_mapchar, int context) override {return left-interpret(context) right-interpret(context);}
};// 非终结符表达式减法
class SubtractExpression : public Expression {
private:std::shared_ptrExpression left, right;
public:SubtractExpression(std::shared_ptrExpression left, std::shared_ptrExpression right) : left(left), right(right) {}int interpret(const std::unordered_mapchar, int context) override {return left-interpret(context) - right-interpret(context);}
};// 客户端代码解析并计算表达式
int main() {std::string expr ab-c;std::unordered_mapchar, int context {{a, 5}, {b, 3}, {c, 2}};std::stackstd::shared_ptrExpression stack;for (char token : expr) {if (isalpha(token)) {stack.push(std::make_sharedVariableExpression(token));} else if (token ) {auto right stack.top(); stack.pop();auto left stack.top(); stack.pop();stack.push(std::make_sharedAddExpression(left, right));} else if (token -) {auto right stack.top(); stack.pop();auto left stack.top(); stack.pop();stack.push(std::make_sharedSubtractExpression(left, right));}}auto expression stack.top();int result expression-interpret(context);std::cout Result: result std::endl;return 0;
}布尔表达式解释器
布尔表达式解释器 -- 可以解析和计算包含与AND和或OR的布尔表达式。
#include iostream
#include string
#include stack
#include memory
#include unordered_map// 抽象表达式
class Expression {
public:virtual ~Expression() default;virtual bool interpret(const std::unordered_mapstd::string, bool context) 0;
};// 终结符表达式变量
class VariableExpression : public Expression {
private:std::string name;
public:VariableExpression(const std::string name) : name(name) {}bool interpret(const std::unordered_mapstd::string, bool context) override {return context.at(name);}
};// 非终结符表达式与操作
class AndExpression : public Expression {
private:std::shared_ptrExpression left, right;
public:AndExpression(std::shared_ptrExpression left, std::shared_ptrExpression right) : left(left), right(right) {}bool interpret(const std::unordered_mapstd::string, bool context) override {return left-interpret(context) right-interpret(context);}
};// 非终结符表达式或操作
class OrExpression : public Expression {
private:std::shared_ptrExpression left, right;
public:OrExpression(std::shared_ptrExpression left, std::shared_ptrExpression right) : left(left), right(right) {}bool interpret(const std::unordered_mapstd::string, bool context) override {return left-interpret(context) || right-interpret(context);}
};// 客户端代码解析并计算布尔表达式
int main() {std::string expr a AND b OR c;std::unordered_mapstd::string, bool context {{a, true}, {b, false}, {c, true}};std::stackstd::shared_ptrExpression stack;std::istringstream iss(expr);std::string token;while (iss token) {if (token a || token b || token c) {stack.push(std::make_sharedVariableExpression(token));} else if (token AND) {auto right stack.top(); stack.pop();auto left stack.top(); stack.pop();stack.push(std::make_sharedAndExpression(left, right));} else if (token OR) {auto right stack.top(); stack.pop();auto left stack.top(); stack.pop();stack.push(std::make_sharedOrExpression(left, right));}}auto expression stack.top();bool result expression-interpret(context);std::cout Result: std::boolalpha result std::endl;return 0;
}总结
解释器模式可以帮助我们定义和解释特定语言的语法规则并将这些规则应用于不同的上下文。