(A Completely Colorless
Powerpoint Presentation of)
The Interpreter Pattern
David Witonsky
A Marginally Relevant Quote
from a Really Smart Person
The common behavior of mankind is the
system of reference by means of which we
interpret an unknown language.
Ludwig Wittgenstein
Purpose of Pattern
For simple languages, the Interpreter pattern is used
to
• Define a language grammar
– Each grammar rule is represented by a class
• Represent sentences
– Sentences within language can be represented by
abstract syntax trees of instances of these classes
• Interpret sentences
A Simple Language
• Structured Query Language (SQL)
SELECT ingredient
FROM food_group
WHERE food_group_name IN
(SELECT food_group_name
FROM food_group
WHERE ingredient='tomato')
Interpreter UML
(Behavioral Pattern)
Context
Client
AbstractExpression
Interpret(Context)
TerminalExpression
NonterminalExpression
Interpret(Context)
Interpret(Context)
Participants
• AbstractExpression
– Declares an abstract Interpret operation that is common to all nodes in the
abstract syntax tree.
• Terminal Expression
– Implements an Interpret operation associated with terminal symbols in the
grammar.
• Nonterminal Expression
– Implements an Interpret operation for nonterminal symbols in the
grammar.
• Context
– Contains information that’s global to the interpreter
• Client
– Builds abstract syntax tree representing a particular sentence in the
language and invokes the interpret operation.
Interpreter UML
(Behavioral Pattern)
Context
Client
AbstractExpression
Interpret(Context)
TerminalExpression
NonterminalExpression
Interpret(Context)
Interpret(Context)
Look familiar?
Composite UML
(Stuctural Pattern)
Client
Component
Operation()
Leaf
Composite
Operation()
Operation()
Interpreter UML
(Behavioral Pattern)
Context
Client
AbstractExpression
Interpret(Context)
TerminalExpression
NonterminalExpression
Interpret(Context)
Interpret(Context)
Example:
Boolean Expression Interpreter
• Boolean Language Grammar
– Variable expressions: p, q, r, etc.
– Constant expressions: True and False
– Compound expressions:
• And expressions: p  q
• Not expressions: p
• Context
– Assignment of T or F to variables
• Sample sentence: p  (q  (True  p))
Abstract Syntax Tree
for p  (q  (True  p))
anAndExpression
expression1: p
expression2: q  (True  p))
aVariableExpression
p
anAndExpression
expression3
expression4
anAndExpression
aVariableExpression
expression5
expression6
q
aConstantExpression
True
aNotExpression
expression7: p
aVariableExpression
p
Boolean Expression Interpreter
Interpretation  Evaluation
Context
BooleanExp
Client
Evaluate(Context)
ConstantExp
AndExp
NotExp
Evaluate(Context)
Evaluate(Context)
Evaluate(Context)
VariableExp
OrExp
Evaluate(Context)
Evaluate(Context)
Boolean Expression Interpreter
Interpretation  Evaluation
Context
BooleanExp
Client
Evaluate(Context)
ConstantExp
AndExp
NotExp
Evaluate(Context)
Evaluate(Context)
Evaluate(Context)
VariableExp
Evaluate(Context)
Boolean Expression Interpreter
Interpretation  Replacement
Context
BooleanExp
Client
Replace()
ConstantExp
AndExp
NotExp
Replace()
Replace()
Replace()
VariableExp
Replace()
Three Pros and a Con
• Easy to extend grammar – new grammar rules can be
created simply by adding new classes
• Easy to change or add new interpretations – new
interpretations can be generated by defining new
operations on the expression classes
• Easy to implement grammar – all classes tend to have
similar implementations
• Hard to maintain complex grammars – one class per rule
means grammars with many rules are hard to manage and
maintain
A completely gratuitous, yet colorful, picture of my
daughter, Lilly
Any questions?
Descargar

Structure - University of Chicago