Interpreter Pattern
# Interpreter Pattern | Tutorial
## Interpreter Pattern
The Interpreter Pattern provides a way to evaluate the grammar or expressions of a language. It belongs to the behavioral pattern category.
The Interpreter Pattern defines a representation for a language's grammar and creates an interpreter that uses this representation to interpret sentences in the language.
This pattern is used in SQL parsing, symbol processing engines, and similar applications.
### Intent
Define a grammatical representation for a language and create an interpreter that can interpret sentences in that language.
### Main Problem Solved
* The Interpreter Pattern is used to build an interpreter that can interpret sentences of a specific language or grammar.
### Use Cases
* When a specific type of problem occurs frequently and can be expressed using a simple language.
### Implementation Approach
* **Define Grammar**: Clearly define the terminal and non-terminal symbols of the language.
* **Build Syntax Tree**: Construct the corresponding syntax tree structure based on the sentences of the language.
* **Create Context Class**: Contains global information needed during interpretation, typically a HashMap.
### Key Code
* **Terminal and Non-terminal Symbols**: Define the grammatical structure of the language.
* **Context Class**: Stores external environment information needed during interpretation.
### Application Examples
* **Compilers**: The Interpreter Pattern can be used in compiler design to interpret source code into target code.
* **Regular Expressions**: The Interpreter Pattern can be used to parse and execute regular expressions.
* **SQL Parsing**: The Interpreter Pattern can be used to parse and execute SQL statements.
### Advantages
* **Good Extensibility**: Easy to add new ways to interpret expressions.
* **Flexibility**: Grammar can be easily extended or modified as needed.
* **Easy to Implement Simple Grammar**: Relatively easy to implement for simple languages.
### Disadvantages
1. **Limited Use Cases**: Only suitable for simple grammars where interpretation is appropriate.
2. **Difficult Maintenance**: Maintenance and extension become challenging for complex grammars.
3. **Class Explosion**: May result in many classes, with each grammar rule corresponding to a class.
4. **Recursive Calls**: The Interpreter Pattern often uses recursive calls, which can be difficult to understand and trace.
### Usage Recommendations
* Consider using the Interpreter Pattern when you need to interpret and execute sentences in a language.
* Ensure the grammar is simple to avoid making the system overly complex.
### Notes
* The Interpreter Pattern may not be the first choice in Java. If a suitable scenario arises, consider using libraries like expression4J instead.
### Structure
The Interpreter Pattern includes the following main roles:
* **Abstract Expression**: Defines the abstract interface for the interpreter, declaring the method for the interpret operation, typically an abstract class or interface.
* **Terminal Expression**: Implements the abstract expression interface for terminal symbols (e.g., variables, constants) and implements the corresponding interpret operation.
* **Non-terminal Expression**: Implements the abstract expression interface for non-terminal symbols (e.g., sentences, expressions) and implements the corresponding interpret operation.
* **Context**: Contains some global information outside the interpreter, provided to the interpreter during the interpretation process, typically used to store variable values, save interpreter state, etc.
* **Client**: Creates and configures concrete interpreter objects and passes the expression to be interpreted to the interpreter for interpretation.
We will create an interface _Expression_ and concrete classes that implement the _Expression_ interface. Define the _TerminalExpression_ class as the main interpreter in the context. Other classes _OrExpression_ and _AndExpression_ are used to create composite expressions.
_InterpreterPatternDemo_, our demo class uses the _Expression_ class to create rules and demonstrate expression parsing.

### 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; } @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; 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); }}
## 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); }}
### Step 3
_InterpreterPatternDemo_ uses the _Expression_ class to create rules and parse them.
## InterpreterPatternDemo.java
public class InterpreterPatternDemo{public static Expression getMaleExpression(){Expression robert = new TerminalExpression("Robert"); Expression john = new TerminalExpression("John"); return new OrExpression(robert, john); }public static Expression getMarriedWomanExpression(){Expression julie = new TerminalExpression("Julie"); Expression married = new TerminalExpression("Married"); return new AndExpression(julie, married); }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 women? " + isMarriedWoman.interpret("Married Julie")); }}
### Step 4
Execute the program and output the results:
John is male? trueJulie is a married women? true
YouTip