Parser with Shared Symbol Manager

This example shows how to use the parser via the parser API and use it to parse additional terms in another input source.

examples/api/cpp/parser_sym_manager.cpp

 1/******************************************************************************
 2 * This file is part of the cvc5 project.
 3 *
 4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
 5 * in the top-level source directory and their institutional affiliations.
 6 * All rights reserved.  See the file COPYING in the top-level source
 7 * directory for licensing information.
 8 * ****************************************************************************
 9 *
10 * A simple demonstration of using multiple parsers with the same symbol manager
11 * via C++ API.
12 */
13
14#include <cvc5/cvc5.h>
15#include <cvc5/cvc5_parser.h>
16
17#include <iostream>
18
19using namespace cvc5;
20using namespace cvc5::parser;
21
22int main()
23{
24  TermManager tm;
25  Solver slv(tm);
26
27  SymbolManager sm(tm);
28
29  // construct an input parser associated the solver above
30  InputParser parser(&slv, &sm);
31
32  std::stringstream ss;
33  ss << "(set-logic QF_LIA)" << std::endl;
34  ss << "(declare-fun a () Int)" << std::endl;
35  ss << "(declare-fun b () Int)" << std::endl;
36  ss << "(declare-fun c () Bool)" << std::endl;
37  parser.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss, "MyStream");
38
39  // parse commands until finished
40  Command cmd;
41  while (true)
42  {
43    cmd = parser.nextCommand();
44    if (cmd.isNull())
45    {
46      break;
47    }
48    std::cout << "Executing command " << cmd << ":" << std::endl;
49    // invoke the command on the solver and the symbol manager, print the result
50    // to std::cout
51    cmd.invoke(&slv, &sm, std::cout);
52  }
53  std::cout << "Finished parsing commands" << std::endl;
54
55  // Note that sm now has a,b,c in its symbol table.
56
57  // Now, construct a new parser with the same symbol manager. We will parse
58  // terms with it.
59  InputParser parser2(&slv, &sm);
60  std::stringstream ss2;
61  ss2 << "(+ a b)" << std::endl;
62  ss2 << "(- a 10)" << std::endl;
63  ss2 << "(>= 0 45)" << std::endl;
64  ss2 << "(and c c)" << std::endl;
65  ss2 << "true" << std::endl;
66  parser2.setStreamInput(modes::InputLanguage::SMT_LIB_2_6, ss2, "MyStream2");
67
68  // parse terms until finished
69  Term t;
70  do
71  {
72    t = parser2.nextTerm();
73    std::cout << "Parsed term: " << t << std::endl;
74  } while (!t.isNull());
75  std::cout << "Finished parsing terms" << std::endl;
76}