170 likes | 192 Views
Learn about the Interpreter pattern through a marginally relevant quote and a simple language example. Explore the UML diagrams and understand the context, client, and interpretation process.
E N D
(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
Structured Query Language (SQL) SELECT ingredient FROM food_group WHERE food_group_name IN (SELECT food_group_name FROM food_group WHERE ingredient='tomato') A Simple Language
AbstractExpression Interpret(Context) TerminalExpression NonterminalExpression Interpret(Context) Interpret(Context) Interpreter UML(Behavioral Pattern) Context Client
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.
AbstractExpression Interpret(Context) TerminalExpression NonterminalExpression Interpret(Context) Interpret(Context) Interpreter UML(Behavioral Pattern) Context Client
Component Operation() Leaf Composite Operation() Operation() Composite UML(Stuctural Pattern) Client
Context AbstractExpression Interpret(Context) TerminalExpression NonterminalExpression Interpret(Context) Interpret(Context) Interpreter UML(Behavioral Pattern) Client
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))
aVariableExpression aVariableExpression aConstantExpression aVariableExpression anAndExpression p p True q expression3 expression4 anAndExpression expression1: p expression2: q (True p)) aNotExpression anAndExpression expression7: p expression5 expression6 Abstract Syntax Treefor p (q (True p))
BooleanExp Evaluate(Context) AndExp ConstantExp Evaluate(Context) Evaluate(Context) OrExp VariableExp NotExp Evaluate(Context) Evaluate(Context) Evaluate(Context) Boolean Expression InterpreterInterpretation Evaluation Context Client
BooleanExp Evaluate(Context) AndExp ConstantExp Evaluate(Context) Evaluate(Context) VariableExp NotExp Evaluate(Context) Evaluate(Context) Boolean Expression InterpreterInterpretation Evaluation Context Client
BooleanExp Replace() AndExp ConstantExp Replace() Replace() VariableExp NotExp Replace() Replace() Boolean Expression InterpreterInterpretation Replacement Context Client
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?