Class ExpressionParser


  • public class ExpressionParser
    extends java.lang.Object
    A parser for mathematical expressions, using Dijkstra's famous shunting-yard algorithm.

    It is important to note that this parser does not attempt to evaluate the expression in any way; rather, it only provides the parsed tree according to the desired operators.

    Author:
    Curtis Rueden
    • Constructor Summary

      Constructors 
      Constructor Description
      ExpressionParser()
      Creates an expression parser with the default set of operators.
      ExpressionParser​(java.util.Collection<? extends Operator> operators)
      Creates an expression parser with the given set of operators.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.LinkedList<java.lang.Object> parsePostfix​(java.lang.String expression)
      Parses the given mathematical expression into a queue in Reverse Polish notation (i.e., postfix notation).
      SyntaxTree parseTree​(java.lang.String expression)
      Parses the given mathematical expression into a syntax tree.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExpressionParser

        public ExpressionParser()
        Creates an expression parser with the default set of operators.
      • ExpressionParser

        public ExpressionParser​(java.util.Collection<? extends Operator> operators)
        Creates an expression parser with the given set of operators.
        Parameters:
        operators - The collection of operators available to expressions.
    • Method Detail

      • parseTree

        public SyntaxTree parseTree​(java.lang.String expression)
        Parses the given mathematical expression into a syntax tree.
        Parameters:
        expression - The mathematical expression to parse.
        Returns:
        Parsed hierarchy of tokens.
        Throws:
        java.lang.IllegalArgumentException - if the syntax of the expression is incorrect.
      • parsePostfix

        public java.util.LinkedList<java.lang.Object> parsePostfix​(java.lang.String expression)
        Parses the given mathematical expression into a queue in Reverse Polish notation (i.e., postfix notation).
        Parameters:
        expression - The mathematical expression to parse.
        Returns:
        Parsed queue of tokens in postfix notation.
        Throws:
        java.lang.IllegalArgumentException - if the syntax of the expression is incorrect.