From 1f6833986e5738eba56fe43eac4f0349eccfa91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Mon, 14 Jun 2021 20:00:35 +0300 Subject: [PATCH] Interpreter explanation --- interpreter/README.md | 157 +++++++++++++++++- .../java/com/iluwatar/interpreter/App.java | 36 +++- 2 files changed, 175 insertions(+), 18 deletions(-) diff --git a/interpreter/README.md b/interpreter/README.md index 01053aaba..92172c70c 100644 --- a/interpreter/README.md +++ b/interpreter/README.md @@ -10,22 +10,161 @@ tags: --- ## Intent -Given a language, define a representation for its grammar along -with an interpreter that uses the representation to interpret sentences in the -language. + +Given a language, define a representation for its grammar along with an interpreter that uses the +representation to interpret sentences in the language. + +## Explanation + +Real-world example + +> The halfling kids are learning basic math at school. They start from the very basics "1 + 1", +> "4 - 2", "5 + 5", and so forth. + +In plain words + +> Interpreter pattern interprets sentences in the desired language. + +Wikipedia says + +> 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. + +**Programmatic example** + +To be able to interpret basic math, we need a hierarchy of expressions. The basic abstraction for +it is the `Expression` class. + +```java +public abstract class Expression { + + public abstract int interpret(); + + @Override + public abstract String toString(); +} +``` + +The simplest of the expressions is the `NumberExpression` that contains only a single integer +number. + +```java +public class NumberExpression extends Expression { + + private final int number; + + public NumberExpression(int number) { + this.number = number; + } + + public NumberExpression(String s) { + this.number = Integer.parseInt(s); + } + + @Override + public int interpret() { + return number; + } + + @Override + public String toString() { + return "number"; + } +} +``` + +The more complex expressions are operations such as `PlusExpression`, `MinusExpression`, and +`MultiplyExpression`. Here's the first of them, the others are similar. + +```java +public class PlusExpression extends Expression { + + private final Expression leftExpression; + private final Expression rightExpression; + + public PlusExpression(Expression leftExpression, Expression rightExpression) { + this.leftExpression = leftExpression; + this.rightExpression = rightExpression; + } + + @Override + public int interpret() { + return leftExpression.interpret() + rightExpression.interpret(); + } + + @Override + public String toString() { + return "+"; + } +} +``` + +Now we are able to show the interpreter pattern in action parsing some simple math. + +```java + // the halfling kids are learning some basic math at school + // define the math string we want to parse + final var tokenString = "4 3 2 - 1 + *"; + + // the stack holds the parsed expressions + var stack = new Stack(); + + // tokenize the string and go through them one by one + var tokenList = tokenString.split(" "); + for (var s : tokenList) { + if (isOperator(s)) { + // when an operator is encountered we expect that the numbers can be popped from the top of + // the stack + var rightExpression = stack.pop(); + var leftExpression = stack.pop(); + LOGGER.info("popped from stack left: {} right: {}", + leftExpression.interpret(), rightExpression.interpret()); + var operator = getOperatorInstance(s, leftExpression, rightExpression); + LOGGER.info("operator: {}", operator); + var result = operator.interpret(); + // the operation result is pushed on top of the stack + var resultExpression = new NumberExpression(result); + stack.push(resultExpression); + LOGGER.info("push result to stack: {}", resultExpression.interpret()); + } else { + // numbers are pushed on top of the stack + var i = new NumberExpression(s); + stack.push(i); + LOGGER.info("push to stack: {}", i.interpret()); + } + } + // in the end, the final result lies on top of the stack + LOGGER.info("result: {}", stack.pop().interpret()); +``` + +Executing the program produces the following console output. + +``` +popped from stack left: 1 right: 1 +operator: + +push result to stack: 2 +popped from stack left: 4 right: 2 +operator: * +push result to stack: 8 +result: 8 +``` ## Class diagram + ![alt text](./etc/interpreter_1.png "Interpreter") ## 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 +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 -## Real world examples +* 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 + +## Known uses * [java.util.Pattern](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html) * [java.text.Normalizer](http://docs.oracle.com/javase/8/docs/api/java/text/Normalizer.html) diff --git a/interpreter/src/main/java/com/iluwatar/interpreter/App.java b/interpreter/src/main/java/com/iluwatar/interpreter/App.java index ebff7b532..c4cfa2a49 100644 --- a/interpreter/src/main/java/com/iluwatar/interpreter/App.java +++ b/interpreter/src/main/java/com/iluwatar/interpreter/App.java @@ -34,25 +34,33 @@ import lombok.extern.slf4j.Slf4j; * *

In this example we use the Interpreter pattern to break sentences into expressions ({@link * Expression}) that can be evaluated and as a whole form the result. + * + *

Expressions can be evaluated using prefix, infix or postfix notations This sample uses + * postfix, where operator comes after the operands. + * */ @Slf4j public class App { /** * Program entry point. - * - *

Expressions can be evaluated using prefix, infix or postfix notations This sample uses - * postfix, where operator comes after the operands. - * - * @param args command line args + * @param args program arguments */ public static void main(String[] args) { - var tokenString = "4 3 2 - 1 + *"; + + // the halfling kids are learning some basic math at school + // define the math string we want to parse + final var tokenString = "4 3 2 - 1 + *"; + + // the stack holds the parsed expressions var stack = new Stack(); + // tokenize the string and go through them one by one var tokenList = tokenString.split(" "); for (var s : tokenList) { if (isOperator(s)) { + // when an operator is encountered we expect that the numbers can be popped from the top of + // the stack var rightExpression = stack.pop(); var leftExpression = stack.pop(); LOGGER.info("popped from stack left: {} right: {}", @@ -60,24 +68,36 @@ public class App { var operator = getOperatorInstance(s, leftExpression, rightExpression); LOGGER.info("operator: {}", operator); var result = operator.interpret(); + // the operation result is pushed on top of the stack var resultExpression = new NumberExpression(result); stack.push(resultExpression); LOGGER.info("push result to stack: {}", resultExpression.interpret()); } else { + // numbers are pushed on top of the stack var i = new NumberExpression(s); stack.push(i); LOGGER.info("push to stack: {}", i.interpret()); } } + // in the end, the final result lies on top of the stack LOGGER.info("result: {}", stack.pop().interpret()); } + /** + * Checks whether the input parameter is an operator. + * @param s input string + * @return true if the input parameter is an operator + */ public static boolean isOperator(String s) { return s.equals("+") || s.equals("-") || s.equals("*"); } /** - * Get expression for string. + * Returns correct expression based on the parameters. + * @param s input string + * @param left expression + * @param right expression + * @return expression */ public static Expression getOperatorInstance(String s, Expression left, Expression right) { switch (s) { @@ -85,8 +105,6 @@ public class App { return new PlusExpression(left, right); case "-": return new MinusExpression(left, right); - case "*": - return new MultiplyExpression(left, right); default: return new MultiplyExpression(left, right); }