An extension of the C library expr, that recognises Computation Tree Logic and produces an AST
This project uses CMake as the build tool. Dependencies are handled automatically with cpm, and the library is also built to be compatible with your project if you use cpm as well.
mkdir bin && cd bin
cmake .. && make
Below is a general usage example, this project also ships a demo executable
that prints the AST to stdout. For linking, simply link with the libctl.so
library.
...
std::istringstream iss{"E F a > 3"}; // inputs must be wrapped in a stream
ctl::ast_factory factory{}; // initialize an overridable ast factory
ctl::multi_query_builder builder{}; // initialize an overridable language builder
ctl::scanner scn{iss, std::cerr, &factory}; // initialize the scanner (print errors to cerr)
ctl::parser_args pargs{&scn, &factory, &builder}; // wrap arguments to the parser
ctl::parser p{pargs}; // initialize the parser
if(p.parse() != 0) // parse the expression stream and handle error(s) if they occur
throw std::logic_error("unable to parse query expression");
auto result = builder.build().queries; // build and return the queries
for(int i = 0; i < result.size(); i ) // print the resulting AST(s)
std::cout << i << " ==> " << result[i] << "\n";
...
Using a library such as yalibs/overload together with std::visit, you can traverse the ast recursively with type-dependent actions
#include <overload> // include yalibs/overload
#include <algorithm> // include std::visit
/* parse an expression (see above example) */
...
auto ast = builder.build().queries;
// Do an action depending on the type of the node
std::visit(ya::overload(
[&ast](const expr::syntax_tree_t &v) { }, // See the expr library dependency
[&ast](const expr::operator_t &v) { },
[&ast](const expr::root_t &v) { },
[&ast](const expr::symbol_value_t &v) { },
[&ast](const ctl::location_t &v) { },
[&ast](const ctl::modal_t &v) { },
[&ast](const ctl::quantifier_t &v) { }
), static_cast<ctl::unerlying_syntax_node_t>(ast.node));
for(auto& child : ast.children) {
// Call a function on children of the tree
}
...
- sillydan1/expr @ v1.6.0
- sillydan1/argvparse @ v1.2.3 (only used for the demo executable)
- yalibs/yatree @ v1.0.1
- yalibs/yaoverload @ v1.0.0
- yalibs/yahashcombine @ v1.0.0