You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.0 KiB
Python
48 lines
1.0 KiB
Python
def parse(expr):
|
|
|
|
opers = ['+', '-', '*', '/', '=']
|
|
|
|
num_par = 0
|
|
tokenized = []
|
|
temp_string = ""
|
|
index = 0
|
|
while index < len(expr):
|
|
if expr[index] in opers:
|
|
tokenized.append(expr[index])
|
|
index += 1
|
|
|
|
elif expr[index].isdigit() or expr[index] == '.':
|
|
while (index < len(expr)) and (expr[index].isdigit() or expr[index] == '.'):
|
|
temp_string += expr[index]
|
|
index += 1
|
|
tokenized.append(temp_string)
|
|
|
|
elif expr[index] == '(':
|
|
num_par = 1
|
|
temp_string += expr[index]
|
|
index += 1
|
|
while index < len(expr) and num_par != 0:
|
|
temp_string += expr[index]
|
|
if expr[index] == '(':
|
|
num_par += 1
|
|
if expr[index] == ')':
|
|
num_par -= 1
|
|
index += 1
|
|
|
|
tokenized.append(temp_string)
|
|
|
|
elif expr[index].isalpha(): # If you encounter a variable
|
|
temp_string += expr[index]
|
|
index += 1
|
|
while index < len(expr) and expr[index].isalpha():
|
|
temp_string += expr[index]
|
|
index += 1
|
|
tokenized.append(temp_string)
|
|
|
|
else:
|
|
index += 1
|
|
|
|
temp_string = ""
|
|
|
|
return tokenized
|