Clingo
Loading...
Searching...
No Matches
ast.c

The example shows how to work with the AST.

The example shows how to work with the AST.

Output

./ast
Answer: a

Code

#include <clingo/ast.h>
#include <clingo/control.h>
#include <clingo/solve.h>
#include <clingo/symbol.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHECK(call) \
do { \
if (!(call)) { \
status = false; \
goto cleanup; \
} \
} while (0)
bool print_symbols(clingo_symbol_t const *symbols, size_t size, void *data) {
bool status = true;
for (size_t i = 0; i < size; ++i) {
CHECK(clingo_symbol_to_string(symbols[i], bld));
clingo_string_t str = {NULL, 0};
CHECK(clingo_string_builder_string(bld, &str));
printf(" %.*s", (int)str.size, str.data);
}
cleanup:
return status;
}
bool on_model(clingo_model_t *model, void *data, bool *goon) {
bool status = true;
*goon = true;
printf("Answer:");
CHECK(clingo_model_symbols(model, clingo_show_type_shown, &print_symbols, data));
printf("\n");
cleanup:
return status;
}
// Build ":- not a." as a statement AST.
bool build_constraint(clingo_lib_t *lib, clingo_location_t const *loc, clingo_ast_t **rule) {
bool status = true;
char const *atom_str = "a";
clingo_ast_t *args = NULL;
clingo_ast_t *term = NULL;
clingo_ast_t *fail = NULL;
clingo_ast_t *atom = NULL;
clingo_ast_t *body = NULL;
clingo_ast_t *head = NULL;
// Construct emty argument tuple "()".
CHECK(clingo_ast_construct(lib, clingo_ast_type_argument_tuple, &args, NULL, (size_t)0));
// Construct function term "a()" equivalent to "a".
CHECK(clingo_ast_construct(lib, clingo_ast_type_term_function, &term, loc, atom_str, strlen(atom_str), &args,
(size_t)1, 0));
// Construct simple literal "not a".
CHECK(clingo_ast_construct(lib, clingo_ast_type_literal_symbolic, &atom, loc, 1, term));
// Construct body literal "not a".
CHECK(clingo_ast_construct(lib, clingo_ast_type_body_simple_literal, &body, atom));
// Construct simple literal "#false".
CHECK(clingo_ast_construct(lib, clingo_ast_type_literal_boolean, &fail, loc, 0, false));
// Construct head literal "#false".
CHECK(clingo_ast_construct(lib, clingo_ast_type_head_simple_literal, &head, fail));
// Construct integrity constraint ":- not a.".
CHECK(clingo_ast_construct(lib, clingo_ast_type_statement_rule, rule, loc, head, &body, (size_t)1));
cleanup:
return status;
}
// Simple example that shows how to work with ASTs in C.
int main(void) {
bool status = true;
clingo_lib_t *lib = NULL;
size_t const limit = 20;
clingo_control_t *ctl = NULL;
clingo_solve_handle_t *hnd = NULL;
clingo_solve_event_handler_t seh = {&on_model, NULL, NULL, NULL, NULL};
char const *model_str = "0";
char const *base_str = "base";
clingo_string_t str = {model_str, strlen(model_str)};
clingo_part_t part = {base_str, strlen(base_str), NULL, 0};
clingo_program_t *prog = NULL;
clingo_location_t const *loc = NULL;
clingo_ast_t *parsed = NULL;
clingo_ast_t *built = NULL;
char const *stm = "{a}.";
CHECK(clingo_lib_new(flags, level, NULL, NULL, limit, &lib));
CHECK(clingo_control_new(lib, &str, 1, &ctl));
// Parse "{a}." as a statement AST.
CHECK(clingo_ast_parse_expression(lib, clingo_ast_parse_type_statement, stm, strlen(stm), &parsed));
CHECK(clingo_ast_attribute_get_location(parsed, clingo_ast_attribute_location, &loc));
// Build ":- not a." as a statement AST.
CHECK(build_constraint(lib, loc, &built));
// Create a program and add both statements.
CHECK(clingo_program_new(lib, &prog));
CHECK(clingo_program_add(prog, parsed));
CHECK(clingo_program_add(prog, built));
// Add program to control.
CHECK(clingo_control_join(ctl, prog));
// Ground and solve.
CHECK(clingo_control_ground(ctl, &part, 1, NULL, NULL));
CHECK(clingo_control_solve(ctl, clingo_solve_mode_default, NULL, 0, &seh, bld, &hnd));
CHECK(clingo_solve_handle_get(hnd, &res));
cleanup:
if (!status) {
clingo_string_t err_msg = {NULL, 0};
clingo_result_t code = 0;
clingo_get_error(&code, &err_msg);
fprintf(stderr, "Error: %.*s\n", (int)err_msg.size, err_msg.data);
}
clingo_ast_free(parsed);
return status ? EXIT_SUCCESS : EXIT_FAILURE;
}
CLINGO_VISIBILITY_DEFAULT void clingo_program_free(clingo_program_t *program)
Destroy the given program object.
struct clingo_ast clingo_ast_t
This struct provides a view to nodes in the AST.
Definition ast.h:157
struct clingo_program clingo_program_t
Object to store.
Definition ast.h:493
CLINGO_VISIBILITY_DEFAULT bool clingo_program_new(clingo_lib_t *lib, clingo_program_t **program)
Create an empty non-ground program.
CLINGO_VISIBILITY_DEFAULT void clingo_ast_free(clingo_ast_t *ast)
Free an AST node.
CLINGO_VISIBILITY_DEFAULT bool clingo_program_add(clingo_program_t *program, clingo_ast_t *statement)
Adds a statement to the program.
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_attribute_get_location(clingo_ast_t *ast, clingo_ast_attribute_t attribute, clingo_location_t const **value)
Get the value of a location attribute.
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_parse_expression(clingo_lib_t *lib, clingo_ast_parse_type_t type, char const *string, size_t size, clingo_ast_t **ast)
Parse a single expression given as string.
CLINGO_VISIBILITY_DEFAULT bool clingo_ast_construct(clingo_lib_t *lib, clingo_ast_type_t type, clingo_ast_t **ast,...)
Construct an AST of the given type.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_join(clingo_control_t *control, clingo_program_t const *program)
Extend the control objects's program with the given one.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_ground(clingo_control_t *control, clingo_part_t const *parts, size_t size, clingo_ground_callback_t ground_callback, void *data)
Ground the selected parts of the current (non-ground) logic program.
CLINGO_VISIBILITY_DEFAULT bool clingo_control_new(clingo_lib_t *lib, clingo_string_t const *arguments, size_t size, clingo_control_t **control)
Create a new control object.
CLINGO_VISIBILITY_DEFAULT void clingo_control_release(clingo_control_t *control)
Decrement the reference count of the given control object and destroy if zero.
CLINGO_VISIBILITY_DEFAULT void clingo_lib_release(clingo_lib_t *lib)
Release a library object created with clingo_lib_new().
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_free(clingo_string_builder_t const *bld)
Free the string builder.
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_clear(clingo_string_builder_t *bld)
Clear the string in the builder.
CLINGO_VISIBILITY_DEFAULT bool clingo_lib_new(clingo_lib_flags_t flags, clingo_log_level_t level, clingo_logger_t const *logger, void *data, size_t limit, clingo_lib_t **lib)
Create a library object.
int clingo_log_level_t
Corresponding type to clingo_log_level_e.
Definition core.h:149
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:171
struct clingo_location clingo_location_t
Represents a source code location marking its beginning and end.
Definition core.h:354
CLINGO_VISIBILITY_DEFAULT void clingo_get_error(clingo_result_t *code, clingo_string_t *message)
Get the error set in the current thread.
struct clingo_string_builder clingo_string_builder_t
A builder for strings.
Definition core.h:252
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_string(clingo_string_builder_t const *bld, clingo_string_t *value)
Get the (zero-terminated) string in the builder.
int clingo_result_t
Corresponding type to clingo_result_e.
Definition core.h:106
uint32_t clingo_lib_flags_t
Bitset of clingo_lib_flags_e.
Definition core.h:180
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_new(clingo_string_builder_t **bld)
Create a new string builder.
@ clingo_log_level_info
the info level
Definition core.h:144
@ clingo_lib_flags_slotted
use custom allocator for storing symbols
Definition core.h:175
@ clingo_lib_flags_fast_release
whether to enable fast release of libraries
Definition core.h:177
CLINGO_VISIBILITY_DEFAULT bool clingo_model_symbols(clingo_model_t const *model, clingo_show_type_bitset_t show, clingo_symbol_callback_t callback, void *data)
Get the symbols of the selected types in the model.
struct clingo_model clingo_model_t
Object representing a model.
Definition model.h:43
@ clingo_show_type_shown
Select shown atoms and terms.
Definition model.h:56
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition solve.h:112
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_close(clingo_solve_handle_t *handle)
Stops the running search and releases the handle.
unsigned clingo_solve_result_bitset_t
Corresponding type to clingo_solve_result_e.
Definition solve.h:61
CLINGO_VISIBILITY_DEFAULT bool clingo_control_solve(clingo_control_t *control, clingo_solve_mode_bitset_t mode, clingo_literal_t const *assumptions, size_t assumptions_size, clingo_solve_event_handler_t const *handler, void *data, clingo_solve_handle_t **handle)
Solve the currently grounded logic program enumerating its models.
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_get(clingo_solve_handle_t *handle, clingo_solve_result_bitset_t *result)
Get the next solve result.
@ clingo_solve_result_empty
An uninitialized solve result.
Definition solve.h:54
@ clingo_solve_mode_default
The defalut solve mode.
Definition solve.h:65
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, clingo_string_builder_t *builder)
Get the string representation of a symbol.
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
auto main(Library &lib, std::span< std::string_view const > arguments={}, App *app=nullptr, bool raise_errors=false) -> int
Run a clingo based application with the given arguments.
Definition app.hh:221
@ atom
Check if term is an atom.
Struct used to specify the program parts that have to be grounded.
Definition control.h:57
The solve event handler interface.
Definition solve.h:74
Struct to capture strings that are not null-terminated.
Definition core.h:86
size_t size
the length of the string
Definition core.h:88
char const * data
pointer to the beginning of the string
Definition core.h:87