siteName > > interpreter
Threads
java ( 540116 ) - com.iluwatar.in ( 541874 ) stack: com.iluwatar.interpreter.App.main(App.java:50) org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:385) org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:374) org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:296) java.base/java.lang.Thread.run(Thread.java:840)
/*
 * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
 *
 * The MIT License
 * Copyright © 2014-2022 Ilkka Seppälä
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.iluwatar.interpreter;

import java.util.Stack;
import lombok.extern.slf4j.Slf4j;

/**
 * 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.
 *
 * <p>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.
 *
 * <p>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.
   * @param args program arguments
   */
  public static void main(String[] args) {

    // 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<Expression>();

    // 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());
  }

  /**
   * 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("*");
  }

  /**
   * 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) {
    return switch (s) {
      case "+" -> new PlusExpression(left, right);
      case "-" -> new MinusExpression(left, right);
      default -> new MultiplyExpression(left, right);
    };
  }
}
Variables All
No.FromNameValue
1class@50LOGGERLogger[com.iluwatar.interpreter.App]
250args[Ljava.lang.String;@54a17fd5
END 0 00
Output All Filter Merge
Process FilterThread Filter
540116 java 541874 com.iluwatar.in
No.PNPIDTIDTNTLMessage
1java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush to stack: 4
2java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush to stack: 3
3java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush to stack: 2
4java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpopped from stack left: 3 right: 2
5java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIoperator: -
6java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush result to stack: 1
7java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush to stack: 1
8java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpopped from stack left: 1 right: 1
9java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIoperator: +
10java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush result to stack: 2
11java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpopped from stack left: 4 right: 2
12java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIoperator: *
13java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIpush result to stack: 8
14java540116541874com.iluwatar.incom.iluwatar.interpreter.AppIresult: 8
END 0 0 0 0 0 00
Project:JavaDesignPatterns
Update:20240509
Commit:bf6456ba6
Source Code:interpreter
BuildTool:Java17
Compiler:Java17
Runtime:Openjdk17
System:MySystemD
Kernel:Linux5.10.211
Cpu:Intel:Corei7-7700K
Machine:AwesomeMachine