Added additional error checking, to check for invalid expressions

master
Aadhavan Srinivasan 11 months ago
parent 87f5c49bbb
commit 516e917086

@ -1,3 +1,41 @@
"""
===============================================================================
ENGR 13300 Fall 2023
Program Description
An algebraic expression calculator. This program can evaluate expressions, assign values to variables, and use variables in additional expressions.
The program consists of three main parts:
1. Parsing the expression. This is accomplished using the 'parse' function, defined in parse.py. The expression is parsed, and a list of 'tokens' is produced, with each token bein a part of the expression. For example '5 + 7 * 3' is parsed into '['5', '+', '7', '*', '3'].
2. Find the inner-most expression. This only applies to expressions that have parantheses. The 'find_inner' function is called recursively, until the inner expression is found. For example, given the expression '3 + (7 * (4 + 8))', the 'find_inner' function is called once, to produce '7 * (4 + 8)', and then called again, to produce '4 + 8'.
3. Evaluate the expression. The 'evaluate' function searches for operators, and then evaluates the operator with the preceding and succeeding token (if one of the tokens is a variable, the value of the variable replaces the variable). The result of the evaluation replaces the expression. Example: Given the expression '3 + (7 * (4 + 8))', the inner expression is evaluated first. This produces '3 + (7 * 12)'. The inner expression is evaluated again, producing '3 + 84'. Finally, the rest of the expression is evaluated, to produce 87.
4. If the expression consists of an assignment, the evaluated expression is stored into the variable on the left. The list of variables is maintained as a dictionary.
Assignment Information
Assignment: Individual Project
Author: Aadhavan Srinivasan, srini193@purdue.edu
Team ID: LC3 - 19
Contributor: Name, login@purdue [repeat for each]
My contributor(s) helped me:
[ ] understand the assignment expectations without
telling me how they will approach it.
[ ] understand different ways to think about a solution
without helping me plan my solution.
[ ] think through the meaning of a specific error or
bug present in my code without looking at my code.
Note that if you helped somebody else with their code, you
have to list that person as a contributor here as well.
ACADEMIC INTEGRITY STATEMENT
I have not used source code obtained from any other unauthorized
source, either modified or unmodified. Neither have I provided
access to my code to another. The project I am submitting
is my own original work.
===============================================================================
"""
import re
from parse import *
@ -28,8 +66,14 @@ def print_error(error_code):
print("One of your values is improperly formatted.")
case 5:
print("Uninitialized variable.")
case 6:
print("Invalid expression.")
return
def check_errors(expr):
# Check for errors before parsing the expression
expr_small = expr.replace(" ", "") # Remove spaces from the string, to make it easier to parse
# Check if number of opening parantheses is equal to number of closing parantheses
@ -56,12 +100,18 @@ def check_errors(expr):
# Check for errors after parsing the expression
expr = parse(expr)
if expr[0].isalpha() and (len(expr) == 1 or expr[1] != '='): # If you just have an expression with a letter eg. 'x', or you use a variable without an assignment (e.g. 'x 5')
return 6
for val in expr:
if val.count('.') > 1: # A value can have at most 1 period
return 4
if val.isalpha() and not val in variables and val != expr[0]: # If the token is a string, and isn't in the dictionary, and isn't the variable at index 0 (e.g. 'x' in 'x = 4')
return 5
return 0

Loading…
Cancel
Save