You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a language, define a representation for its grammar along with an interpreter that uses the
12
-
representation to interpret sentences in the language.
17
+
The Interpreter pattern is used to define a grammatical representation for a language and provides an interpreter to deal with this grammar.
13
18
14
19
## Explanation
15
20
16
21
Real-world example
17
22
18
-
> The halfling kids are learning basic math at school. They start from the very basics "1 + 1",
19
-
> "4 - 2", "5 + 5", and so forth.
23
+
> Consider a calculator application designed to interpret and calculate expressions entered by users. The application uses the Interpreter pattern to parse and evaluate arithmetic expressions such as "5 + 3 * 2". Here, the Interpreter translates each part of the expression into objects that represent numbers and operations (like addition and multiplication). These objects follow a defined grammar that allows the application to understand and compute the result correctly based on the rules of arithmetic. Each element of the expression corresponds to a class in the program's structure, simplifying the parsing and evaluation process for any inputted arithmetic formula.
20
24
21
25
In plain words
22
26
23
-
> Interpreter pattern interprets sentences in the desired language.
27
+
> The Interpreter design pattern defines a representation for a language's grammar along with an interpreter that uses the representation to interpret sentences in the language.
24
28
25
29
Wikipedia says
26
30
27
-
> In computer programming, the interpreter pattern is a design pattern that specifies how to
28
-
> evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or
29
-
> nonterminal) in a specialized computer language. The syntax tree of a sentence in the language
30
-
> is an instance of the composite pattern and is used to evaluate (interpret) the sentence for
31
-
> a client.
31
+
> In computer programming, the interpreter pattern is a design pattern that specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialized computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence for a client.
32
32
33
33
**Programmatic example**
34
34
35
-
To be able to interpret basic math, we need a hierarchy of expressions. The basic abstraction for
36
-
it is the `Expression` class.
35
+
To be able to interpret basic math, we need a hierarchy of expressions. The basic abstraction for it is the `Expression` class.
37
36
38
37
```java
39
38
publicabstractclassExpression {
40
39
41
-
publicabstractintinterpret();
40
+
publicabstractintinterpret();
42
41
43
-
@Override
44
-
publicabstractStringtoString();
42
+
@Override
43
+
publicabstractStringtoString();
45
44
}
46
45
```
47
46
48
-
The simplest of the expressions is the `NumberExpression` that contains only a single integer
49
-
number.
47
+
The simplest of the expressions is the `NumberExpression` that contains only a single integer number.
50
48
51
49
```java
52
50
publicclassNumberExpressionextendsExpression {
53
51
54
-
privatefinalint number;
52
+
privatefinalint number;
55
53
56
-
publicNumberExpression(intnumber) {
57
-
this.number = number;
58
-
}
54
+
publicNumberExpression(intnumber) {
55
+
this.number = number;
56
+
}
59
57
60
-
publicNumberExpression(Strings) {
61
-
this.number =Integer.parseInt(s);
62
-
}
58
+
publicNumberExpression(Strings) {
59
+
this.number =Integer.parseInt(s);
60
+
}
63
61
64
-
@Override
65
-
publicintinterpret() {
66
-
return number;
67
-
}
62
+
@Override
63
+
publicintinterpret() {
64
+
return number;
65
+
}
68
66
69
-
@Override
70
-
publicStringtoString() {
71
-
return"number";
72
-
}
67
+
@Override
68
+
publicStringtoString() {
69
+
return"number";
70
+
}
73
71
}
74
72
```
75
73
76
-
The more complex expressions are operations such as `PlusExpression`, `MinusExpression`, and
77
-
`MultiplyExpression`. Here's the first of them, the others are similar.
74
+
The more complex expressions are operations such as `PlusExpression`, `MinusExpression`, and `MultiplyExpression`. Here's the first of them, the others are similar.
Use the Interpreter pattern when there is a language to interpret, and you can represent statements
159
-
in the language as abstract syntax trees. The Interpreter pattern works best when
154
+
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
160
155
161
-
* 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
162
-
* 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
156
+
* 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.
157
+
* 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.
* SQL parsers in various database management systems.
166
+
167
+
## Consequences
168
+
169
+
Benefits:
170
+
171
+
* Adds new operations to interpret expressions easily without changing the grammar or classes of data.
172
+
* Implements grammar directly in the language, making it easy to modify or extend.
173
+
174
+
Trade-offs:
175
+
176
+
* Can become complex and inefficient for large grammars.
177
+
* Each rule in the grammar results in a class, leading to a large number of classes for complex grammars.
178
+
179
+
## Related Patterns
170
180
181
+
*[Composite](https://java-design-patterns.com/patterns/composite/): Often used together, where the Interpreter pattern leverages the Composite pattern to represent the grammar as a tree structure.
182
+
*[Flyweight](https://java-design-patterns.com/patterns/flyweight/): Useful for sharing state to reduce memory usage in the Interpreter pattern, particularly for interpreters that deal with repetitive elements in a language.
171
183
172
184
## Credits
173
185
174
-
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
175
-
*[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
176
-
*[Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
186
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
187
+
*[Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
188
+
*[Refactoring to Patterns](https://amzn.to/3VOO4F5)
0 commit comments