Skip to content

In this repository there is a simple calculator that takes as input an operation and outputs the result of the operation.

License

Notifications You must be signed in to change notification settings

puskini33/Calculator

Repository files navigation

Calculator

Table of Contents

General Information

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.

Setup

The code can be run from the runner.py file.

Code Description

1. runner.py

1.1. Input Format:

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 =

1.2. Constraints:

You can introduce one operator_sign from the list -> [+, -, *, /, %]

1.3. Output Format:

The result of the operation is printed on the console.



The code goes through 4 phases: scanning, parsing, analyzing, interpreting.

2. string_scanner.py

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.

3. string_parser.py

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))))

4. string_analyzer.py

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.

5. string_interpreter.py

The role of the code in this file is to interpret the analyzed tree and to output the result of the operation.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

In this repository there is a simple calculator that takes as input an operation and outputs the result of the operation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages