Interpreter Pattern
The Interpreter Pattern (Interpreter Pattern) provides a way to evaluate the grammar or expressions of a language; it belongs to the behavioral pattern category.
Given a language, the Interpreter Pattern defines a representation of its grammar and defines an interpreter that uses the representation to interpret sentences in the language.
This pattern is used in SQL parsing, symbol processing engines, etc.
Introduction
Intent
Define a grammar 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 particular type of problem occurs frequently and instances of the problem can be expressed in a simple language.
Implementation
- Define the grammar: Clarify the terminal and non-terminal symbols of the language.
- Build the syntax tree: Build the corresponding syntax tree structure from the sentences of the language.
- Create the context class: Contains the global information needed during interpretation, usually a HashMap.
Key code
- Terminals and non-terminals: Define the grammatical structure of the language.
- Context class: Stores the external environment information needed during interpretation.
Application Example
- Compilers: The Interpreter Pattern can be used in compiler design, interpreting 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 of interpreting expressions.
- Flexibility: The grammar can be easily extended or modified as needed.
- Easy to implement simple grammars: For simple languages, implementation is relatively easy.
Disadvantages
- Limited use cases: Only suitable for simple grammars appropriate for interpretation.
- Difficult to maintain: For complex grammars, maintenance and extension become difficult.
- Class explosion: May produce many classes, with one class for each grammar rule.
- Recursive call: The Interpreter Pattern usually uses recursive calls, which can be difficult to understand and trace.
Usage recommendation
- When there is a need to interpret and execute sentences in a language, consider using the Interpreter Pattern.
- Keep the grammar simple to avoid the system becoming overly complex.
Notes
- The Interpreter Pattern may not be the first choice in Java; if a suitable scenario arises, consider using a library such as expression4J instead.
structures
The Interpreter Pattern contains the following main roles:
-
Abstract Expression (Abstract Expression): Defines the abstract interface of the interpreter, declares the method for the interpretation operation, usually an abstract class or interface.
-
Terminal Expression (Terminal Expression): The terminal expression class implements the abstract expression interface, is used to represent terminal symbols in the language (such as variables, constants, etc.), and implements the corresponding interpretation operation.
-
Non-terminal Expression (Non-terminal Expression): The non-terminal expression class implements the abstract expression interface, is used to represent non-terminal symbols in the language (such as sentences, expressions, etc.), and implements the corresponding interpretation operation.
-
Context (Context): Contains some global information outside the interpreter, provided to the interpreter during interpretation, usually used to store variable values, save interpreter state, etc.
-
Client: Creates and configures concrete interpreter objects, and passes the expressions to be interpreted to the interpreter for interpretation.
Create a class that implements
We will create an interface Expression and a concrete class that implements the Expression The entity class of the interface. Define the class that serves as the main interpreter in the context TerminalExpression class. Other classes OrExpression、AndExpression Used to create composite expressions.
InterpreterPatternDemo, and 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 the concrete class that implements the interface above.
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 Use Expression Class to create rules and parse them.
InterpreterPatternDemo.java
public
class
InterpreterPatternDemo
{
//
Rule: Robert and John are males
public
static
Expression
getMaleExpression
(
)
{
Expression
robert
=
new
TerminalExpression
(
"
Robert
"
)
;
Expression
john
=
new
TerminalExpression
(
"
John
"
)
;
return
new
OrExpression
(
robert
,
john
)
;
}
//
Rule: Julie is a married woman
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
Run the program, output result:
John is male? true
Julie is a married women? true