|
In computer programming, the interpreter pattern is a particular design pattern. The basic idea is to implement a specialized computer language to rapidly solve a defined class of problems. Specialized languages often let a problem be solved several to several hundred times more quickly than a general purpose language would permit. Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. ...
In software engineering (or computer science), a design pattern is a general repeatable solution to a commonly occurring problem in software design. ...
...
The general idea is to have a class for each symbol (terminal or nonterminal) in the language. In object-oriented programming, a class is a programming language construct that is used to group related instance variables and methods. ...
A terminal symbol, in BNF jargon, is a symbol that represents a constant value. ...
A nonterminal symbol is that symbol which has the capability of being further defined in terms of terminals and/or non-terminals. ...
Uses for the Interpreter pattern
- Specialized database query languages such as SQL.
- Specialized computer languages which are often used to describe communication protocols.
- Most general-purpose computer languages actually incorporate several specialized languages.
This structural code demonstrates the Interpreter patterns, which using a defined grammer, provides the interpreter that processes parsed statements. This real-world code demonstrates the Interpreter pattern which is used to convert a Roman numeral to a decimal. // Interpreter pattern -- Real World example SQL (IPA: or IPA: ), commonly expanded as Structured Query Language, is a computer language designed for the retrieval and management of data in relational database management systems, database schema creation and modification, and database object access control management. ...
using System; using System.Collections; namespace DoFactory.GangOfFour.Interpreter.RealWorld { // MainApp test application class MainApp { static void Main() { string roman = "MCMXXVIII"; Context context = new Context(roman); // Build the 'parse tree' ArrayList tree = new ArrayList(); tree.Add(new ThousandExpression()); tree.Add(new HundredExpression()); tree.Add(new TenExpression()); tree.Add(new OneExpression()); // Interpret foreach (Expression exp in tree) { exp.Interpret(context); } Console.WriteLine("{0} = {1}", roman, context.Output); // Wait for user Console.Read(); } } // "Context" class Context { private string input; private int output; // Constructor public Context(string input) { this.input = input; } // Properties public string Input { get{ return input; } set{ input = value; } } public int Output { get{ return output; } set{ output = value; } } } // "AbstractExpression" abstract class Expression { public void Interpret(Context context) { if (context.Input.Length == 0) return; if (context.Input.StartsWith(Nine())) { context.Output += (9 * Multiplier()); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Four())) { context.Output += (4 * Multiplier()); context.Input = context.Input.Substring(2); } else if (context.Input.StartsWith(Five())) { context.Output += (5 * Multiplier()); context.Input = context.Input.Substring(1); } while (context.Input.StartsWith(One())) { context.Output += (1 * Multiplier()); context.Input = context.Input.Substring(1); } } public abstract string One(); public abstract string Four(); public abstract string Five(); public abstract string Nine(); public abstract int Multiplier(); } // Thousand checks for the Roman Numeral M // "TerminalExpression" class ThousandExpression : Expression { public override string One() { return "M"; } public override string Four(){ return " "; } public override string Five(){ return " "; } public override string Nine(){ return " "; } public override int Multiplier() { return 1000; } } // Hundred checks C, CD, D or CM // "TerminalExpression" class HundredExpression : Expression { public override string One() { return "C"; } public override string Four(){ return "CD"; } public override string Five(){ return "D"; } public override string Nine(){ return "CM"; } public override int Multiplier() { return 100; } } // Ten checks for X, XL, L and XC // "TerminalExpression" class TenExpression : Expression { public override string One() { return "X"; } public override string Four(){ return "XL"; } public override string Five(){ return "L"; } public override string Nine(){ return "XC"; } public override int Multiplier() { return 10; } } // One checks for I, II, III, IV, V, VI, VI, VII, VIII, IX // "TerminalExpression" class OneExpression : Expression { public override string One() { return "I"; } public override string Four(){ return "IV"; } public override string Five(){ return "V"; } public override string Nine(){ return "IX"; } public override int Multiplier() { return 1; } } }
Output MCMXXVIII = 1928 Adapted from: http://www.dofactory.com/Patterns/PatternInterpreter.aspx#_self1
Examples Java The following Java example illustrates how a general purpose language would interpret a more specialized language, here the Reverse Polish notation. The output is: Postfix notation is a mathematical notation wherein every operator follows all of its operands. ...
'42 2 1 - +' equals 43 import java.util.*; interface Expression { public void interpret(Stack<Integer> s); } class TerminalExpression_Number implements Expression { private int number; public TerminalExpression_Number(int number) { this.number = number; } public void interpret(Stack<Integer> s) { s.push(number); } } class TerminalExpression_Plus implements Expression { public void interpret(Stack<Integer> s) { s.push( s.pop() + s.pop() ); } } class TerminalExpression_Minus implements Expression { public void interpret(Stack<Integer> s) { int tmp = s.pop(); s.push( s.pop() - tmp ); } } class Parser { private ArrayList<Expression> parseTree = new ArrayList<Expression>(); // only one NonTerminal Expression here public Parser(String s) { for (String token : s.split(" ")) { if (token.equals("+")) parseTree.add( new TerminalExpression_Plus() ); else if (token.equals("-")) parseTree.add( new TerminalExpression_Minus() ); // ... else parseTree.add( new TerminalExpression_Number(Integer.valueOf(token)) ); } } public int evaluate() { Stack<Integer> context = new Stack<Integer>(); for (Expression e : parseTree) e.interpret(context); return context.pop(); } } class InterpreterExample { public static void main(String[] args) { String expression = "42 2 1 - +"; Parser p = new Parser(expression); System.out.println("'" + expression +"' equals " + p.evaluate()); } } See also | Design patterns in Design Patterns | Creational: Abstract factory • Builder • Factory • Prototype • Singleton The Backus-Naur form (BNF) (also known as Backus normal form) is a metasyntax used to express context-free grammars: that is, a formal way to describe formal languages. ...
...
This article is about the book by Gamma et al. ...
In software engineering (or computer science), a design pattern is a general repeatable solution to a commonly occurring problem in software design. ...
This article is about the book by Gamma et al. ...
A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. ...
Oftentimes, builder pattern builds Composite pattern, a structure pattern. ...
The factory method pattern is an object-oriented design pattern. ...
A prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. ...
In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. ...
Structural: Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy In computer programming, the adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) adapts one interface for a class into one that a client expects. ...
The bridge pattern is a design pattern used in software engineering which is meant to decouple an abstraction from its implementation so that the two can vary independently (Gamma et. ...
This does not cite any references or sources. ...
In object-oriented programming, a decorator pattern is a design pattern. ...
The façade pattern is an object-oriented design pattern. ...
Flyweight is a software design pattern. ...
// In computer programming, the proxy pattern is a software design pattern. ...
Behavorial: Chain of responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template method • Visitor In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. ...
In object-oriented programming, the Command pattern is a design pattern in which objects are used to represent actions. ...
In object-oriented programming, an iterator is an object allowing one to sequence through all of the elements or parts contained in some other object, typically a container or list. ...
The mediator pattern is a software design pattern that provides a unified interface to a set of interfaces in a subsystem. ...
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo by rollback). ...
The observer pattern (sometimes known as publish/subscribe) is a design pattern used in computer programming to observe the state of an object in a program. ...
A behavioral software design pattern, state pattern is used in computer programming to represent the state of an object. ...
In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime. ...
Template method: UML class diagram. ...
In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure. ...
| |