Expression Parser is a C++ library to parse a character sequence as an expression using Dijkstra’s Shunting-yard algorithm which modifies Jesse Brown’s code.
The main block is at amalulla/cpp-expression-parser, which is generated from this github repo.
In this example we’ll show a simple code to develop a mathematical expression parser, with expressions like “pi” or “gravity”.
Create a simple project and the code inside it:
$ bii init exp-parser_example -L
$ cd exp-parser_example
$ # copy the code below
main.cpp
#include <iostream>
#include <limits>
#include <map>
#include <string>
#include "shunting-yard.h"
void calculation(const char* expr,
std::map<std::string, double>* vars = 0) {
double result = calculator::calculate(expr, vars);
std::cout << "'" << expr << "' calculated result is: " <<
result << "." << std::endl;
}
int main() {
std::map <std::string, double> vars;
vars["gravity"] = 9.78;
vars["pi"] = 3.14;
calculation("-pi+1", &vars);
calculation("10*gravity", &vars);
return 0;
}
Check the dependencies of the project with bii deps:
$ bii deps
INFO: Processing changes...
your_user/expression_parser depends on:
system:
iostream
limits
map
string
unresolved:
shunting-yard.h
Now, edit the biicode.conf file generated in the project folder. Add your [requirements]
depending on the version you want and map your [includes]
:
[requirements]
amalulla/cpp-expression-parser: 2
[includes]
shunting-yard.h: amalulla/cpp-expression-parser
Now, checking again bii deps shows all resolved dependencies.
Just build the project and run this example!
$ bii build
$ cd bin
$ # run executable
You can see the results of the parsed expressions in the output:
$ '-pi+1' calculated result is -2.14.
$ '10*gravity' calculated result is 97.8.
This example is already in biicode at examples/expression_parser.
To give it a simple run, just open the block and build it like this:
$ bii init exp-parser_example
$ cd exp-parser_example
$ bii open examples/expression_parser
$ bii build
$ cd bin
$ # run executable
Here is the output:
$ '-pi+1' calculated result is -2.14.
$ '10*gravity' calculated result is 97.8.
Any doubts? Do not hesitate to contact us visit our forum and feel free to ask any questions.