So, I have a NumericStringParser
class (extracted from here), defined as below:
from __future__ import division
from pyparsing import Literal, CaselessLiteral, Word, Combine, Group, Optional, ZeroOrMore, Forward, nums, alphas, oneOf, ParseException
import math
import operator
class NumericStringParser(object):
def __push_first__(self, strg, loc, toks):
self.exprStack.append(toks[0])
def __push_minus__(self, strg, loc, toks):
if toks and toks[0] == "-":
self.exprStack.append("unary -")
def __init__(self):
point = Literal(".")
e = CaselessLiteral("E")
fnumber = Combine(Word("+-" + nums, nums) +
Optional(point + Optional(Word(nums))) +
Optional(e + Word("+-" + nums, nums)))
ident = Word(alphas, alphas + nums + "_$")
plus = Literal("+")
minus = Literal("-")
mult = Literal("*")
floordiv = Literal("//")
div = Literal("/")
mod = Literal("%")
lpar = Literal("(").suppress()
rpar = Literal(")").suppress()
addop = plus | minus
multop = mult | floordiv | div | mod
expop = Literal("^")
pi = CaselessLiteral("PI")
tau = CaselessLiteral("TAU")
expr = Forward()
atom = ((Optional(oneOf("- +")) +
(ident + lpar + expr + rpar | pi | e | tau | fnumber).setParseAction(self.__push_first__))
| Optional(oneOf("- +")) + Group(lpar + expr + rpar)
).setParseAction(self.__push_minus__)
factor = Forward()
factor << atom + \
ZeroOrMore((expop + factor).setParseAction(self.__push_first__))
term = factor + \
ZeroOrMore((multop + factor).setParseAction(self.__push_first__))
expr << term + \
ZeroOrMore((addop + term).setParseAction(self.__push_first__))
self.bnf = expr
self.opn = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv,
"//": operator.floordiv,
"%": operator.mod,
"^": operator.pow,
"=": operator.eq,
"!=": operator.ne,
"<=": operator.le,
">=": operator.ge,
"<": operator.lt,
">": operator.gt
}
self.fn = {
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"asin": math.asin,
"acos": math.acos,
"atan": math.atan,
"exp": math.exp,
"abs": abs,
"sqrt": math.sqrt,
"floor": math.floor,
"ceil": math.ceil,
"trunc": math.trunc,
"round": round,
"fact": factorial,
"gamma": math.gamma
}
def __evaluate_stack__(self, s):
op = s.pop()
if op == "unary -":
return -self.__evaluate_stack__(s)
if op in ("+", "-", "*", "//", "/", "^", "%", "!=", "<=", ">=", "<", ">", "="):
op2 = self.__evaluate_stack__(s)
op1 = self.__evaluate_stack__(s)
return self.opn[op](op1, op2)
if op == "PI":
return math.pi
if op == "E":
return math.e
if op == "PHI":
return phi
if op == "TAU":
return math.tau
if op in self.fn:
return self.fn[op](self.__evaluate_stack__(s))
if op[0].isalpha():
raise NameError(f"{op} is not defined.")
return float(op)
I have an evaluate()
function, defined as below:
def evaluate(expression, parse_all=True):
nsp = NumericStringParser()
nsp.exprStack = []
try:
nsp.bnf.parseString(expression, parse_all)
except ParseException as error:
raise SyntaxError(error)
return nsp.__evaluate_stack__(nsp.exprStack[:])
evaluate()
is a function that will parse a string to calculate a mathematical operation, for example:
>>> evaluate("5+5")
10
>>> evaluate("5^2+1")
26
The problem is that it cannot compute comparison operators (=
, !=
, <
, >
, <=
, >=
), and when I try: evaluate("5=5")
, it throws SyntaxError: Expected end of text (at char 1), (line:1, col:2)
instead of returning True
. How can the function compute those six comparison operators?