{"id":23003115,"url":"https://github.com/programmerstevie/cppmather","last_synced_at":"2025-09-06T06:36:44.014Z","repository":{"id":197692054,"uuid":"265743358","full_name":"programmerstevie/cppMather","owner":"programmerstevie","description":"c++ mathematical expression parser and evaluator.","archived":false,"fork":false,"pushed_at":"2024-10-04T04:11:33.000Z","size":264,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-31T17:58:42.152Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/programmerstevie.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-21T03:15:49.000Z","updated_at":"2024-10-04T04:11:36.000Z","dependencies_parsed_at":"2025-02-08T05:35:04.386Z","dependency_job_id":null,"html_url":"https://github.com/programmerstevie/cppMather","commit_stats":null,"previous_names":["stothesecond/cppmather","programmerstevie/cppmather"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/programmerstevie/cppMather","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FcppMather","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FcppMather/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FcppMather/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FcppMather/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/programmerstevie","download_url":"https://codeload.github.com/programmerstevie/cppMather/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmerstevie%2FcppMather/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273867757,"owners_count":25182423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-15T07:13:24.964Z","updated_at":"2025-09-06T06:36:43.991Z","avatar_url":"https://github.com/programmerstevie.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# C++ Expression Parser and Evaluator\r\n\r\nThis project is a C++ implementation of a mathematical expression parser and evaluator. It takes an input string representing a mathematical expression, tokenizes it, parses it into an Abstract Syntax Tree (AST), and then evaluates the result. The parser supports basic arithmetic operations such as addition, subtraction, multiplication, division, exponentiation, and modulus, along with parentheses for grouping and unary negation. This could easily be extended to support common functions such as sine and cosine.\r\n\r\n## Table of Contents\r\n\r\n- [Features](#features)\r\n- [Getting Started](#getting-started)\r\n- [Usage](#usage)\r\n- [Project Structure](#project-structure)\r\n- [How It Works](#how-it-works)\r\n- [Contributing](#contributing)\r\n- [License](#license)\r\n\r\n## Features\r\n\r\n- Parses mathematical expressions into an Abstract Syntax Tree (AST)\r\n- Supports basic arithmetic operations: `+`, `-`, `*`, `/`, `%`, `^`\r\n- Handles unary negation and parentheses for grouping\r\n- Error handling for syntax errors like unmatched parentheses or division by zero\r\n- Clean, modular design with a lexer, parser, and evaluator\r\n\r\n## Getting Started\r\n\r\n### Prerequisites\r\n\r\nTo build and run this project, you will need:\r\n- A C++ compiler supporting C++11 or later (e.g., GCC, Clang)\r\n- `make` (optional, if you use a Makefile)\r\n- Git (to clone the repository)\r\n\r\n### Installing\r\n\r\n1. **Clone the repository:**\r\n\r\n   ```bash\r\n   git clone https://github.com/programmerstevie/cppMather.git\r\n   cd cppMather\r\n   ```\r\n\r\n2. **Compile the project:**\r\n\r\n   If you're using `make`, simply run:\r\n\r\n   ```bash\r\n   make\r\n   ```\r\n\r\n   Alternatively, compile the source files manually:\r\n\r\n   ```bash\r\n   g++ -std=c++11 -o parser main.cpp lexer.cpp parser.cpp evaluator.cpp\r\n   ```\r\n\r\n## Usage\r\n\r\nAfter compiling the project, run the executable and enter a mathematical expression to evaluate:\r\n\r\n1. **Run the executable:**\r\n\r\n   ```bash\r\n   ./parser\r\n   ```\r\n\r\n2. **Input a mathematical expression:**\r\n\r\n   For example:\r\n\r\n   ```bash\r\n   (5 + 3) * 2 - 4 ^ 2\r\n   ```\r\n\r\n3. **The program will output:**\r\n\r\n   ```bash\r\n   (5 + 3) * 2 - 4 ^ 2 = -4\r\n   ```\r\n\r\n## Project Structure\r\n\r\n- **`includes.hpp`**: Centralized file for including necessary standard library headers.\r\n- **`lexer.hpp` / `lexer.cpp`**: Responsible for tokenizing the input string into meaningful tokens (numbers, operators, parentheses).\r\n- **`parser.hpp` / `parser.cpp`**: Processes tokens into an Abstract Syntax Tree (AST) using operator precedence and associativity rules.\r\n- **`evaluator.hpp` / `evaluator.cpp`**: Evaluates the AST and computes the result of the expression.\r\n- **`token.hpp` / `token.cpp`**: Defines the different types of tokens used in the program (integer tokens, operator tokens, parenthesis tokens).\r\n- **`main.cpp`**: The entry point of the program, showing an example of how to go from an expression string to its evaluation.\r\n\r\n## How It Works\r\n\r\n### 1. Lexical Analysis (Tokenization)\r\n\r\nThe lexer (`lexer.cpp`) breaks down the input string into **tokens** representing integers, operators, and parentheses. For example, the expression `3 + (5 * 2)` would be tokenized into:\r\n\r\n- `IntToken(3)`\r\n- `BinaryOpToken(+)`\r\n- `ParenToken(()`\r\n- `IntToken(5)`\r\n- `BinaryOpToken(*)`\r\n- `IntToken(2)`\r\n- `ParenToken())`\r\n\r\n### 2. Parsing\r\n\r\nThe parser (`parser.cpp`) uses a **shunting yard algorithm** to convert the tokens into an Abstract Syntax Tree (AST). It ensures that operations are applied in the correct order by respecting operator precedence and associativity.\r\n\r\nFor example, for the input `5 + 3 * 2`, the AST will look like this:\r\n\r\n```\r\n     +\r\n   /   \\\r\n  5     *\r\n       / \\\r\n      3   2\r\n```\r\n\r\n### 3. Evaluation\r\n\r\nThe evaluator (`evaluator.cpp`) recursively evaluates the AST to compute the final result of the expression. For example, the AST for `5 + 3 * 2` will be evaluated as:\r\n\r\n1. Multiply `3 * 2` to get `6`.\r\n2. Add `5 + 6` to get `11`.\r\n\r\n### Error Handling\r\n\r\nThe parser and evaluator have several built-in error checks to ensure that the input expression is valid:\r\n- **Unmatched Parentheses**: The parser checks for balanced parentheses.\r\n- **Division by Zero**: The evaluator checks for division by zero and terminates with an error if encountered.\r\n- **Invalid Tokens**: The lexer and token constructors validate that only valid tokens are processed.\r\n\r\n### Pretty Printing\r\n\r\nThe `prettyPrint()` function in `evaluator.cpp` can convert an AST back into a human-readable string, showing the expression in standard infix notation.\r\n\r\n## Contributing\r\n\r\nContributions are welcome! If you have suggestions or improvements, feel free to fork the repository and submit a pull request.\r\n\r\n1. Fork the project\r\n2. Create your feature branch (`git checkout -b feature/AmazingFeature`)\r\n3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)\r\n4. Push to the branch (`git push origin feature/AmazingFeature`)\r\n5. Open a pull request\r\n\r\n## License\r\n\r\nDistributed under the MIT License. See `LICENSE` for more information.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogrammerstevie%2Fcppmather","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprogrammerstevie%2Fcppmather","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogrammerstevie%2Fcppmather/lists"}