{"id":13730969,"url":"https://github.com/cparse/cparse","last_synced_at":"2025-05-08T03:32:28.032Z","repository":{"id":1734012,"uuid":"10548078","full_name":"cparse/cparse","owner":"cparse","description":"A C++ configurable Expression Parser. Useful as a Calculator or for helping you write your own Programming Language","archived":false,"fork":false,"pushed_at":"2023-08-11T06:44:03.000Z","size":559,"stargazers_count":331,"open_issues_count":9,"forks_count":65,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-08-04T02:09:53.294Z","etag":null,"topics":["c-plus-plus","calculator","expression-parser","programming-language"],"latest_commit_sha":null,"homepage":"","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/cparse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.mit","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-07T10:57:19.000Z","updated_at":"2024-07-27T10:48:21.000Z","dependencies_parsed_at":"2023-01-13T11:20:57.268Z","dependency_job_id":null,"html_url":"https://github.com/cparse/cparse","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cparse%2Fcparse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cparse%2Fcparse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cparse%2Fcparse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cparse%2Fcparse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cparse","download_url":"https://codeload.github.com/cparse/cparse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224695860,"owners_count":17354496,"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","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":["c-plus-plus","calculator","expression-parser","programming-language"],"created_at":"2024-08-03T02:01:22.175Z","updated_at":"2024-11-14T21:31:59.734Z","avatar_url":"https://github.com/cparse.png","language":"C++","readme":"# CParser • [![Build Status][travis-image]][travis] [![License][license-image]][license]\n\n[travis-image]: https://travis-ci.org/cparse/cparse.png?branch=master\n[travis]: http://travis-ci.org/cparse/cparse\n\n[release-image]: http://img.shields.io/badge/release-0.2.1-blue.svg?style=flat\n[releases]: https://github.com/cmusatyalab/openface/releases\n\n[license-image]: http://img.shields.io/badge/license-MIT-blue.svg?style=flat\n[license]: LICENSE.mit\n\nThis project provides a C++ library to parse a character sequence\nas an expression using Dijkstra's\n[Shunting-yard algorithm](http://en.wikipedia.org/wiki/Shunting-yard_algorithm),\nwhich modifies\n[Jesse Brown's original code](http://www.daniweb.com/software-development/cpp/code/427500/calculator-using-shunting-yard-algorithm).\n\n*This project was developed by [Brandon Amos](http://bamos.github.io) and Vinícius Garcia.*\n\n# Getting Started\n\nIf you want to use this library in your project please take a look at our [Wiki][wiki]\n\n[wiki]: https://github.com/cparse/cparse/wiki\n\n## Builtin Features\n + Unary operators. `+`, `-`\n + Binary operators. `+`, `-`, `/`, `*`, `%`, `\u003c\u003c`, `\u003e\u003e`, `^`, `\u0026`, `|`, `**`\n + Boolean operators. `\u003c`, `\u003e`, `\u003c=`, `\u003e=`, `==`, `!=`, `\u0026\u0026`, `||`\n + Functions. `sin`, `cos`, `tan`, `abs`, `print`\n + Support for an hierarchy of scopes with local scope, global scope etc.\n + Easy to add new operators, operations, functions and even new types\n + Easy to implement object-to-object inheritance (with the prototype concept)\n + Built-in garbage collector (does not handle cyclic references yet)\n\n## Setup\n\n### Download and Compile\n\n```bash\ncd 'my/project/dir'\ngit clone https://github.com/cparse/cparse.git\nmake release -C cparse\n```\n\n### Link with your project:\n\n```bash\ng++ -I cparse -std=c++11 cparse/builtin-features.o cparse/core-shunting-yard.o main.cpp -o main\n```\n\n### Running the library tests:\n\nIf you want to make sure everything is working in your environment:\n\n```bash\nmake test -C cparse\n```\n\n## Customizing your Library\nTo customize your calculator:\n\n 1. Copy the `builtin-features.cpp` file and `builtin-features/` directory to your project.\n 2. Edit the `builtin-features/*.inc` files as you like.\n 3. Then build the project:\n    1. Compile the library: `make release -C cparse/`\n    2. Compile your modified features: `g++ -I cparse -std=c++11 -c builtin-features.cpp -o my-features.o`\n    3. Link your project: `g++ -I cparse -std=c++11 my-features.o cparse/core-shunting-yard.o main.cpp -o main`\n\nFor a more detailed guide read our [Wiki][wiki] advanced concepts' section:\n\n + [Defining New Functions](https://github.com/bamos/cpp-expression-parser/wiki/Defining-New-Functions)\n + [Defining New Operations](https://github.com/bamos/cpp-expression-parser/wiki/Defining-New-Operations)\n + [Defining New Reserved Words](https://github.com/bamos/cpp-expression-parser/wiki/Defining-Reserved-Words)\n\n## Minimal examples\n\n### As a simple calculator\n\n```C++\n#include \u003ciostream\u003e\n#include \"shunting-yard.h\"\n\nint main() {\n  cparse::TokenMap vars;\n  vars[\"pi\"] = 3.14;\n  std::cout \u003c\u003c cparse::calculator::calculate(\"-pi+1\", \u0026vars) \u003c\u003c std::endl;\n\n  // Or if you want to evaluate an expression\n  // several times efficiently:\n  cparse::calculator c1(\"pi-b\");\n  vars[\"b\"] = 0.14;\n  std::cout \u003c\u003c c1.eval(vars) \u003c\u003c std::endl; // 3\n  vars[\"b\"] = 2.14;\n  std::cout \u003c\u003c c1.eval(vars) \u003c\u003c std::endl; // 1\n\n  return 0;\n}\n```\n\n### As a sub-parser for a programming language\n\nHere we implement an interpreter for multiple expressions, the delimiter used\nwill be `;` or `\\n` just like Javascript or Python and the code must start and end on curly brackets.\n\nA similar architecture can be used for interpreting other common programming language statements like `for` loops and `if` statements. If you're interested take a look on the [jSpy programming language](https://github.com/vingarcia/jspy) that uses this project as the core parsing system.\n\n```C++\n#include \u003ciostream\u003e\n#include \"shunting-yard.h\"\n#include \"shunting-yard-exceptions.h\"\n\nstruct codeBlock {\n  static void interpret(const char* start, const char** end, TokenMap vars) {\n    // Remove white spaces:\n    while (isspace(*start)) ++start;\n\n    if (*start != '{') {\n      throw syntax_error(\"Expected '{'\");\n    } else {\n      ++start;\n    }\n\n    while (*start != '}') {\n      cparse::calculator::calculate(start, vars, \";\\n}\", \u0026start);\n\n      // Alternatively you could write above:\n      // - calculator(start, \";\\n}\", \u0026start).eval(vars);\n\n      // Find the beginning of the next expression:\n      while(isspace(*start) || *start == ';') ++start;\n    }\n\n    if (*start == '}') {\n      *end = start+1;\n    } else {\n      throw syntax_error(\"Expected '}'\");\n    }\n  }\n};\n\nint main() {\n  cparse::GlobalScope vars;\n  const char* code =\n    \"{\"\n    \"  a = 10;\"\n    \"  b = 20\\n\"\n    \"  c = a + b }\";\n\n  codeBlock::interpret(code, \u0026code, vars);\n\n  std::cout \u003c\u003c vars[\"c\"] \u003c\u003c std::endl; // 30\n  return 0;\n}\n```\n\nPlease note that a calculator can compile an expression so that it can efficiently be executed several times at a later moment.\n\n## More examples\n\n + For more examples and a comprehensible guide please read our [Wiki][wiki]\n\n## Contributing\n\n- I would like to keep this library minimal so new features should be very useful to be accepted.\n- If proposed change is not a common use case, I will probably not accept it.\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcparse%2Fcparse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcparse%2Fcparse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcparse%2Fcparse/lists"}