Skip to content

An extension of expr that recognises Computation Tree Logic

License

Notifications You must be signed in to change notification settings

sillydan1/ctl-expr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CTL expr

An extension of the C library expr, that recognises Computation Tree Logic and produces an AST

Compile

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

Usage

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";
...

AST Traversal

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
}
...

CPM Dependencies

About

An extension of expr that recognises Computation Tree Logic

Resources

License

Stars

Watchers

Forks

Packages

No packages published