Isis 3 Programmer Reference
Isis::InlineInfixToPostfix Class Reference

A parser for converting equation strings to postfix. More...

#include <InlineInfixToPostfix.h>

Inheritance diagram for Isis::InlineInfixToPostfix:
Inheritance graph
Collaboration diagram for Isis::InlineInfixToPostfix:
Collaboration graph

Public Member Functions

 InlineInfixToPostfix ()
 Constructs an InlineInfixToPostfix object. More...
 
virtual ~InlineInfixToPostfix ()
 Destroys the InlineInfixToPostfix object. More...
 
QString convert (const QString &infix)
 This method converts infix to postfix. More...
 
QString tokenizeEquation (const QString &equation)
 This method will add spaces between all operators and numbers, making it possible to get each element of the equation one by one. More...
 

Protected Member Functions

virtual bool isKnownSymbol (QString representation)
 This method attempts to verify that the given argument is recognized as a valid function, operator, scalar, or variable. More...
 
virtual InfixOperatorfindOperator (QString element)
 This method will first search the recognized list of operators and functions for the given token. More...
 

Protected Attributes

QList< InfixOperator * > p_operators
 

Private Member Functions

void initialize ()
 Adds several infix operators and functions to the operator list that are not already recognized by the parent class, InfixToPostfix. More...
 
bool exists (const QString &str)
 Determines whether the given string exists as a recognized variable. More...
 
bool isScalar (const QString &scalar)
 Determines whether the given token represents a scalar value (i.e. More...
 
bool isVariable (const QString &str)
 Determines whether the given token is a variable and, if so, appends it to the list of variables. More...
 
void uninitialize ()
 This cleans the known operators/functions list. More...
 
QString formatFunctionCalls (QString equation)
 This method looks through equation for function calls, parenthesizes them, and calls itself again with each argument in order to parse any embedded functions. More...
 
QString cleanSpaces (QString equation)
 This function takes a space-delimited string and removes empty delimiters. More...
 
void closeParenthesis (QString &postfix, std::stack< InfixOperator > &theStack)
 This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore. More...
 
void addOperator (QString &postfix, const InfixOperator &op, std::stack< InfixOperator > &theStack)
 This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore. More...
 
bool isFunction (QString representation)
 This method will return true if 'representation' is a known function. More...
 
void checkArgument (QString funcName, int argNum, QString argument)
 

Private Attributes

QStringList m_variables
 The list of variables (represented as strings). More...
 

Detailed Description

A parser for converting equation strings to postfix.

This class converts infix equations to postfix for parsing.

Author
2012-07-15 Kris Becker
History:
2012-07-15 Kris Becker - Original version.
History:
2015-03-18 Jeannie Backer - Brought class files closer to ISIS coding standards.
History:
2015-03-24 Jeffrey Covington and Jeannie Backer - Improved documentation.
History:
2015-03-24 Jeannie Backer - Changed m_variables from a QVector to QStringList
History:
2016-02-21 Kristin Berry - Added unitTest.
History:
2017-01-09 Jesse Mapel - Added logical and, or operators. Fixes #4581.

Definition at line 34 of file InlineInfixToPostfix.h.

Constructor & Destructor Documentation

◆ InlineInfixToPostfix()

Isis::InlineInfixToPostfix::InlineInfixToPostfix ( )

Constructs an InlineInfixToPostfix object.

The operators list is filled with string representations of known symbols, recognized by both this class and the parent class.

Definition at line 24 of file InlineInfixToPostfix.cpp.

References initialize().

◆ ~InlineInfixToPostfix()

Isis::InlineInfixToPostfix::~InlineInfixToPostfix ( )
virtual

Destroys the InlineInfixToPostfix object.

Definition at line 32 of file InlineInfixToPostfix.cpp.

Member Function Documentation

◆ addOperator()

void Isis::InfixToPostfix::addOperator ( QString &  postfix,
const InfixOperator op,
std::stack< InfixOperator > &  theStack 
)
privateinherited

This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore.

Parameters
postfixThe postix generated thus far
opThe operator
theStackThe operator stack

Definition at line 287 of file InfixToPostfix.cpp.

◆ cleanSpaces()

QString Isis::InfixToPostfix::cleanSpaces ( QString  equation)
privateinherited

This function takes a space-delimited string and removes empty delimiters.

In order words, it compresses the spaces. This is used to keep strings small, clean, and remove the necessity to check for empty tokens constantly.

That is, if your string is "a b" the result will be "a b".

Parameters
equationA space-delimited string with excessive spaces
Returns
IString A space-delimited string with data between every pair of spaces

Definition at line 109 of file InfixToPostfix.cpp.

References Isis::IString::Token(), and Isis::IString::ToQt().

◆ closeParenthesis()

void Isis::InfixToPostfix::closeParenthesis ( QString &  postfix,
std::stack< InfixOperator > &  theStack 
)
privateinherited

This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore.

Parameters
postfixThe postix generated thus far
theStackThe operator stack

Definition at line 316 of file InfixToPostfix.cpp.

◆ convert()

QString Isis::InfixToPostfix::convert ( const QString &  infix)
inherited

This method converts infix to postfix.

It uses an enhanced verion of the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore. First, we prep the equation with TokenizeEquation and then parse through it using the known-good algorithm.

Parameters
infixThe infix equation
Returns
IString The postfix equation

Definition at line 139 of file InfixToPostfix.cpp.

References Isis::IString::Remove(), Isis::toDouble(), Isis::IString::Token(), and Isis::IString::ToQt().

Referenced by Isis::InlineCalculator::toPostfix().

◆ exists()

bool Isis::InlineInfixToPostfix::exists ( const QString &  token)
private

Determines whether the given string exists as a recognized variable.

NOTE: This list is constructed using the isVariable() method.

Parameters
tokenA string token to be compared to the recognized variable list.
Returns
bool Indicates whether the given string is recognized as a variable.

Definition at line 136 of file InlineInfixToPostfix.cpp.

References m_variables.

◆ findOperator()

InfixOperator * Isis::InlineInfixToPostfix::findOperator ( QString  token)
protectedvirtual

This method will first search the recognized list of operators and functions for the given token.

If found, it will return a pointer to the represented operator or function. If not found, the method checks whether the given token is a variable. If it is a variable, a new operator is constructed from the given token and a pointer to the represented variable is returned. If the given token is neither an operator, a function nor a variable, then an exception is thrown.

Parameters
tokenThe symbolic representation of the operator, function, or variable
Returns
InfixOperator* A pointer to the operator object that contains known information about the operator
Exceptions
IException::User"The token is not recognized as an operator, function or variable."

Reimplemented from Isis::InfixToPostfix.

Definition at line 81 of file InlineInfixToPostfix.cpp.

References Isis::InfixToPostfix::findOperator(), isVariable(), and Isis::IException::User.

◆ formatFunctionCalls()

QString Isis::InfixToPostfix::formatFunctionCalls ( QString  equation)
privateinherited

This method looks through equation for function calls, parenthesizes them, and calls itself again with each argument in order to parse any embedded functions.

This ensures order of operations holds for cases like sin(.5)^2. This method might add too many parentheses, but that is harmless. This does not affect operators (excepting –) or numbers. Only functions. The input should be space-delimited.

Parameters
equationThe unparenthesized equation
Returns
IString The parenthesized equation

This code block is for multi-parameter functions. Functions with 1+ parameters are parsed here, excepting the negation (with no parentheses) which is done just above.

We figure out the arguments by looking for commas, outside of all parentheses, for each argument up until the last one. We look for an extra closing parenthesis for the last argument. When we figure out what an argument is, we recursively call FormatFunctionCalls to format any and all functionality inside of the argument.

Definition at line 479 of file InfixToPostfix.cpp.

References Isis::IString::Token(), and Isis::IString::ToQt().

◆ initialize()

void Isis::InlineInfixToPostfix::initialize ( )
private

Adds several infix operators and functions to the operator list that are not already recognized by the parent class, InfixToPostfix.

Definition at line 108 of file InlineInfixToPostfix.cpp.

Referenced by InlineInfixToPostfix().

◆ isFunction()

bool Isis::InfixToPostfix::isFunction ( QString  representation)
privateinherited

This method will return true if 'representation' is a known function.

Parameters
representationThe symbolic representation of a function, such as 'abs'
Returns
bool True if it's a known function, false otherwise

Definition at line 270 of file InfixToPostfix.cpp.

◆ isKnownSymbol()

bool Isis::InlineInfixToPostfix::isKnownSymbol ( QString  representation)
protectedvirtual

This method attempts to verify that the given argument is recognized as a valid function, operator, scalar, or variable.

Parameters
representationThe symbolic representation of the operator
Returns
bool Indicates whether the representaion is recognized.

Reimplemented from Isis::InfixToPostfix.

Definition at line 44 of file InlineInfixToPostfix.cpp.

References Isis::InfixToPostfix::isKnownSymbol(), isScalar(), and isVariable().

◆ isScalar()

bool Isis::InlineInfixToPostfix::isScalar ( const QString &  token)
private

Determines whether the given token represents a scalar value (i.e.

whether it can be converted to a double).

Parameters
tokenString to be tested.
Returns
bool Indicates whether the given string can be converted to a double.

Definition at line 149 of file InlineInfixToPostfix.cpp.

References Isis::toDouble().

Referenced by isKnownSymbol().

◆ isVariable()

bool Isis::InlineInfixToPostfix::isVariable ( const QString &  token)
private

Determines whether the given token is a variable and, if so, appends it to the list of variables.

A token is considered a variable if it is not a known operator symbol from the parent InfixToPostfix class, not a scalar, and is not empty. Variables are implemented as functions, so new variables are also added to the operators list.

Parameters
tokenA string containing the token to be tested.
Returns
bool Indicates whether the string was added to the lists of recognized variables and operators.

Definition at line 178 of file InlineInfixToPostfix.cpp.

References m_variables.

Referenced by findOperator(), and isKnownSymbol().

◆ tokenizeEquation()

QString Isis::InfixToPostfix::tokenizeEquation ( const QString &  equation)
inherited

This method will add spaces between all operators and numbers, making it possible to get each element of the equation one by one.

It will also parse out the function calls, adding parenthesis where needed so the user doesn't have to. The result is an equation ready for parsing (but NOT fully parenthesized, just enough to make sure our algorithm makes no mistakes).

Parameters
equationAn unformatted infix equation
Returns
IString A tokenized equation with additional parentheses

Definition at line 369 of file InfixToPostfix.cpp.

References Isis::IString::DownCase(), and Isis::IString::ToQt().

◆ uninitialize()

void Isis::InfixToPostfix::uninitialize ( )
privateinherited

This cleans the known operators/functions list.

Definition at line 89 of file InfixToPostfix.cpp.

Member Data Documentation

◆ m_variables

QStringList Isis::InlineInfixToPostfix::m_variables
private

The list of variables (represented as strings).

Definition at line 50 of file InlineInfixToPostfix.h.

Referenced by exists(), and isVariable().


The documentation for this class was generated from the following files: