给定一个语言,定义它的文法表示,并定义一个解释器,这个解释器使用该标识来解释语言中的句子
解释器模式
1 2 3 4 5 6 7 8 9 10 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 36 37 38 39 40 41 42 43 44 45 46 47
| abstract class AbstractExpression { public abstract void Interpret(Context context); }
class TerminalExpression : AbstractExpression { public override void Interpret(Context context) { Console.WriteLine("终端解释器"); } }
class NonterminalExpression : AbstractExpression { public override void Interpret(Context context) { Console.WriteLine("非终端解释器") } } class Context { private string input; public string Input { get { return input; } set { input = value; } } private string output; public string Output { get { return output; } set { output = value; } } }
Context context = new Context() IList<AbstractExpression> list = new List<AbstractExpression>(); list.Add(new TerminalExpression()); list.Add(new NonterminalExpression()); list.Add(new TerminalExpression()); list.Add(new TerminalExpression()); foreach(AbstractExpression exp in list) { exp.Interpret(); }
|