Fixed a grammar check bug in the tokenizer.

This commit is contained in:
2020-04-16 14:19:59 -05:00
parent 20e114e33d
commit 0a79cdba54
2 changed files with 13 additions and 12 deletions
+12 -11
View File
@@ -1,27 +1,28 @@
import token import token
import tokenizer import tokenizer
import expression #import expression
import calc_engine import calc_engine
import json #import jsonpickle
#fileLocation = input('File Path to JSON: ') #fileLocation = input('File Path to JSON: ')
#json = json.loads(open(fileLocation).read()) #json = json.loads(open(fileLocation).read())
userInput = input('Enter your formula: ') #userInput = input('Enter your formula: ')
tokens = tokenizer.get_tokens_from_expression_string(userInput) tokens = tokenizer.get_tokens_from_expression_string("5000(2(2(1.08^10)-1)/0.08)")
#with open("expression.json", "r") as f: #with open("expression.json", "r") as f:
# j = json.load(f) # j = json.load(f)
# exp = expression.Expression(json.loads(j)) # exp = jsonpickle.decode(f.read())
#exp = expression.Expression.from_json(json.loads(json.load(f)))
# print(exp) # print(exp)
#results = calc_engine.calculate_results(tokens.copy()) results = calc_engine.calculate_results(tokens.copy())
#print(results) print(results)
tokens = tokenizer.replace_variables(tokens, [23, 46, 89]) #tokens = tokenizer.replace_variables(tokens, [23, 46, 89])
for t in tokens: #for t in tokens:
print("%s (%s) " %(t.value, token.TokenType.get_token_type_name(t.type)), end = "") # print("%s (%s) " %(t.value, token.TokenType.get_token_type_name(t.type)), end = "")
print() #print()
#def factors(factor_object, starting_point, variable_type) #def factors(factor_object, starting_point, variable_type)
+1 -1
View File
@@ -13,7 +13,7 @@ def get_tokens_from_expression_string(expression_string):
for i in range(0, len(expression_string)): for i in range(0, len(expression_string)):
c = expression_string[i] c = expression_string[i]
if c == '.' or c.isdigit(): if c == '.' or c.isdigit():
if tmp.count('.') == 1: if c == '.' and tmp.count('.') == 1:
raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".") raise SyntaxError("Invalid grammar, to many periods in number. Found at position " + str (i) + ".")
if len(tokens) > 1: if len(tokens) > 1:
lookBehind = tokens[-1] lookBehind = tokens[-1]