sitelink1 | |
---|---|
sitelink2 | |
sitelink3 | http://1 |
extra_vars4 | ko |
extra_vars5 | |
extra_vars6 | sitelink1 |
Interpreter pattern
This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (November 2008) |
In computer programming, the interpreter pattern is a particular design pattern. The interpreter pattern specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialized computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence.[1]
?
Contents[hide] |
[edit] 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.
[edit] Structure
[edit] Example
The Wikibook Computer Science Design Patterns has a page on the topic of |
The following Reverse Polish notation example illustrates the interpreter pattern. The grammarexpression ::= plus | minus | variable | number
defines a language which contains reverse polish expressions like:
plus ::= expression expression '+'
minus ::= expression expression '-'
variable ::= 'a' | 'b' | 'c' | ... | 'z'
digit = '0' | '1' | ... '9'
number ::= digit | digit numbera b +
Following the interpreter pattern there is a class for each grammar rule.
a b c + -
a b + c a - -
import java.util.HashMap; interface Expression { public int interpret(HashMap<String,Expression> variables); } class Number implements Expression { private int number; public Number(int number) { this.number = number; } public int interpret(HashMap<String,Expression> variables) { return number; } } class Plus implements Expression { Expression leftOperand; Expression rightOperand; public Plus(Expression left, Expression right) { leftOperand = left; rightOperand = right; } public int interpret(HashMap<String,Expression> variables) { return leftOperand.interpret(variables) + rightOperand.interpret(variables); } } class Minus implements Expression { Expression leftOperand; Expression rightOperand; public Minus(Expression left, Expression right) { leftOperand = left; rightOperand = right; } public int interpret(HashMap<String,Expression> variables) { return leftOperand.interpret(variables) - rightOperand.interpret(variables); } } class Variable implements Expression { private String name; public Variable(String name) { this.name = name; } public int interpret(HashMap<String,Expression> variables) { if(null==variables.get(name)) return 0; //Either return new Number(0). return variables.get(name).interpret(variables); } }
While the interpreter pattern does not address parsing[2] a parser is provided for completeness.
import java.util.HashMap; import java.util.Stack; class Evaluator implements Expression { private Expression syntaxTree; public Evaluator(String expression) { Stack<Expression> expressionStack = new Stack<Expression>(); for (String token : expression.split(" ")) { if (token.equals("+")) { Expression subExpression = new Plus(expressionStack.pop(), expressionStack.pop()); expressionStack.push( subExpression ); } else if (token.equals("-")) { // it's necessary remove first the right operand from the stack Expression right = expressionStack.pop(); // ..and after the left one Expression left = expressionStack.pop(); Expression subExpression = new Minus(left, right); expressionStack.push( subExpression ); } else expressionStack.push( new Variable(token) ); } syntaxTree = expressionStack.pop(); } public int interpret(HashMap<String,Expression> context) { return syntaxTree.interpret(context); } }
Finally evaluating the expression "w x z - +" with w = 5, x = 10, and z = 42.
import java.util.HashMap; public class InterpreterExample { public static void main(String[] args) { String expression = "w x z - +"; Evaluator sentence = new Evaluator(expression); HashMap<String,Expression> variables = new HashMap<String,Expression>(); variables.put("w", new Number(5)); variables.put("x", new Number(10)); variables.put("z", new Number(42)); int result = sentence.interpret(variables); System.out.println(result); } }
[edit] See also
[edit] References
- ^ Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. pp. 243 ISBN 0-201-63361-2
- ^ Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. pp. 247 ISBN 0-201-63361-2
[edit] External links
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
30 | The Facade Pattern | 황제낙엽 | 2017.01.04 | 312 |
29 | 디자인패턴[Observer 패턴] | 황제낙엽 | 2013.10.18 | 156 |
28 | AOP가 필요한 이유 | 황제낙엽 | 2008.10.08 | 187 |
27 | Core J2EE Patterns: Patterns index page | 황제낙엽 | 2005.11.14 | 14 |
26 | 쓴 자바"의 맛 (반 패턴으로 프로그래밍을 향상시키는 방법) | 황제낙엽 | 2007.10.03 | 728 |
25 | 해석자 (Interpreter) | 황제낙엽 | 2008.06.25 | 18 |
» | 해석자 (Interpreter) | 황제낙엽 | 2008.06.25 | 551 |
23 | 해석자 (Interpreter) | 황제낙엽 | 2008.06.25 | 43 |
22 | 해석자 (Interpreter) | 황제낙엽 | 2008.06.25 | 35 |
21 | EJB3의 Entity Access Object 패턴 | 황제낙엽 | 2008.04.10 | 35 |
20 | 비즈니스 패턴 | 황제낙엽 | 2008.04.10 | 21 |
19 | SOA Service Benefit Pattern | 황제낙엽 | 2008.04.10 | 8 |
18 | 패턴 입문가에게 권하는 책 - Head First Design Patterns (스토리가 있는 패턴 학습법) | 황제낙엽 | 2007.11.25 | 118 |
17 | 디자인 패턴(GoF) 카다로그 | 황제낙엽 | 2007.11.25 | 29 |
16 | 해석자(Interpreter) | 황제낙엽 | 2007.11.25 | 89 |
15 | 책임연쇄(Chain of Responsibility) 패턴 | 황제낙엽 | 2007.11.25 | 93 |
14 | 복합체(Composite) 패턴 | 황제낙엽 | 2007.11.25 | 69 |
13 | Singleton(싱글턴) 패턴 | 황제낙엽 | 2007.11.25 | 43 |
12 | Prototype(프로토 타입) 패턴 | 황제낙엽 | 2007.11.25 | 112 |
11 | Builder(빌더) 패턴 | 황제낙엽 | 2007.11.25 | 195 |