variables = {'e': e, 'pi': pi}
try:
    eval(expression, variables, functions)
except (SyntaxError, NameError, ZeroDivisionError):
    return False
else:
    return True
math syntax checker written in python