FACTOID # 121: Houses in English-speaking countries have the most rooms.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Interpreter pattern

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. ...

Contents

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


  Results from FactBites:
 
Interpreter pattern - Wikipedia, the free encyclopedia (196 words)
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.
The following Java example illustrates how a general purpose language would interpret a more specialized language, here the Reverse polish notation.
Interpreter - Wikipedia, the free encyclopedia (120 words)
Interpreter (communication), a person who facilitates dialogue between parties who use different languages.
Interpreter (computing), a program designed to run other non-executable programs directly.
Interpreter (history), a person who acts the role of a historical character in a living museum.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.