C++中的解释器模式

目录

解释器模式(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_map<char, int>& context) = 0;
};

// 终结符表达式(变量)
class VariableExpression : public Expression {
private:
    char name;
public:
    VariableExpression(char name) : name(name) {}
    int interpret(const std::unordered_map<char, int>& context) override {
        return context.at(name);
    }
};

// 非终结符表达式(加法)
class AddExpression : public Expression {
private:
    std::shared_ptr<Expression> left, right;
public:
    AddExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) 
        : left(left), right(right) {}
    int interpret(const std::unordered_map<char, int>& context) override {
        return left->interpret(context) + right->interpret(context);
    }
};

// 非终结符表达式(减法)
class SubtractExpression : public Expression {
private:
    std::shared_ptr<Expression> left, right;
public:
    SubtractExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) 
        : left(left), right(right) {}
    int interpret(const std::unordered_map<char, int>& context) override {
        return left->interpret(context) - right->interpret(context);
    }
};

// 客户端代码:解析并计算表达式
int main() {
    std::string expr = "a+b-c";
    std::unordered_map<char, int> context = {{'a', 5}, {'b', 3}, {'c', 2}};

    std::stack<std::shared_ptr<Expression>> stack;
    for (char token : expr) {
        if (isalpha(token)) {
            stack.push(std::make_shared<VariableExpression>(token));
        } else if (token == '+') {
            auto right = stack.top(); stack.pop();
            auto left = stack.top(); stack.pop();
            stack.push(std::make_shared<AddExpression>(left, right));
        } else if (token == '-') {
            auto right = stack.top(); stack.pop();
            auto left = stack.top(); stack.pop();
            stack.push(std::make_shared<SubtractExpression>(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_map<std::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_map<std::string, bool>& context) override {
        return context.at(name);
    }
};

// 非终结符表达式(与操作)
class AndExpression : public Expression {
private:
    std::shared_ptr<Expression> left, right;
public:
    AndExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) 
        : left(left), right(right) {}
    bool interpret(const std::unordered_map<std::string, bool>& context) override {
        return left->interpret(context) && right->interpret(context);
    }
};

// 非终结符表达式(或操作)
class OrExpression : public Expression {
private:
    std::shared_ptr<Expression> left, right;
public:
    OrExpression(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right) 
        : left(left), right(right) {}
    bool interpret(const std::unordered_map<std::string, bool>& context) override {
        return left->interpret(context) || right->interpret(context);
    }
};

// 客户端代码:解析并计算布尔表达式
int main() {
    std::string expr = "a AND b OR c";
    std::unordered_map<std::string, bool> context = {{"a", true}, {"b", false}, {"c", true}};

    std::stack<std::shared_ptr<Expression>> stack;
    std::istringstream iss(expr);
    std::string token;
    while (iss >> token) {
        if (token == "a" || token == "b" || token == "c") {
            stack.push(std::make_shared<VariableExpression>(token));
        } else if (token == "AND") {
            auto right = stack.top(); stack.pop();
            auto left = stack.top(); stack.pop();
            stack.push(std::make_shared<AndExpression>(left, right));
        } else if (token == "OR") {
            auto right = stack.top(); stack.pop();
            auto left = stack.top(); stack.pop();
            stack.push(std::make_shared<OrExpression>(left, right));
        }
    }

    auto expression = stack.top();
    bool result = expression->interpret(context);
    std::cout << "Result: " << std::boolalpha << result << std::endl;

    return 0;
}

总结

解释器模式可以帮助我们定义和解释特定语言的语法规则,并将这些规则应用于不同的上下文。

相关推荐

  1. C++解释模式

    2024-06-11 21:18:03       35 阅读
  2. C++装饰模式

    2024-06-11 21:18:03       25 阅读
  3. C++ QT设计模式解释模式

    2024-06-11 21:18:03       34 阅读
  4. 解释模式(极简c++)》

    2024-06-11 21:18:03       40 阅读
  5. 解释模式(大话设计模式)C/C++版本

    2024-06-11 21:18:03       21 阅读
  6. 突破编程_C++_设计模式解释模式

    2024-06-11 21:18:03       31 阅读
  7. Linux C++ 058-设计模式解释模式

    2024-06-11 21:18:03       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 21:18:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 21:18:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 21:18:03       82 阅读
  4. Python语言-面向对象

    2024-06-11 21:18:03       91 阅读

热门阅读

  1. 乘船过河(ship)

    2024-06-11 21:18:03       34 阅读
  2. Canny边缘算法总结(基于C语言)

    2024-06-11 21:18:03       35 阅读
  3. Poco 使用 use关键字时的注意事项

    2024-06-11 21:18:03       36 阅读
  4. 数据仓库之拉链表

    2024-06-11 21:18:03       26 阅读
  5. 爬山算法介绍(极简)

    2024-06-11 21:18:03       33 阅读
  6. mysql安装配置教程(Linux+Windows)

    2024-06-11 21:18:03       32 阅读
  7. 盛水最多的容器

    2024-06-11 21:18:03       26 阅读
  8. dpkg安装包打包器介绍

    2024-06-11 21:18:03       27 阅读
  9. 算法之链表知识

    2024-06-11 21:18:03       34 阅读