Interpreter Pattern
This chapter covers the interpreter pattern.
- PDF / 790,755 Bytes
- 19 Pages / 504 x 720 pts Page_size
- 81 Downloads / 204 Views
Interpreter Pattern This chapter covers the interpreter pattern.
GoF Definition Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
Concept To understand this pattern, you need to be familiar with some key terms, like sentences, grammar, languages, and so forth. So, you may need to visit the topics of formal languages in Automata, if you are not familiar with them. Normally, this pattern deals with how to evaluate sentences in a language. So, you first need to define a grammar to represent the language. Then the interpreter deals with that grammar. This pattern is best if the grammar is simple. Each class in this pattern may represent a rule in that language, and it should have a method to interpret an expression. So, to handle a greater number of rules, you need to create a greater number of classes. This is why an interpreter pattern should not be used to handle complex grammar. Let’s consider different arithmetic expressions in a calculator program. Though these expressions are different, they are all constructed using some basic rules, which are defined in the grammar of the language (of these arithmetic expressions). So, it is best if you can interpret a generic combination of these rules rather than treat each combination of rules as separate cases. An interpreter pattern can be used in such a scenario. A typical structure of this pattern is often described with a diagram similar to Figure 23-1. © Vaskaran Sarcar 2019 V. Sarcar, Java Design Patterns, https://doi.org/10.1007/978-1-4842-4078-6_23
389
Chapter 23
Interpreter Pattern
Figure 23-1. Structure of a typical interpreter pattern The terms are described as follows.
390
•
AbstractExpression: Typically an interface with an interpret method. You need to pass a context object to this method.
•
TerminalExpression: Used for terminal expressions. A terminal expression does not need other expressions to interpret. These are basically leaf nodes (i.e., they do not have child nodes) in the data structure.
•
NonterminalExpression: Used for nonterminal expressions. Also known as AlternationExpression, RepititionExpression, or SequenceExpression. These are like composites that can contain both the terminal and nonterminal expressions. When you call interpret() method on this, you basically call it on all of its children.
•
Context: Holds the global information that the interpreter needs.
•
Client: Calls the interpret() method. It can optionally build a syntax tree based on the rules of the language.
Chapter 23
Interpreter Pattern
Note An interpreter is used to process a language with simple rules or grammar. Ideally, developers do not want to create their own languages. This is the reason why they seldom use this pattern.
Real-World Example •
A translator who translates a foreign language.
•
Consider music notes as grammar, where musicians play the role of interpreters.
Computer-World Example •
Java compiler interprets the Java
Data Loading...