DesignPatterns_Interpreter

///
// Interpreter pattern
// - Given a language, define a represention for its grammar along with an interpreter
//   that uses the representation to interpret sentences in the language.
///
#include 
   
   
    
    
#include 
    
    
using namespace std;

// forward declaraction
class VariableExp;

// Context
// - contains information that's global to the interpreter
class Context
{
public:
	bool Lookup(char *name) const;
	void Assign(VariableExp *exp, bool b);

private:
	map
     
     
      
       _mapping;
};

// AbstractExpression
// - declares an abstract Interpret operation that is common to all nodes in the
//   abstract syntax tree.
class BooleanExp
{
public:
	virtual ~BooleanExp() { }
	virtual bool Evaluate(Context &c) = 0;
};

// TerminalExpression
// - implements an Interpret operation associated with terminal symbols in the
//   grammar.
// - an instance is required for every terminal symbol in a sentence.
class VariableExp : public BooleanExp
{
public:
	VariableExp(char *name) : _name(name) { }

	virtual bool Evaluate(Context &aContext)
	{
		return aContext.Lookup(_name);
	}

	const char *GetName()
	{
		return _name;
	}

private:
	char *_name;
};


bool Context::Lookup(char *name) const
{
	for (map
      
      
       
       ::const_iterator map_it = _mapping.begin(); 
		map_it != _mapping.end(); ++map_it)
	{
		if (strcmp(map_it->first->GetName(), name) == 0)
		{
			return map_it->second;
		}
	}
}

void Context::Assign(VariableExp *exp, bool b)
{
	_mapping.insert(std::pair
       
       
         (exp, b)); } // NonterminalExpression // - one such class is required for every rule R::=R1R2...Rn in the grammar. // - maintains instance variables of type AbstractExpression for each of the // symbols R1 through Rn. // - implements an Interpret operation for nonterminal symbols in the grammer. // Interpret typically calls itself recursively on the variables representing // R1 through Rn. class AndExp : public BooleanExp { public: AndExp(BooleanExp *op1, BooleanExp *op2) : _operand1(op1), _operand2(op2) { } virtual bool Evaluate(Context &aContext) { return _operand1->Evaluate(aContext) && _operand2->Evaluate(aContext); } private: BooleanExp *_operand1; BooleanExp *_operand2; }; class OrExp : public BooleanExp { public: OrExp(BooleanExp *op1, BooleanExp *op2) : _operand1(op1), _operand2(op2) { } virtual bool Evaluate(Context &aContext) { return _operand1->Evaluate(aContext) || _operand2->Evaluate(aContext); } private: BooleanExp *_operand1; BooleanExp *_operand2; }; class NotExp : public BooleanExp { public: NotExp(BooleanExp *op) : _operand(op) { } virtual bool Evaluate(Context &aContext) { return ~_operand->Evaluate(aContext); } private: BooleanExp *_operand; }; // Client // - builds(or is given) an abstract syntax tree representing a particular // sentence in the language that the grammar defines. The abstract syntax // tree is assembled from instance fo the NonterminalExpression and // TerminalExpression classes. // - invokes the Interpret operation. int main() { // VariableExp represents a named variable. VariableExp *x = new VariableExp("X"); VariableExp *y = new VariableExp("Y"); // boolean expression (x and y) or (x and (not y)) BooleanExp *expression = new OrExp( new AndExp(x, y), new AndExp(x, new NotExp(y)) ); Context context; context.Assign(x, true); context.Assign(y, false); bool result = expression->Evaluate(context); return 0; } 
       
      
      
     
     
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值