CBMC
Loading...
Searching...
No Matches
smt2irep.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "smt2irep.h"
10
11#include <util/message.h>
12
13#include <stack>
14
15#include "smt2_tokenizer.h"
16
18{
19public:
20 smt2irept(std::istream &_in, message_handlert &message_handler)
21 : smt2_tokenizert(_in), log(message_handler)
22 {
23 }
24
25 std::optional<irept> operator()();
26
27protected:
29};
30
31std::optional<irept> smt2irept::operator()()
32{
33 try
34 {
35 std::stack<irept> stack;
36
37 while(true)
38 {
39 auto token = next_token();
40
41 switch(token)
42 {
43 case END_OF_FILE:
44 if(stack.empty())
45 return {};
46 else
47 throw error("unexpected end of file");
48
49 case STRING_LITERAL:
50 case NUMERAL:
51 case SYMBOL:
52 if(stack.empty())
53 return irept{token.text}; // all done!
54 else
55 stack.top().get_sub().push_back(irept{token.text});
56 break;
57
58 case OPEN: // '('
59 // produce sub-irep
60 stack.push(irept());
61 break;
62
63 case CLOSE: // ')'
64 // done with sub-irep
65 if(stack.empty())
66 throw error("unexpected ')'");
67 else
68 {
69 irept tmp = stack.top();
70 stack.pop();
71
72 if(stack.empty())
73 return tmp; // all done!
74
75 stack.top().get_sub().push_back(tmp);
76 break;
77 }
78
79 case NONE:
80 case KEYWORD:
81 throw error("unexpected token");
82 }
83 }
84 }
85 catch(const smt2_errort &e)
86 {
88 log.error() << e.what() << messaget::eom;
89 return {};
90 }
91}
92
93std::optional<irept>
94smt2irep(std::istream &in, message_handlert &message_handler)
95{
96 return smt2irept(in, message_handler)();
97}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition irep.h:364
subt & get_sub()
Definition irep.h:448
source_locationt source_location
Definition message.h:239
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
mstreamt & error() const
Definition message.h:401
static eomt eom
Definition message.h:289
Exception thrown by the tokenizer (and the parser built on top of it) to report a syntactic error at ...
tokent next_token()
Consume and return the next token.
smt2_errort error() const
generate an error exception
messaget log
Definition smt2irep.cpp:28
std::optional< irept > operator()()
Definition smt2irep.cpp:31
smt2irept(std::istream &_in, message_handlert &message_handler)
Definition smt2irep.cpp:20
void set_line(const irep_idt &line)
Tokenizer for the SMT-LIB v2.6 syntax.
std::optional< irept > smt2irep(std::istream &in, message_handlert &message_handler)
returns an irep for an SMT-LIB2 expression read from a given stream returns {} when EOF is encountere...
Definition smt2irep.cpp:94