def validate_text(text): try: BNF().parseString(text.replace('x', str(0)), parseAll=True) fn.evaluate_stack(fn.exprStack) print('validated') except fn.ParseException as pe: print(text, "failed parse:", str(pe)) return False except Exception as e: print(text, "failed eval:", str(e), fn.exprStack) return False return True
def calculate_y_values(x_values, expression): # Формирование значений оси у по функции expression = expression.replace('X', 'x') y_values = [] for x in x_values: fn.exprStack = [] try: BNF().parseString(expression.replace('x', str(x)), parseAll=True) x_value = fn.evaluate_stack(fn.exprStack) except fn.ParseException as pe: print(expression, "failed parse:", str(pe)) return 0 except Exception as e: print(expression, "failed eval:", str(e), fn.exprStack) return 0 y_values.append(x_value) return y_values
'Parse Failure', input_string, (str(err), err.line, err.column) ] # show result of parsing the input string if debug_flag: print(input_string, "->", L) if len(L) == 0 or L[0] != 'Parse Failure': if debug_flag: print("exprStack=", exprStack) for i, ob in enumerate(exprStack): if isinstance(ob, str) and ob in variables: exprStack[i] = str(variables[ob]) # calculate result , store a copy in ans , display the result to user try: result = evaluate_stack(exprStack) except Exception as e: print(str(e)) else: variables['ans'] = result print(result) # Assign result to a variable if required if L.varname: variables[L.varname] = result if debug_flag: print("variables=", variables) else: print('Parse Failure') err_str, err_line, err_col = L[-1] print(err_line) print(" " * (err_col - 1) + "^")