Interpreter Presented by Joey Richey T J Emond
Interpreter Presented by: Joey Richey T. J. Emond
Metsker’s Definition ● The intent of the Interpreter pattern is: – “Define a class hierarchy that represents a grammer - a set of composition rules - and define an operation throughout this hierarchy to interpret, or bring meaning to, instances of the set of possible compositions”
What it is good for ● A language with a simple grammar ● Efficency is not a prime concern ● Easy to change grammar ● Easy to implement ● Easily add new ways to interpret expressions
Regular Expression (Grammar) Expression : : = literal | alternation | sequence | repetition | '(' expression ')' alternation : : = expression '|' expression sequence : : = expression '&' expression repetition : : = expression '*' literal : = 'a' | 'b' | 'c' |. . . {'a' | 'b' | 'c' |. . . } *
Regular Expression (Class Diagram)
Regular Expression (Parse Tree) “ Raining & (dogs | cats) * ”
Ifs ● ● ● Term has eval() method that returns a Machine object Context maps machine names to instances of Machine eval() returning null represents false
Challenge ● Write code for While. Command. java
Solution
Something useful ● ● Has. Material’s eval() method again returns a Machine or null We can use a While. Command now
Challenge ● What is the difference, if any, between the Command Interpreter patterns?
Solution ● Command – – ● Attempt is to encapsulate a request in an object Done on a command-by-command basis Interpreter – – Attempt is to define classes that interpret an operation based on its composition Can create an infinite “language”
Conclusion Any Questions?
- Slides: 13