def parse(expr): class State: current = expr[0] tokens = [] def exp(): result = term() while State.current in ('+', '-'): if State.current == '+': next_token() result += term() if State.current == '-': next_token() result -= term() return result def factor(): result = None if State.current[0].isdigit() or State.current[-1].isdigit(): result = float(State.current) next_token() elif State.current is '(': next_token() result = exp() next_token() return result def next_token(): State.tokens = State.tokens[1:] State.current = State.tokens[0] if len(State.tokens) > 0 else None def term(): result = factor() while State.current in ('*', '/'): if State.current == '*': next_token() result *= term() if State.current == '/': next_token() result /= term() return result for i in range(len(expr)): if expr[i].isdigit() and i > 0 and (State.tokens[-1].isdigit() or State.tokens[-1][-1] is '.'): State.tokens[-1] += expr[i] elif expr[i] is '.' and i > 0 and State.tokens[-1].isdigit(): State.tokens[-1] += expr[i] else: State.tokens.append(expr[i]) return exp()
def parse(expr): #The main parserps = 0 #Number of open parenthesescval = 0 #Current valueop = "+" #Current operationaccum = "" #Accumulating valuefor i in range(len(expr)):c = expr[i]if c in ["+","-"] and not ps: #Operation not inside parensif op=="+": #Additioncval+=parse_fact(accum)else: #Subtractioncval-=parse_fact(accum)accum = "" #Reset the valueop = c #New operation once that was calculatedelse:if c=="(": ps+=1 #Open parenif c==")": ps-=1 #Close parenaccum+=c #Add a character to accumulating valueif op=="+": #Do the operation one more timecval+=parse_fact(accum)else:cval-=parse_fact(accum)return cvaldef parse_fact(term):ps = 0cval = 1op = "*"accum = ""for i in range(len(term)):c = term[i]if c in ["*","/"] and not ps:if op=="*":cval*=parse_val(accum)else:cval/=parse_val(accum)accum = ""op = celse:if c=="(": ps+=1if c==")": ps-=1accum+=cif op=="*":cval*=parse_val(accum)else:cval/=parse_val(accum)return cvaldef parse_val(val):if val[0] == "(": #Parenthetical expressionreturn parse(val[1:-1]) #Cut off parentheses and reevaluateelse:return float(val) #Not parenthetical- def parse(expr):
- class State:
- current = expr[0]
- tokens = []
- def exp():
- result = term()
- while State.current in ('+', '-'):
- if State.current == '+':
- next_token()
- result += term()
- if State.current == '-':
- next_token()
- result -= term()
- return result
- def factor():
- result = None
- if State.current[0].isdigit() or State.current[-1].isdigit():
- result = float(State.current)
- next_token()
- elif State.current is '(':
- next_token()
- result = exp()
- next_token()
- return result
- def next_token():
- State.tokens = State.tokens[1:]
- State.current = State.tokens[0] if len(State.tokens) > 0 else None
- def term():
- result = factor()
- while State.current in ('*', '/'):
- if State.current == '*':
- next_token()
- result *= term()
- if State.current == '/':
- next_token()
- result /= term()
- return result
- for i in range(len(expr)):
- if expr[i].isdigit() and i > 0 and (State.tokens[-1].isdigit() or State.tokens[-1][-1] is '.'):
- State.tokens[-1] += expr[i]
- elif expr[i] is '.' and i > 0 and State.tokens[-1].isdigit():
- State.tokens[-1] += expr[i]
- else:
- State.tokens.append(expr[i])
- return exp()