Parser

This example shows how to use the parser via the parser API.

examples/api/cpp/parser.cpp

 1/******************************************************************************
 2 * Top contributors (to current version):
 3 *   Andrew Reynolds, Aina Niemetz
 4 *
 5 * This file is part of the cvc5 project.
 6 *
 7 * Copyright (c) 2009-2024 by the authors listed in the file AUTHORS
 8 * in the top-level source directory and their institutional affiliations.
 9 * All rights reserved.  See the file COPYING in the top-level source
10 * directory for licensing information.
11 * ****************************************************************************
12 *
13 * A simple demonstration of using the parser via C++ API.
14 */
15
16#include <cvc5/cvc5.h>
17#include <cvc5/cvc5_parser.h>
18
19#include <iostream>
20
21using namespace cvc5;
22using namespace cvc5::parser;
23
24int main()
25{
26  TermManager tm;
27  Solver slv(tm);
28
29  // set that we should print success after each successful command
30  slv.setOption("print-success", "true");
31
32  // construct an input parser associated the solver above
33  InputParser parser(&slv);
34
35  std::stringstream ss;
36  ss << "(set-logic QF_LIA)" << std::endl;
37  ss << "(declare-fun a () Int)" << std::endl;
38  ss << "(declare-fun b () Int)" << std::endl;
39  ss << "(declare-fun c () Int)" << std::endl;
40  ss << "(assert (> a (+ b c)))" << std::endl;
41  ss << "(assert (< a b))" << std::endl;
42  ss << "(assert (> c 0))" << std::endl;
43  parser.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss, "MyStream");
44
45  // get the symbol manager of the parser, used when invoking commands below
46  SymbolManager* sm = parser.getSymbolManager();
47
48  // parse commands until finished
49  Command cmd;
50  while (true)
51  {
52    cmd = parser.nextCommand();
53    if (cmd.isNull())
54    {
55      break;
56    }
57    std::cout << "Executing command " << cmd << ":" << std::endl;
58    // invoke the command on the solver and the symbol manager, print the result
59    // to std::cout
60    cmd.invoke(&slv, sm, std::cout);
61  }
62  std::cout << "Finished parsing commands" << std::endl;
63
64  // now, check sat with the solver
65  Result r = slv.checkSat();
66  std::cout << "expected: unsat" << std::endl;
67  std::cout << "result: " << r << std::endl;
68}