In this repository there is a simple calculator that takes as input an operation and outputs the result of the operation.
For example, given the operation "3 * 6 =" this is a multiplication operation. The result of this operation is 18. The result is printed in the console.
The code can be run from the runner.py file.
Write the operation that you would like to calculate that follows the format:
A. integer operator integer (=) (e.g., 18 % 6 =)
B. x = 5 y = 7 x + y =
You can introduce one operator_sign from the list -> [+, -, *, /, %]
The result of the operation is printed on the console.
The code goes through 4 phases: scanning, parsing, analyzing, interpreting.
The code in this file scans the introduced operation and outputs a list of 'ScannerStringSegment' objects. Each object has the attributes:
token = one from the list [Integer, Space, Plus, Minus, Division Sign, Modulo Sign, Equal, Multiplication Sign, Variable]
start_string = the matched string
index and end_string are the numbers where the element begins and ends in the string.
The code in this file displays the specific, pre-set BNF Grammar that is followed to match the elements in the scanned list.
The BNF Grammar for this Calculator is:
root = operation/variable_definition
operation = integer operator integer (equal) / variable_symbol operator variable_symbol
variable_definition = variable_symbol equal integer
integer = non-fractional numbers
operator = one element of the list [+, -, *, /, %]
equal = '='
variable_symbol = letters/words
letter = [A-Z; a-z]
word = any word in the English language
The output after this step is a 'parse tree of grammar production objects'
that reflects the grammar specified above (e.g., Operation(AddExpression(Integer(5), Integer(4))))
The role of the code in this file is to search for, find, and correct semantic mistakes. The semantic mistakes are errors that are grammatically correct, but do not make sense as a whole.
The file takes the 'parse tree of grammar production objects', analyzes each object in the expression, and outputs the analyzed tree.
The role of the code in this file is to interpret the analyzed tree and to output the result of the operation.
This project is licensed under the MIT License - see the LICENSE.md file for details.