一个可配置的 C++ 表达式解析器。可用作计算器或帮助您编写自己的编程语言
This project provides a C++ library to parse a character sequence as an expression using Dijkstra's Shunting-yard algorithm, which modifies Jesse Brown's original code.
This project was developed by Brandon Amos and Vinícius Garcia.
If you want to use this library in your project please take a look at our Wiki
+, -+, -, /, *, %, >, ^, &, |, **=, ==, !=, &&, ||sin, cos, tan, abs, printcd 'my/project/dir'
git clone https://github.com/cparse/cparse.git
make release -C cparseg++ -I cparse -std=c++11 cparse/builtin-features.o cparse/core-shunting-yard.o main.cpp -o mainIf you want to make sure everything is working in your environment:
make test -C cparseTo customize your calculator:
builtin-features.cpp file and builtin-features/ directory to your project.builtin-features/*.inc files as you like.make release -C cparse/g++ -I cparse -std=c++11 -c builtin-features.cpp -o my-features.og++ -I cparse -std=c++11 my-features.o cparse/core-shunting-yard.o main.cpp -o mainFor a more detailed guide read our Wiki advanced concepts' section:
#include
#include "shunting-yard.h"
int main() {
cparse::TokenMap vars;
vars["pi"] = 3.14;
std::cout << cparse::calculator::calculate("-pi+1", &vars) << std::endl;
// Or if you want to evaluate an expression
// several times efficiently:
cparse::calculator c1("pi-b");
vars["b"] = 0.14;
std::cout << c1.eval(vars) << std::endl; // 3
vars["b"] = 2.14;
std::cout << c1.eval(vars) << std::endl; // 1
return 0;
}Here we implement an interpreter for multiple expressions, the delimiter used
will be ; or \n just like Javascript or Python and the code must start and end on curly brackets.
A 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 that uses this project as the core parsing system.
…Please note that a calculator can compile an expression so that it can efficiently be executed several times at a later moment.
暂无开放 Issues,或尚未同步最近议题。