Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interpreter Pattern.

Similar presentations


Presentation on theme: "Interpreter Pattern."— Presentation transcript:

1 Interpreter Pattern

2 Interpreter pattern provides a way to evaluate language grammar or expression. This type of pattern comes under behavioral pattern. This pattern involves implementing an expression interface which tells to interpret a particular context. This pattern is used in SQL parsing, symbol processing engine etc.

3 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 If a particular kind of problem occurs often enough, then it might be worth while to express instances of the problem as sentences in a simple language. Then you can build an interpreter that solves the problem by interpreting these sentences. For example, searching for strings that match a pattern is a common problem. Regular expressions are a standard language for specifying patterns of strings. Rather than building custom algorithms to match each pattern against strings, search algorithms could interpret a regular expression that specifies a set of strings to match.

4 Applicability Use the Interpreter pattern when there is a language to interpret, and you can represent statements in the language as abstract syntax trees. The Interpreter pattern works best when the grammar is simple. For complex grammars, the class hierarchy for the grammar becomes large and unmanageable. Tools such as parser generators are a better alternative in such cases. They can interpret expressions without building abstract syntax trees, which can save space and possibly time. efficiency is not a critical concern. The most efficient interpreters are usually not implemented by interpreting parse trees directly but by first translating them into another form. For example, regular expressions are often transformed into state machines. But even then, the translator can be implemented by the Interpreter pattern, so the pattern is still applicable.

5 Participants · AbstractExpression (RegularExpression) o declares an abstract Interpret operation that is common to all nodes in the abstract syntax tree. · TerminalExpression (LiteralExpression) o implements an Interpret operation associated with terminal symbols in the grammar. · NonterminalExpression (AlternationExpression,RepetitionExpression, SequenceExpressions) o one such class is required for every rule R ::= R1 R2 ... Rn in the grammar. · Context o contains information that's global to the interpreter. · Client o builds (or is given) an abstract syntax tree representing a particular sentence in the language that the grammar defines o invokes the Interpret operation.

6 Collaborations The client builds (or is given) the sentence as an abstract syntax tree of Non terminal Expression and Terminal Expression instances. Then the client initializes the context and invokes the Interpret operation. Each Non terminal Expression node defines Interpret in terms of Interpret on each sub expression. The Interpret operation of each Terminal Expression defines the base case in the recursion. The Interpret operations at each node use the context to store and access the state of the interpreter.

7 Consequences The Interpreter pattern has the following benefits and liabilities: It's easy to change and extend the grammar. Implementing the grammar is easy, too. Complex grammars are hard to maintain. Adding new ways to interpret expressions.

8 Implementation We are going to create an interface Expression and concrete classes implementing the Expression interface. A class Terminal Expression is defined which acts as a main interpreter of context in question. Other classes OrExpression, AndExpression are used to create combinational expressions. InterpreterPatternDemo, our demo class, will use Expression class to create rules and demonstrate parsing of expressions.

9

10 Step 1 Create an expression interface.
Expression.java public interface Expression { public boolean interpret(String context); } Step 2 Create concrete classes implementing the above interface. TerminalExpression.java public class TerminalExpression implements Expression private String data; public TerminalExpression(String data) this.data = data;

11 @Override public boolean interpret(String context) { if(context.contains(data)) return true; } return false; OrExpression.java public class OrExpression implements Expression private Expression expr1 = null; private Expression expr2 = null;

12 public OrExpression(Expression expr1, Expression expr2)
{ this.expr1 = expr1; this.expr2 = expr2; } @Override public boolean interpret(String context) return expr1.interpret(context) || expr2.interpret(context);

13 AndExpression.java public class AndExpression implements Expression { private Expression expr1 = null; private Expression expr2 = null; public AndExpression(Expression expr1, Expression expr2) this.expr1 = expr1; this.expr2 = expr2; } @Override public boolean interpret(String context) return expr1.interpret(context) && expr2.interpret(context);

14 Step 3 InterpreterPatternDemo uses Expression class to create rules and then parse them.
InterpreterPatternDemo.java public class InterpreterPatternDemo { //Rule: Robert and John are male public static Expression getMaleExpression() Expression robert = new TerminalExpression("Robert"); Expression john = new TerminalExpression("John"); return new OrExpression(robert, john); } //Rule: Julie is a married women public static Expression getMarriedWomanExpression() Expression julie = new TerminalExpression("Julie"); Expression married = new TerminalExpression("Married"); return new AndExpression(julie, married);

15 public static void main(String[] args)
{ Expression isMale = getMaleExpression(); Expression isMarriedWoman = getMarriedWomanExpression(); System.out.println("John is male? " + isMale.interpret("John")); System.out.println("Julie is a married woman? " + isMarriedWoman.interpret("Married Julie")); }

16 Step 4 Verify the output. John is male? true Julie is a married woman? true


Download ppt "Interpreter Pattern."

Similar presentations


Ads by Google