Я создаю калькулятор с помощью PLY и хочу упростить уравнения.
Допустим, от этого x + x - 42 / 2
до 2x - 21
У меня уже есть правила 'classi c' для операций:
def p_operations(p):
""" expression : sixth
sixth : fifth
fifth : fourth
fourth : third
third : second
second : first
first : NUMBER
"""
p[0] = p[1]
def p_plus(p):
""" fifth : fifth '+' fourth """
p[0] = p[1] + p[3]
def p_minus(p):
""" fifth : fifth '-' fourth """
p[0] = p[1] - p[3]
def p_implicit_times(p):
""" fourth : fourth second """
p[0] = p[1] * p[2]
def p_times(p):
""" fourth : fourth '*' third """
p[0] = p[1] * p[3]
def p_divide(p):
""" fourth : fourth '/' third """
p[0] = p[1] / p[3]
def p_modulo(p):
""" fourth : fourth '%' third """
p[0] = p[1] % p[3]
def p_floor_divide(p):
""" fourth : fourth FLOORDIV third """
p[0] = p[1] // p[3]
def p_unary_minus(p):
""" third : '-' third """
p[0] = -p[2]
def p_power(p):
""" second : first '^' third """
p[0] = p[1] ** p[3]
def p_paren(p):
""" first : '(' expression ')' """
p[0] = p[2]
Должен ли я создать специальный класс для неизвестных чисел или есть лучший способ в Python?