Presentation is loading. Please wait.

Presentation is loading. Please wait.

(A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky.

Similar presentations


Presentation on theme: "(A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky."— Presentation transcript:

1

2 (A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky

3 The common behavior of mankind is the system of reference by means of which we interpret an unknown language. Ludwig Wittgenstein A Marginally Relevant Quote from a Really Smart Person

4 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

5 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')

6 Interpreter UML (Behavioral Pattern) Client AbstractExpression Interpret(Context) TerminalExpression Interpret(Context) NonterminalExpression Interpret(Context) Context

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

8 Interpreter UML (Behavioral Pattern) Client AbstractExpression Interpret(Context) TerminalExpression Interpret(Context) NonterminalExpression Interpret(Context) Context

9 Look familiar?

10 Composite UML (Stuctural Pattern) Client Component Operation() Leaf Operation() Composite Operation()

11 Interpreter UML (Behavioral Pattern) Client AbstractExpression Interpret(Context) TerminalExpression Interpret(Context) NonterminalExpression Interpret(Context) Context

12 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))

13 Abstract Syntax Tree for p  (q  (True   p)) anAndExpression expression3 expression4 anAndExpression expression2: q  (True   p)) expression1: p aVariableExpression p anAndExpression expression5 expression6 aVariableExpression q aConstantExpression True aVariableExpression p aNotExpression expression7:  p

14 Boolean Expression Interpreter Interpretation  Evaluation Client BooleanExp Evaluate(Context) ConstantExp Evaluate(Context) Context AndExp Evaluate(Context) NotExp Evaluate(Context) VariableExp Evaluate(Context) OrExp

15 Boolean Expression Interpreter Interpretation  Evaluation Client BooleanExp Evaluate(Context) ConstantExp Evaluate(Context) Context AndExp Evaluate(Context) NotExp Evaluate(Context) VariableExp Evaluate(Context)

16 Boolean Expression Interpreter Interpretation  Replacement Client BooleanExp Replace() ConstantExp Replace() Context AndExp Replace() NotExp Replace() VariableExp Replace()

17 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

18 A completely gratuitous, yet colorful, picture of my daughter, Lilly Any questions?


Download ppt "(A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky."

Similar presentations


Ads by Google