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.

78 lines
2.4 KiB
Python

"""
===============================================================================
ENGR 13300 Fall 2023
Program Description
This is the function that parses the expression. It goes through the expression, and produces a list of 'tokens', where each token represents a part of the expression. For example, '3 + 2' would yield the tokens '3', '+' and '2'. It then returns the list of tokens.
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.
===============================================================================
"""
11 months ago
def parse(expr):
opers = ['+', '-', '*', '/', '=']
11 months ago
num_par = 0
tokenized = []
temp_string = ""
index = 0
while index < len(expr):
if expr[index] in opers:
tokenized.append(expr[index])
index += 1
11 months ago
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:
11 months ago
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
11 months ago
temp_string = ""
11 months ago
return tokenized