• हिंदी (भारत)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • लॉगिन
  • पंजीकृत करें
DotNetAge - Mvc & jQuery CMS
साइडबार छुपाएँ

Interpreter


Definition



Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

UML class diagram





Participants



The classes and/or objects participating in this pattern are:
  • AbstractExpression - declares an interface for executing an operation
  • TerminalExpression
implements an Interpret operation associated with terminal symbols in the grammar.

an instance is required for every terminal symbol in the sentence.

  • 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 grammar. Interpret typically calls itself recursively on the variables representing R1 through Rn.

  • Context - contains information that is global to the interpreter
  • 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 instances of the NonterminalExpression and TerminalExpression classes invokes the Interpret operation

Sample code in C#




///
/// MainApp startup class for Structural
/// Interpreter Design Pattern.
///
class MainApp
{
///
/// Entry point into console application.
///
static void Main()
{
Context context = new Context();

// Usually a tree
ArrayList list = new ArrayList();

// Populate 'abstract syntax tree'
list.Add(new TerminalExpression());
list.Add(new NonterminalExpression());
list.Add(new TerminalExpression());
list.Add(new TerminalExpression());

// Interpret
foreach (AbstractExpression exp in list)
{
exp.Interpret(context);
}

// Wait for user
Console.ReadKey();
}
}

///
/// The 'Context' class
///
class Context
{
}

///
/// The 'AbstractExpression' abstract class
///
abstract class AbstractExpression
{
public abstract void Interpret(Context context);
}

///
/// The 'TerminalExpression' class
///
class TerminalExpression : AbstractExpression
{
public override void Interpret(Context context)
{
Console.WriteLine("Called Terminal.Interpret()");
}
}

///
/// The 'NonterminalExpression' class
///
class NonterminalExpression : AbstractExpression
{
public override void Interpret(Context context)
{
Console.WriteLine("Called Nonterminal.Interpret()");
}
}

 


    Average:
  • पुस्तकें
    (959)
  • कहते हैं:
को साझा करें:

Tag cloud

Anything in here will be replaced on browsers that support the canvas element