标签:
结构 | |
意图 | 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。 |
适用性 |
|
1 using System; 2 using System.Collections; 3 4 class Context 5 { 6 7 } 8 9 abstract class AbstractExpression 10 { 11 abstract public void Interpret(Context c); 12 } 13 14 // class for terminal symbol 15 class TerminalExpression : AbstractExpression 16 { 17 override public void Interpret(Context c) 18 { 19 20 } 21 } 22 23 // class for grammar rule (one per rule needed) 24 class NonterminalExpression : AbstractExpression 25 { 26 override public void Interpret(Context c) 27 { 28 29 } 30 } 31 // to extend grammar, just add other NonterminalExpression classes 32 33 /// <summary> 34 /// Summary description for Client. 35 /// </summary> 36 public class Client 37 { 38 public static int Main(string[] args) 39 { 40 Context c = new Context(); 41 ArrayList l = new ArrayList(); //really need a tree here! 42 43 // build up context information 44 // . . . 45 46 // Populate abstract syntax tree with data 47 l.Add(new TerminalExpression()); 48 l.Add(new NonterminalExpression()); 49 50 // interpret 51 foreach (AbstractExpression exp in l) 52 { 53 exp.Interpret(c); 54 } 55 56 return 0; 57 } 58 }
标签:
原文地址:http://www.cnblogs.com/ziranquliu/p/4666218.html