1 / 17

Interpreter Pattern: A Completely Colorless PowerPoint Presentation

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.

lorenzon
Télécharger la présentation

Interpreter Pattern: A Completely Colorless PowerPoint Presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

  8. Look familiar?

  9. Component Operation() Leaf Composite Operation() Operation() Composite UML(Stuctural Pattern) Client

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

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

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

  13. BooleanExp Evaluate(Context) AndExp ConstantExp Evaluate(Context) Evaluate(Context) OrExp VariableExp NotExp Evaluate(Context) Evaluate(Context) Evaluate(Context) Boolean Expression InterpreterInterpretation  Evaluation Context Client

  14. BooleanExp Evaluate(Context) AndExp ConstantExp Evaluate(Context) Evaluate(Context) VariableExp NotExp Evaluate(Context) Evaluate(Context) Boolean Expression InterpreterInterpretation  Evaluation Context Client

  15. BooleanExp Replace() AndExp ConstantExp Replace() Replace() VariableExp NotExp Replace() Replace() Boolean Expression InterpreterInterpretation  Replacement Context Client

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

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

More Related