mirror of
https://github.com/cpm-cmake/CPM.cmake.git
synced 2025-11-17 06:37:43 -05:00
rename examples test.cpp -> main.cpp (#20)
This commit is contained in:
27
examples/simple/main.cpp
Normal file
27
examples/simple/main.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <lars/parser/generator.h>
|
||||
|
||||
int main() {
|
||||
lars::ParserGenerator<float> g;
|
||||
|
||||
// Define grammar and evaluation rules
|
||||
g.setSeparator(g["Whitespace"] << "[\t ]");
|
||||
g["Sum" ] << "Add | Subtract | Product";
|
||||
g["Product" ] << "Multiply | Divide | Atomic";
|
||||
g["Atomic" ] << "Number | '(' Sum ')'";
|
||||
g["Add" ] << "Sum '+' Product" >> [](auto e){ return e[0].evaluate() + e[1].evaluate(); };
|
||||
g["Subtract"] << "Sum '-' Product" >> [](auto e){ return e[0].evaluate() - e[1].evaluate(); };
|
||||
g["Multiply"] << "Product '*' Atomic" >> [](auto e){ return e[0].evaluate() * e[1].evaluate(); };
|
||||
g["Divide" ] << "Product '/' Atomic" >> [](auto e){ return e[0].evaluate() / e[1].evaluate(); };
|
||||
g["Number" ] << "'-'? [0-9]+ ('.' [0-9]+)?" >> [](auto e){ return stof(e.string()); };
|
||||
g.setStart(g["Sum"]);
|
||||
|
||||
// Execute a string
|
||||
float result = g.run("1 + 2 * (3+4)/2 - 3");
|
||||
|
||||
// validate result
|
||||
if (result == 5) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user