给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。文章来源地址https://uudwc.com/A/4PDNW
#include <iostream>
#include <string>
using namespace std;
class Context { };
class Expression {
public:
virtual ~Expression() { }
virtual void Interpret(const Context& c) = 0;
};
class TerminalExpression :public Expression {
public:
TerminalExpression(const string& statement) {
_statement = statement;
}
void Interpret(const Context& c) {
cout << this->_statement << " -- TerminalExpression" << endl;
}
private:
string _statement;
};
class NonterminalExpression :public Expression {
public:
NonterminalExpression(Expression* expression, int times) {
_expression = expression;
_times = times;
}
void Interpret(const Context& c) {
for (int i = 0; i < _times; i++) {
_expression->Interpret(c);
}
}
private:
Expression *_expression;
int _times;
};
int main() {
Context *c = new Context();
Expression *tp = new TerminalExpression("echo");
Expression *ntp = new NonterminalExpression(tp, 4);
ntp->Interpret(*c);
delete ntp;
delete tp;
delete c;
return 0;
}
文章来源:https://uudwc.com/A/4PDNW