/stal-arithmetic-parser

Rudimentary arithmetic parser executable on the command line. Built with C++11 using standard libraries.

Primary LanguageC++MIT LicenseMIT

Simple Arithmetic Parser

Seth Tal

07.31.2021


Rudimentary arithmetic parser executable on the command line. Built with C++11 using standard libraries. Calculates simple arithmetic expressions of the form:

  • EXPRESSION : (2 + 2) | (3 * 7) | (4 / 2) | (5 - 2)
  • VALUE : NUMBER (i.e. within the set of integers)
  • OPERATION : 3 + 9 (i.e. Expression without parenthesis)

All of which can be chained together.

# This will evaluate to -> 9
./parser -s "1 + (4 * 10) / (7 - 2)"

Important Note: This parser does not follow PEMDAS mathmatical rules, any operation will have Left Hand Side priority. Here is an example:

./parser -s "3 + (4*5) / 9"

# This should evaluate to: 5.22222222 because of order of oporations (PEMDAS)
# Instead due to Left hand side priority it will evaluate to: 2.55555555

# In practice this looks like so:
#    3 + (4*5) / 9 => 
#    3 + 20 / 9    =>
#    23 / 9        => 2.55555555

How to Compile and Run

This program was made using Windows Subsystem for Linux in a Ubuntu 20.04.1 LTS install. This program was written and compiled with g++ using the following flags:

-g -W -Wall -std=c++11

Requirements

  • Ubuntu 20.04.1 LTS
    • or Windows Subsystem for Linux (WSL) running Ubuntu 20.04.1 LTS
  • g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
  • GNU Make 4.2.1

Step 1: Download and Extract the contents from the .ZIP file

# File Structure
.
├── _.vscode
│   ├── settings.json
├── _src
│   ├── main.cpp
│   ├── Makefile
│   ├── parser.cpp
│   └── parser.h
├── .gitignore
├── clean.sh
├── LICENSE
├── Makefile
├── README.md
└── test.txt

Step 2

Using the makefiles provided you can make the project which will produce the executable, parser, inside the src directory.

# Navigate to the root directory of the project and simply perform the following command:

make

# This will build the linux executable 'parser' inside the ./src directory

Step 3

You can either navigate into the src directory or simply run the program from root like so:

# Expression should be enclosed in quotes

./src/parser -s "Expression to Parse"

# Or you can use a .txt file to parse multiple expressions...

./src/parser -f ./test.txt

Note about file input

The contents of the test file should like like the following:

1 + 1
(3 + 4) * 6
(1 * 4) + (5 * 2)

Where each expression is seperated onto its own line in the file.