1 /********************* */
4 ** Original author: dejan
5 ** Major contributors: cconway, mdeters
6 ** Minor contributors (to current version): ajreynol
7 ** This file is part of the CVC4 prototype.
8 ** Copyright (c) 2009, 2010, 2011 The Analysis of Computer Systems Group (ACSys)
9 ** Courant Institute of Mathematical Sciences
10 ** New York University
11 ** See the file COPYING in the top-level source directory for licensing
12 ** information.\endverbatim
14 ** \brief Parser state implementation.
16 ** Parser state implementation.
24 #include "parser/input.h"
25 #include "parser/parser.h"
26 #include "parser/parser_exception.h"
27 #include "expr/command.h"
28 #include "expr/expr.h"
29 #include "expr/kind.h"
30 #include "expr/type.h"
31 #include "util/output.h"
32 #include "util/options.h"
33 #include "util/Assert.h"
34 #include "parser/cvc/cvc_input.h"
35 #include "parser/smt/smt_input.h"
38 using namespace CVC4::kind
;
43 Parser::Parser(ExprManager
* exprManager
, Input
* input
, bool strictMode
, bool parseOnly
) :
44 d_exprManager(exprManager
),
46 d_declScopeAllocated(),
47 d_declScope(&d_declScopeAllocated
),
48 d_anonymousFunctionCount(0),
50 d_checksEnabled(true),
51 d_strictMode(strictMode
),
52 d_parseOnly(parseOnly
) {
53 d_input
->setParser(*this);
56 Expr
Parser::getSymbol(const std::string
& name
, SymbolType type
) {
57 checkDeclaration(name
, CHECK_DECLARED
, type
);
58 Assert( isDeclared(name
, type
) );
62 case SYM_VARIABLE
: // Functions share var namespace
63 return d_declScope
->lookup(name
);
71 Expr
Parser::getVariable(const std::string
& name
) {
72 return getSymbol(name
, SYM_VARIABLE
);
75 Expr
Parser::getFunction(const std::string
& name
) {
76 return getSymbol(name
, SYM_VARIABLE
);
79 Type
Parser::getType(const std::string
& var_name
,
81 checkDeclaration(var_name
, CHECK_DECLARED
, type
);
82 Assert( isDeclared(var_name
, type
) );
83 Type t
= getSymbol(var_name
, type
).getType();
87 Type
Parser::getSort(const std::string
& name
) {
88 checkDeclaration(name
, CHECK_DECLARED
, SYM_SORT
);
89 Assert( isDeclared(name
, SYM_SORT
) );
90 Type t
= d_declScope
->lookupType(name
);
94 Type
Parser::getSort(const std::string
& name
,
95 const std::vector
<Type
>& params
) {
96 checkDeclaration(name
, CHECK_DECLARED
, SYM_SORT
);
97 Assert( isDeclared(name
, SYM_SORT
) );
98 Type t
= d_declScope
->lookupType(name
, params
);
102 size_t Parser::getArity(const std::string
& sort_name
){
103 checkDeclaration(sort_name
, CHECK_DECLARED
, SYM_SORT
);
104 Assert( isDeclared(sort_name
, SYM_SORT
) );
105 return d_declScope
->lookupArity(sort_name
);
108 /* Returns true if name is bound to a boolean variable. */
109 bool Parser::isBoolean(const std::string
& name
) {
110 return isDeclared(name
, SYM_VARIABLE
) && getType(name
).isBoolean();
113 /* Returns true if name is bound to a function-like thing (function,
114 * constructor, tester, or selector). */
115 bool Parser::isFunctionLike(const std::string
& name
) {
116 if(!isDeclared(name
, SYM_VARIABLE
)) {
119 Type type
= getType(name
);
120 return type
.isFunction() || type
.isConstructor() ||
121 type
.isTester() || type
.isSelector();
124 /* Returns true if name is bound to a defined function. */
125 bool Parser::isDefinedFunction(const std::string
& name
) {
126 // more permissive in type than isFunction(), because defined
127 // functions can be zero-ary and declared functions cannot.
128 return d_declScope
->isBoundDefinedFunction(name
);
131 /* Returns true if the Expr is a defined function. */
132 bool Parser::isDefinedFunction(Expr func
) {
133 // more permissive in type than isFunction(), because defined
134 // functions can be zero-ary and declared functions cannot.
135 return d_declScope
->isBoundDefinedFunction(func
);
138 /* Returns true if name is bound to a function returning boolean. */
139 bool Parser::isPredicate(const std::string
& name
) {
140 return isDeclared(name
, SYM_VARIABLE
) && getType(name
).isPredicate();
144 Parser::mkVar(const std::string
& name
, const Type
& type
) {
145 Debug("parser") << "mkVar(" << name
<< ", " << type
<< ")" << std::endl
;
146 Expr expr
= d_exprManager
->mkVar(name
, type
);
147 defineVar(name
, expr
);
152 Parser::mkFunction(const std::string
& name
, const Type
& type
) {
153 Debug("parser") << "mkVar(" << name
<< ", " << type
<< ")" << std::endl
;
154 Expr expr
= d_exprManager
->mkVar(name
, type
);
155 defineFunction(name
, expr
);
160 Parser::mkAnonymousFunction(const std::string
& prefix
, const Type
& type
) {
162 name
<< prefix
<< ':' << ++d_anonymousFunctionCount
;
163 return mkFunction(name
.str(), type
);
167 Parser::mkVars(const std::vector
<std::string
> names
,
169 std::vector
<Expr
> vars
;
170 for(unsigned i
= 0; i
< names
.size(); ++i
) {
171 vars
.push_back(mkVar(names
[i
], type
));
177 Parser::defineVar(const std::string
& name
, const Expr
& val
) {
178 d_declScope
->bind(name
, val
);
179 Assert( isDeclared(name
) );
183 Parser::defineFunction(const std::string
& name
, const Expr
& val
) {
184 d_declScope
->bindDefinedFunction(name
, val
);
185 Assert( isDeclared(name
) );
189 Parser::defineType(const std::string
& name
, const Type
& type
) {
190 d_declScope
->bindType(name
, type
);
191 Assert( isDeclared(name
, SYM_SORT
) );
195 Parser::defineType(const std::string
& name
,
196 const std::vector
<Type
>& params
,
198 d_declScope
->bindType(name
, params
, type
);
199 Assert( isDeclared(name
, SYM_SORT
) );
203 Parser::defineParameterizedType(const std::string
& name
,
204 const std::vector
<Type
>& params
,
206 if(Debug
.isOn("parser")) {
207 Debug("parser") << "defineParameterizedType(" << name
<< ", " << params
.size() << ", [";
208 if(params
.size() > 0) {
209 copy(params
.begin(), params
.end() - 1,
210 ostream_iterator
<Type
>(Debug("parser"), ", ") );
211 Debug("parser") << params
.back();
213 Debug("parser") << "], " << type
<< ")" << std::endl
;
215 defineType(name
, params
, type
);
219 Parser::mkSort(const std::string
& name
) {
220 Debug("parser") << "newSort(" << name
<< ")" << std::endl
;
221 Type type
= d_exprManager
->mkSort(name
);
222 defineType(name
, type
);
227 Parser::mkSortConstructor(const std::string
& name
, size_t arity
) {
228 Debug("parser") << "newSortConstructor(" << name
<< ", " << arity
<< ")"
230 SortConstructorType type
= d_exprManager
->mkSortConstructor(name
, arity
);
231 defineType(name
, vector
<Type
>(arity
), type
);
235 SortType
Parser::mkUnresolvedType(const std::string
& name
) {
236 SortType unresolved
= mkSort(name
);
237 d_unresolved
.insert(unresolved
);
242 Parser::mkUnresolvedTypeConstructor(const std::string
& name
,
244 SortConstructorType unresolved
= mkSortConstructor(name
, arity
);
245 d_unresolved
.insert(unresolved
);
250 Parser::mkUnresolvedTypeConstructor(const std::string
& name
,
251 const std::vector
<Type
>& params
) {
252 Debug("parser") << "newSortConstructor(P)(" << name
<< ", " << params
.size() << ")"
254 SortConstructorType unresolved
= d_exprManager
->mkSortConstructor(name
, params
.size());
255 defineType(name
, params
, unresolved
);
256 Type t
= getSort( name
, params
);
257 d_unresolved
.insert(unresolved
);
261 bool Parser::isUnresolvedType(const std::string
& name
) {
262 if(!isDeclared(name
, SYM_SORT
)) {
265 return d_unresolved
.find(getSort(name
)) != d_unresolved
.end();
268 std::vector
<DatatypeType
>
269 Parser::mkMutualDatatypeTypes(const std::vector
<Datatype
>& datatypes
) {
271 std::vector
<DatatypeType
> types
=
272 d_exprManager
->mkMutualDatatypeTypes(datatypes
, d_unresolved
);
274 Assert(datatypes
.size() == types
.size());
276 for(unsigned i
= 0; i
< datatypes
.size(); ++i
) {
277 DatatypeType t
= types
[i
];
278 const Datatype
& dt
= t
.getDatatype();
279 const std::string
& name
= dt
.getName();
280 Debug("parser-idt") << "define " << name
<< " as " << t
<< std::endl
;
281 if(isDeclared(name
, SYM_SORT
)) {
282 throw ParserException(name
+ " already declared");
284 if( t
.isParametric() ) {
285 std::vector
< Type
> paramTypes
= t
.getParamTypes();
286 defineType(name
, paramTypes
, t
);
290 for(Datatype::const_iterator j
= dt
.begin(),
294 const DatatypeConstructor
& ctor
= *j
;
295 Expr::printtypes::Scope
pts(Debug("parser-idt"), true);
296 Expr constructor
= ctor
.getConstructor();
297 Debug("parser-idt") << "+ define " << constructor
<< std::endl
;
298 string constructorName
= constructor
.toString();
299 if(isDeclared(constructorName
, SYM_VARIABLE
)) {
300 throw ParserException(constructorName
+ " already declared");
302 defineVar(constructorName
, constructor
);
303 Expr tester
= ctor
.getTester();
304 Debug("parser-idt") << "+ define " << tester
<< std::endl
;
305 string testerName
= tester
.toString();
306 if(isDeclared(testerName
, SYM_VARIABLE
)) {
307 throw ParserException(testerName
+ " already declared");
309 defineVar(testerName
, tester
);
310 for(DatatypeConstructor::const_iterator k
= ctor
.begin(),
314 Expr selector
= (*k
).getSelector();
315 Debug("parser-idt") << "+++ define " << selector
<< std::endl
;
316 string selectorName
= selector
.toString();
317 if(isDeclared(selectorName
, SYM_VARIABLE
)) {
318 throw ParserException(selectorName
+ " already declared");
320 defineVar(selectorName
, selector
);
325 // These are no longer used, and the ExprManager would have
326 // complained of a bad substitution if anything is left unresolved.
327 // Clear out the set.
328 d_unresolved
.clear();
333 DatatypeType
Parser::mkRecordType(const std::vector
< std::pair
<std::string
, Type
> >& typeIds
) {
334 DatatypeType
& dtt
= d_recordTypes
[typeIds
];
336 Datatype
dt("__cvc4_record");
337 Debug("datatypes") << "make new record_ctor" << std::endl
;
338 DatatypeConstructor
c("__cvc4_record_ctor");
339 for(std::vector
< std::pair
<std::string
, Type
> >::const_iterator i
= typeIds
.begin(); i
!= typeIds
.end(); ++i
) {
340 c
.addArg((*i
).first
, (*i
).second
);
342 dt
.addConstructor(c
);
343 dtt
= d_exprManager
->mkDatatypeType(dt
);
345 Debug("datatypes") << "use old record_ctor" << std::endl
;
350 DatatypeType
Parser::mkTupleType(const std::vector
<Type
>& types
) {
351 DatatypeType
& dtt
= d_tupleTypes
[types
];
353 Datatype
dt("__cvc4_tuple");
354 DatatypeConstructor
c("__cvc4_tuple_ctor");
355 for(std::vector
<Type
>::const_iterator i
= types
.begin(); i
!= types
.end(); ++i
) {
356 c
.addArg("__cvc4_tuple_stor", *i
);
358 dt
.addConstructor(c
);
359 dtt
= d_exprManager
->mkDatatypeType(dt
);
364 bool Parser::isDeclared(const std::string
& name
, SymbolType type
) {
367 return d_declScope
->isBound(name
);
369 return d_declScope
->isBoundType(name
);
375 void Parser::checkDeclaration(const std::string
& varName
,
376 DeclarationCheck check
,
378 throw(ParserException
) {
379 if(!d_checksEnabled
) {
385 if( !isDeclared(varName
, type
) ) {
386 parseError("Symbol " + varName
+ " not declared as a " +
387 (type
== SYM_VARIABLE
? "variable" : "type"));
391 case CHECK_UNDECLARED
:
392 if( isDeclared(varName
, type
) ) {
393 parseError("Symbol " + varName
+ " previously declared as a " +
394 (type
== SYM_VARIABLE
? "variable" : "type"));
406 void Parser::checkFunctionLike(const std::string
& name
) throw(ParserException
) {
407 if(d_checksEnabled
&& !isFunctionLike(name
)) {
408 parseError("Expecting function-like symbol, found '" + name
+ "'");
412 void Parser::checkArity(Kind kind
, unsigned numArgs
)
413 throw(ParserException
) {
414 if(!d_checksEnabled
) {
418 unsigned min
= d_exprManager
->minArity(kind
);
419 unsigned max
= d_exprManager
->maxArity(kind
);
421 if( numArgs
< min
|| numArgs
> max
) {
424 if( numArgs
< min
) {
425 ss
<< "at least " << min
<< " ";
427 ss
<< "at most " << max
<< " ";
429 ss
<< "arguments for operator '" << kind
<< "', ";
430 ss
<< "found " << numArgs
;
431 parseError(ss
.str());
435 void Parser::checkOperator(Kind kind
, unsigned numArgs
) throw(ParserException
) {
436 if( d_strictMode
&& d_logicOperators
.find(kind
) == d_logicOperators
.end() ) {
437 parseError( "Operator is not defined in the current logic: " + kindToString(kind
) );
439 checkArity(kind
, numArgs
);
442 void Parser::addOperator(Kind kind
) {
443 d_logicOperators
.insert(kind
);
446 void Parser::preemptCommand(Command
* cmd
) {
447 d_commandQueue
.push_back(cmd
);
450 Command
* Parser::nextCommand() throw(ParserException
) {
451 Debug("parser") << "nextCommand()" << std::endl
;
453 if(!d_commandQueue
.empty()) {
454 cmd
= d_commandQueue
.front();
455 d_commandQueue
.pop_front();
462 cmd
= d_input
->parseCommand();
463 d_commandQueue
.push_back(cmd
);
464 cmd
= d_commandQueue
.front();
465 d_commandQueue
.pop_front();
469 } catch(ParserException
& e
) {
472 } catch(Exception
& e
) {
475 // set the language of the stream, otherwise if it contains
476 // Exprs or Types it prints in the AST language
477 OutputLanguage outlang
=
478 language::toOutputLanguage(d_input
->getLanguage());
479 ss
<< Expr::setlanguage(outlang
) << e
;
480 parseError( ss
.str() );
484 Debug("parser") << "nextCommand() => " << cmd
<< std::endl
;
488 Expr
Parser::nextExpression() throw(ParserException
) {
489 Debug("parser") << "nextExpression()" << std::endl
;
493 result
= d_input
->parseExpr();
494 if(result
.isNull()) {
497 } catch(ParserException
& e
) {
500 } catch(Exception
& e
) {
504 parseError( ss
.str() );
507 Debug("parser") << "nextExpression() => " << result
<< std::endl
;
512 }/* CVC4::parser namespace */
513 }/* CVC4 namespace */