Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:

Similar presentations


Presentation on theme: "Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:"— Presentation transcript:

1 Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from: http://www.dofactory.com/Patterns/PatternInterpreter.aspx

2 Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language Motivation Interpreting and evaluating expressions governed by a rules of some language is a common programming problem e.g., arithmetic expressions, regular expressions This pattern provides a way to define the grammar for the language, represent sentences, and then interpret those sentences

3 Design Solution

4 Participants Client, context, expression Client typically calls an evaluate method on the context object The call, in turn, calls interpret on several expression objects, and culminates in a returned result The expression objects together represent the entire sentence, and hence are usually contained in another object

5 Examples Roman numeral parsing: From http://www.dofactory.com/Patterns/PatternInterpreter.aspx Simple arithmetic expression parsing: Reverse polish notation From http://en.wikipedia.org/wiki/Interpreter_pattern

6 Example: parsing arithmetic expressions Client (InterpreterDemo) creates Parser object using input sentence Client calls evaluate() on the Parser object Parser contains a context object (a Stack) and a list of Expression objects evaluate() calls interpret(context) on each Expression object Desired result is returned via the context object

7 Related Patterns The expression structure follows the Composite pattern


Download ppt "Interpreter CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns Some material taken from:"

Similar presentations


Ads by Google