|
| 1 | +# 解释器 |
| 2 | + |
| 3 | +给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子 |
| 4 | + |
| 5 | +## UML |
| 6 | + |
| 7 | +* AbstractExpression(抽象表达式): 声明了抽象的解释操作interpret(),是所有终结符表达式和非终结符表达式的基类 |
| 8 | +* TerminalExpression(终结符表达式): 终结符是文法规则的组成元素中最基本的语言单位,不能再分解 |
| 9 | +* NonterminalExpression(非终结符表达式): 实现了文法规则中非终结符的解释操作,因为非终结符表达式同样可以包含终结符表达式,所以终结符表达式可以是非终结符表达式的成员 |
| 10 | +* Context(环境类): 即上下文类,用于存储解释器之外的一些全局信息 |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## 优缺点 |
| 15 | + |
| 16 | +* 优点 |
| 17 | + * 易于改变和扩展文法,在解释器中使用类表示语言的文法规则,可以通过继承等机制类改变或扩展文法 |
| 18 | + * 每一条文法规则都可以表示为一个类,因此可以方便地实现一个简单的语言 |
| 19 | + * 如果要增加新的解释表达式,只需增加一个新的终结符表达式或非终结符表达式类,无需修改原有代码,符合开闭原则 |
| 20 | + |
| 21 | +* 缺点 |
| 22 | + * 对于复杂文法难以维护 |
| 23 | + * 执行效率低,因为解释器模式中有大量循环和递归调用 |
| 24 | + |
| 25 | + |
| 26 | +## 使用场景 |
| 27 | + |
| 28 | +* 一些重复出现的问题可以用一种简单的语言进行表达 |
| 29 | +* 一个语言的文法较为简单 |
| 30 | +* 不考虑执行效率的问题时可以使用解释器模式 |
| 31 | + |
| 32 | + |
| 33 | +## 用例 |
| 34 | + |
| 35 | +10进制的加减简单表达式: |
| 36 | + |
| 37 | +* 1 + 1 |
| 38 | +* 9 - 5 |
| 39 | +* 1 + 1 - 1 |
| 40 | + |
| 41 | + |
| 42 | +[code](../code/16_interpreter) |
| 43 | + |
| 44 | +```c++ |
| 45 | +// expression_type.h |
| 46 | +#pragma once |
| 47 | +#include <string> |
| 48 | +#include <iostream> |
| 49 | +#include <vector> |
| 50 | +using namespace std; |
| 51 | + |
| 52 | +class AbstractNode { |
| 53 | +public: |
| 54 | + virtual string interpret() = 0; |
| 55 | +}; |
| 56 | + |
| 57 | +class ValueNode : public AbstractNode { |
| 58 | +public: |
| 59 | + ValueNode() :m_val(0) {} |
| 60 | + ValueNode(int val) :m_val(val) {} |
| 61 | + string interpret() { |
| 62 | + return std::to_string(m_val); |
| 63 | + } |
| 64 | + |
| 65 | +private: |
| 66 | + int m_val; |
| 67 | +}; |
| 68 | + |
| 69 | +class OperationNode : public AbstractNode { |
| 70 | +public: |
| 71 | + OperationNode() :m_oper("+") {} |
| 72 | + OperationNode(string val) :m_oper(val) {} |
| 73 | + string interpret() { |
| 74 | + return m_oper; |
| 75 | + } |
| 76 | + |
| 77 | +private: |
| 78 | + string m_oper; |
| 79 | +}; |
| 80 | + |
| 81 | +class SimpleExpression :public AbstractNode { |
| 82 | +public: |
| 83 | + SimpleExpression() :m_leftNode(nullptr), m_operNode(nullptr), m_rightNode(nullptr) {} |
| 84 | + SimpleExpression(AbstractNode* left, AbstractNode* operNode, AbstractNode* right) : |
| 85 | + m_leftNode(left), m_operNode(operNode), m_rightNode(right) {} |
| 86 | + string interpret() { |
| 87 | + if (m_operNode && m_leftNode && m_rightNode) { |
| 88 | + string operation = m_operNode->interpret(); |
| 89 | + string valL = m_leftNode->interpret(); |
| 90 | + string valR = m_rightNode->interpret(); |
| 91 | + if (operation == "+") { |
| 92 | + return std::to_string(stoi(valL) + stoi(valR)); |
| 93 | + } |
| 94 | + else if (operation == "-") { |
| 95 | + return std::to_string((stoi(valL) - stoi(valR))); |
| 96 | + } |
| 97 | + } |
| 98 | + return "0"; |
| 99 | + } |
| 100 | +private: |
| 101 | + AbstractNode* m_leftNode, * m_rightNode, * m_operNode; |
| 102 | +}; |
| 103 | + |
| 104 | +class Context { |
| 105 | +public: |
| 106 | + Context(); |
| 107 | + Context* input(string expression); |
| 108 | + Context* interpret(); |
| 109 | + string output(); |
| 110 | + void showOutput(); |
| 111 | +private: |
| 112 | + void splitString(const string& s, vector<string>& v, const string& c); |
| 113 | + string m_input; |
| 114 | + string m_result; |
| 115 | +}; |
| 116 | +``` |
| 117 | +
|
| 118 | +```c++ |
| 119 | +// expression_type.cpp |
| 120 | +#include "expression_type.h" |
| 121 | +
|
| 122 | +
|
| 123 | +Context::Context() :m_input(""), m_result("") { |
| 124 | +
|
| 125 | +} |
| 126 | +
|
| 127 | +Context* Context::input(string input) { |
| 128 | + m_input = input; |
| 129 | + return this; |
| 130 | +} |
| 131 | +
|
| 132 | +Context* Context::interpret() { |
| 133 | + vector<string> list; |
| 134 | + splitString(m_input, list, " "); |
| 135 | + AbstractNode* nodes[4]; |
| 136 | + for (int i = 0; i < list.size() - 2; i += 2) { |
| 137 | + nodes[0] = new ValueNode(stoi(list.at(i))); |
| 138 | + nodes[1] = new OperationNode(list.at(i + 1)); |
| 139 | + nodes[2] = new ValueNode(stoi(list.at(i + 2))); |
| 140 | + nodes[3] = new SimpleExpression(nodes[0], nodes[1], nodes[2]); |
| 141 | + list[i + 2] = nodes[3]->interpret(); |
| 142 | + } |
| 143 | + this->m_result = list.back(); |
| 144 | + return this; |
| 145 | +} |
| 146 | +
|
| 147 | +string Context::output() { |
| 148 | + return m_result; |
| 149 | +} |
| 150 | +
|
| 151 | +void Context::showOutput() { |
| 152 | + cout << "Expression:" << this->m_input << " = " << output() << endl; |
| 153 | +} |
| 154 | +
|
| 155 | +void Context::splitString(const string& s, vector<string>& v, const string& c) |
| 156 | +{ |
| 157 | + string::size_type pos1, pos2; |
| 158 | + pos2 = s.find(c); |
| 159 | + pos1 = 0; |
| 160 | + while (string::npos != pos2) |
| 161 | + { |
| 162 | + v.push_back(s.substr(pos1, pos2 - pos1)); |
| 163 | +
|
| 164 | + pos1 = pos2 + c.size(); |
| 165 | + pos2 = s.find(c, pos1); |
| 166 | + } |
| 167 | + if (pos1 != s.length()) |
| 168 | + v.push_back(s.substr(pos1)); |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +```c++ |
| 173 | +// client.cpp |
| 174 | +#include "expression_type.h" |
| 175 | + |
| 176 | +int main() { |
| 177 | + Context ctx; |
| 178 | + ctx.input("1 + 1")->interpret()->showOutput(); |
| 179 | + ctx.input("9 - 5")->interpret()->showOutput(); |
| 180 | + ctx.input("1 + 1 - 1")->interpret()->showOutput(); |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | + |
0 commit comments