Replace `Debug` by `Trace` (#7793)
[cvc5.git] / src / parser / smt2 / Smt2.g
1 /* ****************************************************************************
2 * Top contributors (to current version):
3 * Andrew Reynolds, Morgan Deters, Christopher L. Conway
4 *
5 * This file is part of the cvc5 project.
6 *
7 * Copyright (c) 2009-2021 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 * Parser for SMT-LIB v2 input language.
14 */
15
16 grammar Smt2;
17
18 options {
19 // C output for antlr
20 language = 'C';
21
22 // Skip the default error handling, just break with exceptions
23 // defaultErrorHandler = false;
24
25 // Only lookahead of <= k requested (disable for LL* parsing)
26 // Note that cvc5's BoundedTokenBuffer requires a fixed k !
27 // If you change this k, change it also in smt2_input.cpp !
28 k = 2;
29 }/* options */
30
31 @header {
32 /* ****************************************************************************
33 * This file is part of the cvc5 project.
34 *
35 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
36 * in the top-level source directory and their institutional affiliations.
37 * All rights reserved. See the file COPYING in the top-level source
38 * directory for licensing information.
39 * ****************************************************************************
40 */
41 }/* @header */
42
43 @lexer::includes {
44
45 /** This suppresses warnings about the redefinition of token symbols between
46 * different parsers. The redefinitions should be harmless as long as no
47 * client: (a) #include's the lexer headers for two grammars AND (b) uses the
48 * token symbol definitions.
49 */
50 #pragma GCC system_header
51
52 #if defined(CVC5_COMPETITION_MODE) && !defined(CVC5_SMTCOMP_APPLICATION_TRACK)
53 /* This improves performance by ~10 percent on big inputs.
54 * This option is only valid if we know the input is ASCII (or some 8-bit encoding).
55 * If we know the input is UTF-16, we can use ANTLR3_INLINE_INPUT_UTF16.
56 * Otherwise, we have to let the lexer detect the encoding at runtime.
57 */
58 # define ANTLR3_INLINE_INPUT_ASCII
59 # define ANTLR3_INLINE_INPUT_8BIT
60 #endif /* CVC5_COMPETITION_MODE && !CVC5_SMTCOMP_APPLICATION_TRACK */
61
62 }/* @lexer::includes */
63
64 @lexer::postinclude {
65 #include "parser/smt2/smt2.h"
66 #include "parser/antlr_input.h"
67
68 using namespace cvc5;
69 using namespace cvc5::parser;
70
71 #undef PARSER_STATE
72 #define PARSER_STATE ((Smt2*)LEXER->super)
73 }/* @lexer::postinclude */
74
75 @parser::includes {
76
77 #include <memory>
78
79 #include "base/check.h"
80 #include "parser/parse_op.h"
81 #include "parser/parser.h"
82 #include "smt/command.h"
83
84 namespace cvc5 {
85
86 namespace api {
87 class Term;
88 class Sort;
89 }
90
91 }/* cvc5 namespace */
92
93 }/* @parser::includes */
94
95 @parser::postinclude {
96
97 #include <set>
98 #include <sstream>
99 #include <string>
100 #include <unordered_set>
101 #include <vector>
102
103 #include "api/cpp/cvc5.h"
104 #include "base/output.h"
105 #include "parser/antlr_input.h"
106 #include "parser/parser.h"
107 #include "parser/smt2/smt2.h"
108 #include "util/floatingpoint_size.h"
109 #include "util/hash.h"
110
111 using namespace cvc5;
112 using namespace cvc5::parser;
113
114 /* These need to be macros so they can refer to the PARSER macro, which
115 * will be defined by ANTLR *after* this section. (If they were functions,
116 * PARSER would be undefined.) */
117 #undef PARSER_STATE
118 #define PARSER_STATE ((Smt2*)PARSER->super)
119 #undef SOLVER
120 #define SOLVER PARSER_STATE->getSolver()
121 #undef SYM_MAN
122 #define SYM_MAN PARSER_STATE->getSymbolManager()
123 #undef MK_TERM
124 #define MK_TERM SOLVER->mkTerm
125 #define UNSUPPORTED PARSER_STATE->unimplementedFeature
126
127 }/* parser::postinclude */
128
129 /**
130 * Parses an expression.
131 * @return the parsed expression, or the Null Expr if we've reached the
132 * end of the input
133 */
134 parseExpr returns [cvc5::api::Term expr = cvc5::api::Term()]
135 @declarations {
136 cvc5::api::Term expr2;
137 }
138 : term[expr, expr2]
139 | EOF
140 ;
141
142 /**
143 * Parses a command
144 * @return the parsed command, or NULL if we've reached the end of the input
145 */
146 parseCommand returns [cvc5::Command* cmd_return = NULL]
147 @declarations {
148 std::unique_ptr<cvc5::Command> cmd;
149 std::string name;
150 }
151 @after {
152 cmd_return = cmd.release();
153 }
154 : LPAREN_TOK command[&cmd] RPAREN_TOK
155
156 /* This extended command has to be in the outermost production so that
157 * the RPAREN_TOK is properly eaten and we are in a good state to read
158 * the included file's tokens. */
159 | LPAREN_TOK INCLUDE_TOK str[name,true] RPAREN_TOK
160 { if(!PARSER_STATE->canIncludeFile()) {
161 PARSER_STATE->parseError("include-file feature was disabled for this "
162 "run.");
163 }
164 if(PARSER_STATE->strictModeEnabled()) {
165 PARSER_STATE->parseError("Extended commands are not permitted while "
166 "operating in strict compliance mode.");
167 }
168 PARSER_STATE->includeFile(name);
169 // The command of the included file will be produced at the next
170 // parseCommand() call
171 cmd.reset(new EmptyCommand("include::" + name));
172 }
173
174 | EOF
175 ;
176
177 /**
178 * Parses a SyGuS command.
179 * @return the parsed SyGuS command, or NULL if we've reached the end of the
180 * input
181 */
182 parseSygus returns [cvc5::Command* cmd_return = NULL]
183 @declarations {
184 std::string name;
185 }
186 @after {
187 cmd_return = cmd.release();
188 }
189 : LPAREN_TOK cmd=sygusCommand RPAREN_TOK
190 | EOF
191 ;
192
193 /**
194 * Parse the internal portion of the command, ignoring the surrounding
195 * parentheses.
196 */
197 command [std::unique_ptr<cvc5::Command>* cmd]
198 @declarations {
199 std::string name;
200 std::vector<std::string> names;
201 cvc5::api::Term expr, expr2;
202 cvc5::api::Sort t;
203 std::vector<cvc5::api::Term> terms;
204 std::vector<api::Sort> sorts;
205 std::vector<std::pair<std::string, cvc5::api::Sort> > sortedVarNames;
206 std::vector<cvc5::api::Term> flattenVars;
207 }
208 : /* set the logic */
209 SET_LOGIC_TOK symbol[name,CHECK_NONE,SYM_SORT]
210 {
211 cmd->reset(PARSER_STATE->setLogic(name));
212 }
213 | /* set-info */
214 SET_INFO_TOK setInfoInternal[cmd]
215 | /* get-info */
216 GET_INFO_TOK KEYWORD
217 { cmd->reset(new GetInfoCommand(
218 AntlrInput::tokenText($KEYWORD).c_str() + 1));
219 }
220 | /* set-option */
221 SET_OPTION_TOK setOptionInternal[cmd]
222 | /* get-option */
223 GET_OPTION_TOK KEYWORD
224 { cmd->reset(new GetOptionCommand(
225 AntlrInput::tokenText($KEYWORD).c_str() + 1));
226 }
227 | /* sort declaration */
228 DECLARE_SORT_TOK
229 {
230 PARSER_STATE->checkThatLogicIsSet();
231 PARSER_STATE->checkLogicAllowsFreeSorts();
232 }
233 symbol[name,CHECK_UNDECLARED,SYM_SORT]
234 { PARSER_STATE->checkUserSymbol(name); }
235 n=INTEGER_LITERAL
236 { Trace("parser") << "declare sort: '" << name
237 << "' arity=" << n << std::endl;
238 unsigned arity = AntlrInput::tokenToUnsigned(n);
239 if(arity == 0) {
240 api::Sort type = PARSER_STATE->mkSort(name);
241 cmd->reset(new DeclareSortCommand(name, 0, type));
242 } else {
243 api::Sort type = PARSER_STATE->mkSortConstructor(name, arity);
244 cmd->reset(new DeclareSortCommand(name, arity, type));
245 }
246 }
247 | /* sort definition */
248 DEFINE_SORT_TOK { PARSER_STATE->checkThatLogicIsSet(); }
249 symbol[name,CHECK_UNDECLARED,SYM_SORT]
250 { PARSER_STATE->checkUserSymbol(name); }
251 LPAREN_TOK symbolList[names,CHECK_UNDECLARED,SYM_SORT] RPAREN_TOK
252 { PARSER_STATE->pushScope();
253 for(std::vector<std::string>::const_iterator i = names.begin(),
254 iend = names.end();
255 i != iend;
256 ++i) {
257 sorts.push_back(PARSER_STATE->mkSort(*i));
258 }
259 }
260 sortSymbol[t,CHECK_DECLARED]
261 { PARSER_STATE->popScope();
262 // Do NOT call mkSort, since that creates a new sort!
263 // This name is not its own distinct sort, it's an alias.
264 PARSER_STATE->defineParameterizedType(name, sorts, t);
265 cmd->reset(new DefineSortCommand(name, sorts, t));
266 }
267 | /* function declaration */
268 DECLARE_FUN_TOK { PARSER_STATE->checkThatLogicIsSet(); }
269 symbol[name,CHECK_NONE,SYM_VARIABLE]
270 { PARSER_STATE->checkUserSymbol(name); }
271 LPAREN_TOK sortList[sorts] RPAREN_TOK
272 sortSymbol[t,CHECK_DECLARED]
273 { Trace("parser") << "declare fun: '" << name << "'" << std::endl;
274 if( !sorts.empty() ) {
275 t = PARSER_STATE->mkFlatFunctionType(sorts, t);
276 }
277 if(t.isFunction())
278 {
279 PARSER_STATE->checkLogicAllowsFunctions();
280 }
281 // we allow overloading for function declarations
282 if( PARSER_STATE->sygus() )
283 {
284 PARSER_STATE->parseErrorLogic("declare-fun are not allowed in sygus "
285 "version 2.0");
286 }
287 else
288 {
289 api::Term func =
290 PARSER_STATE->bindVar(name, t, true);
291 cmd->reset(new DeclareFunctionCommand(name, func, t));
292 }
293 }
294 | /* function definition */
295 DEFINE_FUN_TOK { PARSER_STATE->checkThatLogicIsSet(); }
296 symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
297 { PARSER_STATE->checkUserSymbol(name); }
298 LPAREN_TOK sortedVarList[sortedVarNames] RPAREN_TOK
299 sortSymbol[t,CHECK_DECLARED]
300 { /* add variables to parser state before parsing term */
301 Trace("parser") << "define fun: '" << name << "'" << std::endl;
302 if( sortedVarNames.size() > 0 ) {
303 sorts.reserve(sortedVarNames.size());
304 for(std::vector<std::pair<std::string, api::Sort> >::const_iterator i =
305 sortedVarNames.begin(), iend = sortedVarNames.end();
306 i != iend;
307 ++i) {
308 sorts.push_back((*i).second);
309 }
310 }
311
312 t = PARSER_STATE->mkFlatFunctionType(sorts, t, flattenVars);
313 if (t.isFunction())
314 {
315 t = t.getFunctionCodomainSort();
316 }
317 if (sortedVarNames.size() > 0)
318 {
319 PARSER_STATE->pushScope();
320 }
321 terms = PARSER_STATE->bindBoundVars(sortedVarNames);
322 }
323 term[expr, expr2]
324 {
325 if( !flattenVars.empty() ){
326 // if this function has any implicit variables flattenVars,
327 // we apply the body of the definition to the flatten vars
328 expr = PARSER_STATE->mkHoApply(expr, flattenVars);
329 terms.insert(terms.end(), flattenVars.begin(), flattenVars.end());
330 }
331 if (sortedVarNames.size() > 0)
332 {
333 PARSER_STATE->popScope();
334 }
335 cmd->reset(new DefineFunctionCommand(name, terms, t, expr));
336 }
337 | DECLARE_DATATYPE_TOK datatypeDefCommand[false, cmd]
338 | DECLARE_DATATYPES_TOK datatypesDefCommand[false, cmd]
339 | /* value query */
340 GET_VALUE_TOK
341 {
342 PARSER_STATE->checkThatLogicIsSet();
343 // bind all symbols specific to the model, e.g. uninterpreted constant
344 // values
345 PARSER_STATE->pushGetValueScope();
346 }
347 ( LPAREN_TOK termList[terms,expr] RPAREN_TOK
348 { cmd->reset(new GetValueCommand(terms)); }
349 | ~LPAREN_TOK
350 { PARSER_STATE->parseError("The get-value command expects a list of "
351 "terms. Perhaps you forgot a pair of "
352 "parentheses?");
353 }
354 )
355 { PARSER_STATE->popScope(); }
356 | /* get-assignment */
357 GET_ASSIGNMENT_TOK { PARSER_STATE->checkThatLogicIsSet(); }
358 { cmd->reset(new GetAssignmentCommand()); }
359 | /* assertion */
360 ASSERT_TOK { PARSER_STATE->checkThatLogicIsSet(); }
361 { PARSER_STATE->clearLastNamedTerm(); }
362 term[expr, expr2]
363 { cmd->reset(new AssertCommand(expr));
364 if (PARSER_STATE->lastNamedTerm().first == expr)
365 {
366 // set the expression name, if there was a named term
367 std::pair<api::Term, std::string> namedTerm =
368 PARSER_STATE->lastNamedTerm();
369 SYM_MAN->setExpressionName(namedTerm.first, namedTerm.second, true);
370 }
371 }
372 | /* check-sat */
373 CHECK_SAT_TOK { PARSER_STATE->checkThatLogicIsSet(); }
374 {
375 if (PARSER_STATE->sygus()) {
376 PARSER_STATE->parseError("Sygus does not support check-sat command.");
377 }
378 cmd->reset(new CheckSatCommand());
379 }
380 | /* check-sat-assuming */
381 CHECK_SAT_ASSUMING_TOK { PARSER_STATE->checkThatLogicIsSet(); }
382 ( LPAREN_TOK termList[terms,expr] RPAREN_TOK
383 {
384 cmd->reset(new CheckSatAssumingCommand(terms));
385 }
386 | ~LPAREN_TOK
387 { PARSER_STATE->parseError("The check-sat-assuming command expects a "
388 "list of terms. Perhaps you forgot a pair of "
389 "parentheses?");
390 }
391 )
392 | /* get-assertions */
393 GET_ASSERTIONS_TOK { PARSER_STATE->checkThatLogicIsSet(); }
394 { cmd->reset(new GetAssertionsCommand()); }
395 | /* get-proof */
396 GET_PROOF_TOK { PARSER_STATE->checkThatLogicIsSet(); }
397 { cmd->reset(new GetProofCommand()); }
398 | /* get-unsat-assumptions */
399 GET_UNSAT_ASSUMPTIONS_TOK { PARSER_STATE->checkThatLogicIsSet(); }
400 { cmd->reset(new GetUnsatAssumptionsCommand); }
401 | /* get-unsat-core */
402 GET_UNSAT_CORE_TOK { PARSER_STATE->checkThatLogicIsSet(); }
403 { cmd->reset(new GetUnsatCoreCommand); }
404 | /* get-difficulty */
405 GET_DIFFICULTY_TOK { PARSER_STATE->checkThatLogicIsSet(); }
406 { cmd->reset(new GetDifficultyCommand); }
407 | /* get-learned-literals */
408 GET_LEARNED_LITERALS_TOK { PARSER_STATE->checkThatLogicIsSet(); }
409 { cmd->reset(new GetLearnedLiteralsCommand); }
410 | /* push */
411 PUSH_TOK { PARSER_STATE->checkThatLogicIsSet(); }
412 ( k=INTEGER_LITERAL
413 { unsigned num = AntlrInput::tokenToUnsigned(k);
414 if(num == 0) {
415 cmd->reset(new EmptyCommand());
416 } else if(num == 1) {
417 PARSER_STATE->pushScope(true);
418 cmd->reset(new PushCommand());
419 } else {
420 std::unique_ptr<CommandSequence> seq(new CommandSequence());
421 do {
422 PARSER_STATE->pushScope(true);
423 Command* push_cmd = new PushCommand();
424 push_cmd->setMuted(num > 1);
425 seq->addCommand(push_cmd);
426 --num;
427 } while(num > 0);
428 cmd->reset(seq.release());
429 }
430 }
431 | { if(PARSER_STATE->strictModeEnabled()) {
432 PARSER_STATE->parseError(
433 "Strict compliance mode demands an integer to be provided to "
434 "PUSH. Maybe you want (push 1)?");
435 } else {
436 PARSER_STATE->pushScope(true);
437 cmd->reset(new PushCommand());
438 }
439 } )
440 | POP_TOK { PARSER_STATE->checkThatLogicIsSet(); }
441 ( k=INTEGER_LITERAL
442 { unsigned num = AntlrInput::tokenToUnsigned(k);
443 // we don't compare num to PARSER_STATE->scopeLevel() here, since
444 // when global declarations is true, the scope level of the parser
445 // is not indicative of the context level.
446 if(num == 0) {
447 cmd->reset(new EmptyCommand());
448 } else if(num == 1) {
449 PARSER_STATE->popScope();
450 cmd->reset(new PopCommand());
451 } else {
452 std::unique_ptr<CommandSequence> seq(new CommandSequence());
453 do {
454 PARSER_STATE->popScope();
455 Command* pop_command = new PopCommand();
456 pop_command->setMuted(num > 1);
457 seq->addCommand(pop_command);
458 --num;
459 } while(num > 0);
460 cmd->reset(seq.release());
461 }
462 }
463 | { if(PARSER_STATE->strictModeEnabled()) {
464 PARSER_STATE->parseError(
465 "Strict compliance mode demands an integer to be provided to POP."
466 "Maybe you want (pop 1)?");
467 } else {
468 PARSER_STATE->popScope();
469 cmd->reset(new PopCommand());
470 }
471 }
472 )
473 /* exit */
474 | EXIT_TOK
475 { cmd->reset(new QuitCommand()); }
476
477 /* New SMT-LIB 2.5 command set */
478 | smt25Command[cmd]
479
480 /* cvc5-extended SMT-LIB commands */
481 | extendedCommand[cmd]
482 { if(PARSER_STATE->strictModeEnabled()) {
483 PARSER_STATE->parseError(
484 "Extended commands are not permitted while operating in strict "
485 "compliance mode.");
486 }
487 }
488
489 /* error handling */
490 | SIMPLE_SYMBOL
491 { std::string id = AntlrInput::tokenText($SIMPLE_SYMBOL);
492 if(id == "benchmark") {
493 PARSER_STATE->parseError(
494 "In SMT-LIBv2 mode, but got something that looks like SMT-LIBv1, "
495 "which is not supported anymore.");
496 } else {
497 PARSER_STATE->parseError("expected SMT-LIBv2 command, got `" + id +
498 "'.");
499 }
500 }
501 ;
502
503 sygusCommand returns [std::unique_ptr<cvc5::Command> cmd]
504 @declarations {
505 cvc5::api::Term expr, expr2, fun;
506 cvc5::api::Sort t, range;
507 std::vector<std::string> names;
508 std::vector<std::pair<std::string, cvc5::api::Sort> > sortedVarNames;
509 std::vector<cvc5::api::Term> sygusVars;
510 std::string name;
511 bool isAssume;
512 bool isInv;
513 cvc5::api::Grammar* grammar = nullptr;
514 }
515 : /* declare-var */
516 DECLARE_VAR_TOK { PARSER_STATE->checkThatLogicIsSet(); }
517 symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
518 { PARSER_STATE->checkUserSymbol(name); }
519 sortSymbol[t,CHECK_DECLARED]
520 {
521 api::Term var = SOLVER->mkSygusVar(t, name);
522 PARSER_STATE->defineVar(name, var);
523 cmd.reset(new DeclareSygusVarCommand(name, var, t));
524 }
525 | /* synth-fun */
526 ( SYNTH_FUN_TOK { isInv = false; }
527 | SYNTH_INV_TOK { isInv = true; range = SOLVER->getBooleanSort(); }
528 )
529 { PARSER_STATE->checkThatLogicIsSet(); }
530 symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
531 LPAREN_TOK sortedVarList[sortedVarNames] RPAREN_TOK
532 ( sortSymbol[range,CHECK_DECLARED] )?
533 {
534 PARSER_STATE->pushScope();
535 sygusVars = PARSER_STATE->bindBoundVars(sortedVarNames);
536 }
537 (
538 // optionally, read the sygus grammar
539 //
540 // `grammar` specifies the required grammar for the function to
541 // synthesize, expressed as a type
542 sygusGrammar[grammar, sygusVars, name]
543 )?
544 {
545 Trace("parser-sygus") << "Define synth fun : " << name << std::endl;
546
547 fun = isInv ? (grammar == nullptr
548 ? SOLVER->synthInv(name, sygusVars)
549 : SOLVER->synthInv(name, sygusVars, *grammar))
550 : (grammar == nullptr
551 ? SOLVER->synthFun(name, sygusVars, range)
552 : SOLVER->synthFun(name, sygusVars, range, *grammar));
553
554 Trace("parser-sygus") << "...read synth fun " << name << std::endl;
555 PARSER_STATE->popScope();
556 // we do not allow overloading for synth fun
557 PARSER_STATE->defineVar(name, fun);
558 cmd = std::unique_ptr<Command>(
559 new SynthFunCommand(name, fun, sygusVars, range, isInv, grammar));
560 }
561 | /* constraint */
562 ( CONSTRAINT_TOK { isAssume = false; } | ASSUME_TOK { isAssume = true; } )
563 {
564 PARSER_STATE->checkThatLogicIsSet();
565 }
566 term[expr, expr2]
567 { Trace("parser-sygus") << "...read constraint " << expr << std::endl;
568 cmd.reset(new SygusConstraintCommand(expr, isAssume));
569 }
570 | /* inv-constraint */
571 INV_CONSTRAINT_TOK
572 ( symbol[name,CHECK_NONE,SYM_VARIABLE] { names.push_back(name); } )+
573 {
574 cmd = PARSER_STATE->invConstraint(names);
575 }
576 | /* check-synth */
577 CHECK_SYNTH_TOK
578 {
579 PARSER_STATE->checkThatLogicIsSet();
580 cmd.reset(new CheckSynthCommand());
581 }
582 | /* check-synth-next */
583 CHECK_SYNTH_NEXT_TOK
584 {
585 PARSER_STATE->checkThatLogicIsSet();
586 cmd.reset(new CheckSynthCommand(true));
587 }
588 | /* set-feature */
589 SET_FEATURE_TOK keyword[name] symbolicExpr[expr]
590 {
591 PARSER_STATE->checkThatLogicIsSet();
592 // ":grammars" is defined in the SyGuS version 2.1 standard and is by
593 // default supported, all other features are not.
594 if (name != ":grammars")
595 {
596 std::stringstream ss;
597 ss << "SyGuS feature " << name << " not currently supported";
598 PARSER_STATE->warning(ss.str());
599 }
600 cmd.reset(new EmptyCommand());
601 }
602 | command[&cmd]
603 ;
604
605
606 /** Reads a sygus grammar in the sygus version 2 format
607 *
608 * The resulting sygus datatype encoding the grammar is stored in ret.
609 * The argument sygusVars indicates the sygus bound variable list, which is
610 * the argument list of the function-to-synthesize (or null if the grammar
611 * has bound variables).
612 * The argument fun is a unique identifier to avoid naming clashes for the
613 * datatypes constructed by this call.
614 */
615 sygusGrammar[cvc5::api::Grammar*& ret,
616 const std::vector<cvc5::api::Term>& sygusVars,
617 const std::string& fun]
618 @declarations
619 {
620 // the pre-declaration
621 std::vector<std::pair<std::string, cvc5::api::Sort>> sortedVarNames;
622 // non-terminal symbols of the grammar
623 std::vector<cvc5::api::Term> ntSyms;
624 cvc5::api::Sort t;
625 std::string name;
626 cvc5::api::Term e, e2;
627 unsigned dtProcessed = 0;
628 }
629 :
630 // predeclaration
631 LPAREN_TOK
632 // We read a sorted variable list here in a custom way that throws an
633 // error to recognize if the user is using the (deprecated) version 1.0
634 // sygus syntax.
635 ( LPAREN_TOK symbol[name,CHECK_NONE,SYM_VARIABLE]
636 sortSymbol[t,CHECK_DECLARED] (
637 // SyGuS version 1.0 expects a grammar ((Start Int ( ...
638 // SyGuS version 2.0 expects a predeclaration ((Start Int) ...
639 LPAREN_TOK
640 {
641 std::stringstream sse;
642 if (sortedVarNames.empty())
643 {
644 sse << "The expected SyGuS language is version 2.0, whereas the "
645 << "input appears to be SyGuS version 1.0 format. The version "
646 << "2.0 format requires a predeclaration of the non-terminal "
647 << "symbols of the grammar to be given prior to the definition "
648 << "of the grammar. See https://sygus.org/language/ for details "
649 << "and examples. cvc5 versions past 1.8 do not support SyGuS "
650 << "version 1.0.";
651 }
652 else
653 {
654 // an unknown syntax error
655 sse << "Unexpected syntax for SyGuS predeclaration.";
656 }
657 PARSER_STATE->parseError(sse.str().c_str());
658 }
659 | RPAREN_TOK )
660 { sortedVarNames.push_back(make_pair(name, t)); }
661 )*
662 RPAREN_TOK
663 {
664 // non-terminal symbols in the pre-declaration are locally scoped
665 PARSER_STATE->pushScope();
666 for (std::pair<std::string, api::Sort>& i : sortedVarNames)
667 {
668 PARSER_STATE->checkDeclaration(name, CHECK_UNDECLARED, SYM_SORT);
669 // make the non-terminal symbol, which will be parsed as an ordinary
670 // free variable.
671 api::Term nts = PARSER_STATE->bindBoundVar(i.first, i.second);
672 ntSyms.push_back(nts);
673 }
674 ret = PARSER_STATE->mkGrammar(sygusVars, ntSyms);
675 }
676 // the grouped rule listing
677 LPAREN_TOK
678 (
679 LPAREN_TOK
680 symbol[name, CHECK_DECLARED, SYM_VARIABLE] sortSymbol[t, CHECK_DECLARED]
681 {
682 // check that it matches sortedVarNames
683 if (sortedVarNames[dtProcessed].first != name)
684 {
685 std::stringstream sse;
686 sse << "Grouped rule listing " << name
687 << " does not match the name (in order) from the predeclaration ("
688 << sortedVarNames[dtProcessed].first << ")." << std::endl;
689 PARSER_STATE->parseError(sse.str().c_str());
690 }
691 if (sortedVarNames[dtProcessed].second != t)
692 {
693 std::stringstream sse;
694 sse << "Type for grouped rule listing " << name
695 << " does not match the type (in order) from the predeclaration ("
696 << sortedVarNames[dtProcessed].second << ")." << std::endl;
697 PARSER_STATE->parseError(sse.str().c_str());
698 }
699 }
700 LPAREN_TOK
701 (
702 term[e,e2] {
703 // add term as constructor to datatype
704 ret->addRule(ntSyms[dtProcessed], e);
705 }
706 | LPAREN_TOK SYGUS_CONSTANT_TOK sortSymbol[t, CHECK_DECLARED] RPAREN_TOK {
707 // allow constants in datatype for ntSyms[dtProcessed]
708 ret->addAnyConstant(ntSyms[dtProcessed]);
709 }
710 | LPAREN_TOK SYGUS_VARIABLE_TOK sortSymbol[t, CHECK_DECLARED] RPAREN_TOK {
711 // add variable constructors to datatype
712 ret->addAnyVariable(ntSyms[dtProcessed]);
713 }
714 )+
715 RPAREN_TOK
716 RPAREN_TOK
717 {
718 dtProcessed++;
719 }
720 )+
721 RPAREN_TOK
722 {
723 // pop scope from the pre-declaration
724 PARSER_STATE->popScope();
725 }
726 ;
727
728 setInfoInternal[std::unique_ptr<cvc5::Command>* cmd]
729 @declarations {
730 std::string name;
731 api::Term sexpr;
732 }
733 : keyword[name] symbolicExpr[sexpr]
734 { cmd->reset(new SetInfoCommand(name.c_str() + 1, sexprToString(sexpr))); }
735 ;
736
737 setOptionInternal[std::unique_ptr<cvc5::Command>* cmd]
738 @init {
739 std::string name;
740 api::Term sexpr;
741 }
742 : keyword[name] symbolicExpr[sexpr]
743 { cmd->reset(new SetOptionCommand(name.c_str() + 1, sexprToString(sexpr)));
744 // Ugly that this changes the state of the parser; but
745 // global-declarations affects parsing, so we can't hold off
746 // on this until some SolverEngine eventually (if ever) executes it.
747 if(name == ":global-declarations")
748 {
749 SYM_MAN->setGlobalDeclarations(sexprToString(sexpr) == "true");
750 }
751 }
752 ;
753
754 smt25Command[std::unique_ptr<cvc5::Command>* cmd]
755 @declarations {
756 std::string name;
757 std::string fname;
758 cvc5::api::Term expr, expr2;
759 std::vector<std::pair<std::string, cvc5::api::Sort> > sortedVarNames;
760 std::string s;
761 cvc5::api::Sort t;
762 cvc5::api::Term func;
763 std::vector<cvc5::api::Term> bvs;
764 std::vector<std::vector<std::pair<std::string, cvc5::api::Sort>>>
765 sortedVarNamesList;
766 std::vector<std::vector<cvc5::api::Term>> flattenVarsList;
767 std::vector<std::vector<cvc5::api::Term>> formals;
768 std::vector<cvc5::api::Term> funcs;
769 std::vector<cvc5::api::Term> func_defs;
770 cvc5::api::Term aexpr;
771 std::unique_ptr<cvc5::CommandSequence> seq;
772 std::vector<api::Sort> sorts;
773 std::vector<cvc5::api::Term> flattenVars;
774 }
775 /* declare-const */
776 : DECLARE_CONST_TOK { PARSER_STATE->checkThatLogicIsSet(); }
777 symbol[name,CHECK_NONE,SYM_VARIABLE]
778 { PARSER_STATE->checkUserSymbol(name); }
779 sortSymbol[t,CHECK_DECLARED]
780 { // allow overloading here
781 if( PARSER_STATE->sygus() )
782 {
783 PARSER_STATE->parseErrorLogic("declare-const is not allowed in sygus "
784 "version 2.0");
785 }
786 api::Term c =
787 PARSER_STATE->bindVar(name, t, true);
788 cmd->reset(new DeclareFunctionCommand(name, c, t)); }
789
790 /* get model */
791 | GET_MODEL_TOK { PARSER_STATE->checkThatLogicIsSet(); }
792 { cmd->reset(new GetModelCommand()); }
793
794 /* echo */
795 | ECHO_TOK
796 ( str[s, true]
797 { cmd->reset(new EchoCommand(s)); }
798 | { cmd->reset(new EchoCommand()); }
799 )
800
801 /* reset: reset everything, returning solver to initial state.
802 * Logic and options must be set again. */
803 | RESET_TOK
804 {
805 cmd->reset(new ResetCommand());
806 // reset the state of the parser, which is independent of the symbol
807 // manager
808 PARSER_STATE->reset();
809 }
810 /* reset-assertions: reset assertions, assertion stack, declarations,
811 * etc., but the logic and options remain as they were. */
812 | RESET_ASSERTIONS_TOK
813 { cmd->reset(new ResetAssertionsCommand());
814 }
815 | DEFINE_FUN_REC_TOK
816 { PARSER_STATE->checkThatLogicIsSet(); }
817 symbol[fname,CHECK_NONE,SYM_VARIABLE]
818 { PARSER_STATE->checkUserSymbol(fname); }
819 LPAREN_TOK sortedVarList[sortedVarNames] RPAREN_TOK
820 sortSymbol[t,CHECK_DECLARED]
821 {
822 func =
823 PARSER_STATE->bindDefineFunRec(fname, sortedVarNames, t, flattenVars);
824 PARSER_STATE->pushDefineFunRecScope(
825 sortedVarNames, func, flattenVars, bvs);
826 }
827 term[expr, expr2]
828 { PARSER_STATE->popScope();
829 if( !flattenVars.empty() ){
830 expr = PARSER_STATE->mkHoApply( expr, flattenVars );
831 }
832 cmd->reset(new DefineFunctionRecCommand(func, bvs, expr));
833 }
834 | DEFINE_FUNS_REC_TOK
835 { PARSER_STATE->checkThatLogicIsSet();}
836 LPAREN_TOK
837 ( LPAREN_TOK
838 symbol[fname,CHECK_UNDECLARED,SYM_VARIABLE]
839 { PARSER_STATE->checkUserSymbol(fname); }
840 LPAREN_TOK sortedVarList[sortedVarNames] RPAREN_TOK
841 sortSymbol[t,CHECK_DECLARED]
842 {
843 flattenVars.clear();
844 func = PARSER_STATE->bindDefineFunRec(
845 fname, sortedVarNames, t, flattenVars);
846 funcs.push_back( func );
847
848 // add to lists (need to remember for when parsing the bodies)
849 sortedVarNamesList.push_back( sortedVarNames );
850 flattenVarsList.push_back( flattenVars );
851
852 // set up parsing the next variable list block
853 sortedVarNames.clear();
854 flattenVars.clear();
855 }
856 RPAREN_TOK
857 )+
858 RPAREN_TOK
859 LPAREN_TOK
860 {
861 //set up the first scope
862 if( sortedVarNamesList.empty() ){
863 PARSER_STATE->parseError("Must define at least one function in "
864 "define-funs-rec");
865 }
866 bvs.clear();
867 PARSER_STATE->pushDefineFunRecScope( sortedVarNamesList[0], funcs[0],
868 flattenVarsList[0], bvs);
869 }
870 (
871 term[expr,expr2]
872 {
873 unsigned j = func_defs.size();
874 if( !flattenVarsList[j].empty() ){
875 expr = PARSER_STATE->mkHoApply( expr, flattenVarsList[j] );
876 }
877 func_defs.push_back( expr );
878 formals.push_back(bvs);
879 j++;
880 //set up the next scope
881 PARSER_STATE->popScope();
882 if( func_defs.size()<funcs.size() ){
883 bvs.clear();
884 PARSER_STATE->pushDefineFunRecScope( sortedVarNamesList[j], funcs[j],
885 flattenVarsList[j], bvs);
886 }
887 }
888 )+
889 RPAREN_TOK
890 { if( funcs.size()!=func_defs.size() ){
891 PARSER_STATE->parseError(std::string(
892 "Number of functions defined does not match number listed in "
893 "define-funs-rec"));
894 }
895 cmd->reset(new DefineFunctionRecCommand(funcs, formals, func_defs));
896 }
897 ;
898
899 extendedCommand[std::unique_ptr<cvc5::Command>* cmd]
900 @declarations {
901 std::vector<api::DatatypeDecl> dts;
902 cvc5::api::Term e, e2;
903 cvc5::api::Sort t, s;
904 std::string name;
905 std::vector<std::string> names;
906 std::vector<cvc5::api::Term> terms;
907 std::vector<api::Sort> sorts;
908 std::vector<std::pair<std::string, cvc5::api::Sort> > sortedVarNames;
909 std::unique_ptr<cvc5::CommandSequence> seq;
910 api::Grammar* g = nullptr;
911 }
912 /* Extended SMT-LIB set of commands syntax, not permitted in
913 * --smtlib2 compliance mode. */
914 : DECLARE_CODATATYPE_TOK datatypeDefCommand[true, cmd]
915 | DECLARE_CODATATYPES_TOK datatypesDefCommand[true, cmd]
916
917 /* Support some of Z3's extended SMT-LIB commands */
918
919 | DECLARE_SORTS_TOK
920 {
921 PARSER_STATE->checkThatLogicIsSet();
922 PARSER_STATE->checkLogicAllowsFreeSorts();
923 seq.reset(new cvc5::CommandSequence());
924 }
925 LPAREN_TOK
926 ( symbol[name,CHECK_UNDECLARED,SYM_SORT]
927 { PARSER_STATE->checkUserSymbol(name);
928 api::Sort type = PARSER_STATE->mkSort(name);
929 seq->addCommand(new DeclareSortCommand(name, 0, type));
930 }
931 )+
932 RPAREN_TOK
933 { cmd->reset(seq.release()); }
934
935 | DECLARE_FUNS_TOK { PARSER_STATE->checkThatLogicIsSet(); }
936 { seq.reset(new cvc5::CommandSequence()); }
937 LPAREN_TOK
938 ( LPAREN_TOK symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
939 { PARSER_STATE->checkUserSymbol(name); }
940 nonemptySortList[sorts] RPAREN_TOK
941 { api::Sort tt;
942 if(sorts.size() > 1) {
943 PARSER_STATE->checkLogicAllowsFunctions();
944 // must flatten
945 api::Sort range = sorts.back();
946 sorts.pop_back();
947 tt = PARSER_STATE->mkFlatFunctionType(sorts, range);
948 } else {
949 tt = sorts[0];
950 }
951 // allow overloading
952 api::Term func =
953 PARSER_STATE->bindVar(name, tt, true);
954 seq->addCommand(new DeclareFunctionCommand(name, func, tt));
955 sorts.clear();
956 }
957 )+
958 RPAREN_TOK
959 { cmd->reset(seq.release()); }
960 | DECLARE_PREDS_TOK { PARSER_STATE->checkThatLogicIsSet(); }
961 { seq.reset(new cvc5::CommandSequence()); }
962 LPAREN_TOK
963 ( LPAREN_TOK symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
964 { PARSER_STATE->checkUserSymbol(name); }
965 sortList[sorts] RPAREN_TOK
966 { t = SOLVER->getBooleanSort();
967 if(sorts.size() > 0) {
968 PARSER_STATE->checkLogicAllowsFunctions();
969 t = SOLVER->mkFunctionSort(sorts, t);
970 }
971 // allow overloading
972 api::Term func =
973 PARSER_STATE->bindVar(name, t, true);
974 seq->addCommand(new DeclareFunctionCommand(name, func, t));
975 sorts.clear();
976 }
977 )+
978 RPAREN_TOK
979 { cmd->reset(seq.release()); }
980 | // (define-const x U t)
981 DEFINE_CONST_TOK { PARSER_STATE->checkThatLogicIsSet(); }
982 symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
983 { PARSER_STATE->checkUserSymbol(name); }
984 sortSymbol[t,CHECK_DECLARED]
985 term[e, e2]
986 {
987 // declare the name down here (while parsing term, signature
988 // must not be extended with the name itself; no recursion
989 // permitted)
990 cmd->reset(new DefineFunctionCommand(name, t, e));
991 }
992
993 | SIMPLIFY_TOK { PARSER_STATE->checkThatLogicIsSet(); }
994 term[e,e2]
995 { cmd->reset(new SimplifyCommand(e)); }
996 | GET_QE_TOK { PARSER_STATE->checkThatLogicIsSet(); }
997 term[e,e2]
998 { cmd->reset(new GetQuantifierEliminationCommand(e, true)); }
999 | GET_QE_DISJUNCT_TOK { PARSER_STATE->checkThatLogicIsSet(); }
1000 term[e,e2]
1001 { cmd->reset(new GetQuantifierEliminationCommand(e, false)); }
1002 | GET_ABDUCT_TOK {
1003 PARSER_STATE->checkThatLogicIsSet();
1004 }
1005 symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
1006 term[e,e2]
1007 (
1008 sygusGrammar[g, terms, name]
1009 )?
1010 {
1011 cmd->reset(new GetAbductCommand(name, e, g));
1012 }
1013 | GET_ABDUCT_NEXT_TOK {
1014 PARSER_STATE->checkThatLogicIsSet();
1015 cmd->reset(new GetAbductNextCommand);
1016 }
1017 | GET_INTERPOL_TOK {
1018 PARSER_STATE->checkThatLogicIsSet();
1019 }
1020 symbol[name,CHECK_UNDECLARED,SYM_VARIABLE]
1021 term[e,e2]
1022 (
1023 sygusGrammar[g, terms, name]
1024 )?
1025 {
1026 cmd->reset(new GetInterpolCommand(name, e, g));
1027 }
1028 | GET_INTERPOL_NEXT_TOK {
1029 PARSER_STATE->checkThatLogicIsSet();
1030 cmd->reset(new GetInterpolNextCommand);
1031 }
1032 | DECLARE_HEAP LPAREN_TOK
1033 sortSymbol[t, CHECK_DECLARED]
1034 sortSymbol[s, CHECK_DECLARED]
1035 { cmd->reset(new DeclareHeapCommand(t, s)); }
1036 RPAREN_TOK
1037 | DECLARE_POOL { PARSER_STATE->checkThatLogicIsSet(); }
1038 symbol[name,CHECK_NONE,SYM_VARIABLE]
1039 { PARSER_STATE->checkUserSymbol(name); }
1040 sortSymbol[t,CHECK_DECLARED]
1041 LPAREN_TOK
1042 ( term[e, e2]
1043 { terms.push_back( e ); }
1044 )* RPAREN_TOK
1045 { Trace("parser") << "declare pool: '" << name << "'" << std::endl;
1046 api::Term pool = SOLVER->declarePool(name, t, terms);
1047 PARSER_STATE->defineVar(name, pool);
1048 cmd->reset(new DeclarePoolCommand(name, pool, t, terms));
1049 }
1050 | BLOCK_MODEL_TOK { PARSER_STATE->checkThatLogicIsSet(); }
1051 { cmd->reset(new BlockModelCommand()); }
1052
1053 | BLOCK_MODEL_VALUES_TOK { PARSER_STATE->checkThatLogicIsSet(); }
1054 ( LPAREN_TOK termList[terms,e] RPAREN_TOK
1055 { cmd->reset(new BlockModelValuesCommand(terms)); }
1056 | ~LPAREN_TOK
1057 { PARSER_STATE->parseError("The block-model-value command expects a list "
1058 "of terms. Perhaps you forgot a pair of "
1059 "parentheses?");
1060 }
1061 )
1062 ;
1063
1064 datatypeDefCommand[bool isCo, std::unique_ptr<cvc5::Command>* cmd]
1065 @declarations {
1066 std::vector<api::DatatypeDecl> dts;
1067 std::string name;
1068 std::vector<std::string> dnames;
1069 std::vector<int> arities;
1070 }
1071 : { PARSER_STATE->checkThatLogicIsSet(); }
1072 symbol[name,CHECK_UNDECLARED,SYM_SORT]
1073 {
1074 dnames.push_back(name);
1075 arities.push_back(-1);
1076 }
1077 datatypesDef[isCo, dnames, arities, cmd]
1078 ;
1079
1080 datatypesDefCommand[bool isCo, std::unique_ptr<cvc5::Command>* cmd]
1081 @declarations {
1082 std::vector<api::DatatypeDecl> dts;
1083 std::string name;
1084 std::vector<std::string> dnames;
1085 std::vector<int> arities;
1086 }
1087 : { PARSER_STATE->checkThatLogicIsSet(); }
1088 LPAREN_TOK /* datatype definition prelude */
1089 ( LPAREN_TOK symbol[name,CHECK_UNDECLARED,SYM_SORT] n=INTEGER_LITERAL RPAREN_TOK
1090 { unsigned arity = AntlrInput::tokenToUnsigned(n);
1091 Trace("parser-dt") << "Datatype : " << name << ", arity = " << arity << std::endl;
1092 dnames.push_back(name);
1093 arities.push_back( static_cast<int>(arity) );
1094 }
1095 )*
1096 RPAREN_TOK
1097 LPAREN_TOK
1098 datatypesDef[isCo, dnames, arities, cmd]
1099 RPAREN_TOK
1100 ;
1101
1102 /**
1103 * Read a list of datatype definitions for datatypes with names dnames and
1104 * parametric arities arities. A negative value in arities indicates that the
1105 * arity for the corresponding datatype has not been fixed: notice that we do
1106 * not know the arity of datatypes in the declare-datatype command prior to
1107 * parsing their body, whereas the arity of datatypes in declare-datatypes is
1108 * given in the preamble of that command and thus is known prior to this call.
1109 */
1110 datatypesDef[bool isCo,
1111 const std::vector<std::string>& dnames,
1112 const std::vector<int>& arities,
1113 std::unique_ptr<cvc5::Command>* cmd]
1114 @declarations {
1115 std::vector<api::DatatypeDecl> dts;
1116 std::string name;
1117 std::vector<api::Sort> params;
1118 }
1119 : { PARSER_STATE->pushScope();
1120 // Declare the datatypes that are currently being defined as unresolved
1121 // types. If we do not know the arity of the datatype yet, we wait to
1122 // define it until parsing the preamble of its body, which may optionally
1123 // involve `par`. This is limited to the case of single datatypes defined
1124 // via declare-datatype, and hence no datatype body is parsed without
1125 // having all types declared. This ensures we can parse datatypes with
1126 // nested recursion, e.g. datatypes D having a subfield type
1127 // (Array Int D).
1128 for (unsigned i=0, dsize=dnames.size(); i<dsize; i++)
1129 {
1130 if( arities[i]<0 )
1131 {
1132 // do not know the arity yet
1133 continue;
1134 }
1135 unsigned arity = static_cast<unsigned>(arities[i]);
1136 PARSER_STATE->mkUnresolvedType(dnames[i], arity);
1137 }
1138 }
1139 ( LPAREN_TOK {
1140 params.clear();
1141 Trace("parser-dt") << "Processing datatype #" << dts.size() << std::endl;
1142 if( dts.size()>=dnames.size() ){
1143 PARSER_STATE->parseError("Too many datatypes defined in this block.");
1144 }
1145 }
1146 ( PAR_TOK { PARSER_STATE->pushScope(); } LPAREN_TOK
1147 ( symbol[name,CHECK_UNDECLARED,SYM_SORT]
1148 {
1149 params.push_back( PARSER_STATE->mkSort(name)); }
1150 )*
1151 RPAREN_TOK {
1152 // if the arity was fixed by prelude and is not equal to the number of parameters
1153 if( arities[dts.size()]>=0 && static_cast<int>(params.size())!=arities[dts.size()] ){
1154 PARSER_STATE->parseError("Wrong number of parameters for datatype.");
1155 }
1156 if (arities[dts.size()]<0)
1157 {
1158 // now declare it as an unresolved type
1159 PARSER_STATE->mkUnresolvedType(dnames[dts.size()], params.size());
1160 }
1161 Trace("parser-dt") << params.size() << " parameters for " << dnames[dts.size()] << std::endl;
1162 dts.push_back(SOLVER->mkDatatypeDecl(dnames[dts.size()], params, isCo));
1163 }
1164 LPAREN_TOK
1165 ( LPAREN_TOK constructorDef[dts.back()] RPAREN_TOK )+
1166 RPAREN_TOK { PARSER_STATE->popScope(); }
1167 | { // if the arity was fixed by prelude and is not equal to 0
1168 if( arities[dts.size()]>0 ){
1169 PARSER_STATE->parseError("No parameters given for datatype.");
1170 }
1171 else if (arities[dts.size()]<0)
1172 {
1173 // now declare it as an unresolved type
1174 PARSER_STATE->mkUnresolvedType(dnames[dts.size()], 0);
1175 }
1176 Trace("parser-dt") << params.size() << " parameters for " << dnames[dts.size()] << std::endl;
1177 dts.push_back(SOLVER->mkDatatypeDecl(dnames[dts.size()],
1178 params,
1179 isCo));
1180 }
1181 ( LPAREN_TOK constructorDef[dts.back()] RPAREN_TOK )+
1182 )
1183 RPAREN_TOK
1184 )+
1185 {
1186 if (dts.size() != dnames.size())
1187 {
1188 PARSER_STATE->parseError("Wrong number of datatypes provided.");
1189 }
1190 PARSER_STATE->popScope();
1191 cmd->reset(new DatatypeDeclarationCommand(
1192 PARSER_STATE->bindMutualDatatypeTypes(dts, true)));
1193 }
1194 ;
1195
1196 simpleSymbolicExprNoKeyword[std::string& s]
1197 : INTEGER_LITERAL
1198 { s = AntlrInput::tokenText($INTEGER_LITERAL); }
1199 | DECIMAL_LITERAL
1200 { s = AntlrInput::tokenText($DECIMAL_LITERAL); }
1201 | HEX_LITERAL
1202 { s = AntlrInput::tokenText($HEX_LITERAL); }
1203 | BINARY_LITERAL
1204 { s = AntlrInput::tokenText($BINARY_LITERAL); }
1205 | SIMPLE_SYMBOL
1206 { s = AntlrInput::tokenText($SIMPLE_SYMBOL); }
1207 | QUOTED_SYMBOL
1208 { s = AntlrInput::tokenText($QUOTED_SYMBOL); }
1209 | STRING_LITERAL
1210 { s = AntlrInput::tokenText($STRING_LITERAL); }
1211 | tok=(ASSERT_TOK | CHECK_SAT_TOK | CHECK_SAT_ASSUMING_TOK | DECLARE_FUN_TOK
1212 | DECLARE_SORT_TOK
1213 | DEFINE_FUN_TOK | DEFINE_FUN_REC_TOK | DEFINE_FUNS_REC_TOK
1214 | DEFINE_SORT_TOK | GET_VALUE_TOK | GET_ASSIGNMENT_TOK
1215 | GET_ASSERTIONS_TOK | GET_PROOF_TOK | GET_UNSAT_ASSUMPTIONS_TOK
1216 | GET_UNSAT_CORE_TOK | GET_DIFFICULTY_TOK | EXIT_TOK
1217 | RESET_TOK | RESET_ASSERTIONS_TOK | SET_LOGIC_TOK | SET_INFO_TOK
1218 | GET_INFO_TOK | SET_OPTION_TOK | GET_OPTION_TOK | PUSH_TOK | POP_TOK
1219 | DECLARE_DATATYPES_TOK | GET_MODEL_TOK | ECHO_TOK | SIMPLIFY_TOK)
1220 { s = AntlrInput::tokenText($tok); }
1221 ;
1222
1223 keyword[std::string& s]
1224 : KEYWORD
1225 { s = AntlrInput::tokenText($KEYWORD); }
1226 ;
1227
1228 simpleSymbolicExpr[std::string& s]
1229 : simpleSymbolicExprNoKeyword[s]
1230 | KEYWORD { s = AntlrInput::tokenText($KEYWORD); }
1231 ;
1232
1233 symbolicExpr[cvc5::api::Term& sexpr]
1234 @declarations {
1235 std::string s;
1236 std::vector<api::Term> children;
1237 }
1238 : simpleSymbolicExpr[s]
1239 { sexpr = SOLVER->mkString(PARSER_STATE->processAdHocStringEsc(s)); }
1240 | LPAREN_TOK
1241 ( symbolicExpr[sexpr] { children.push_back(sexpr); } )* RPAREN_TOK
1242 { sexpr = SOLVER->mkTerm(cvc5::api::SEXPR, children); }
1243 ;
1244
1245 /**
1246 * Matches a term.
1247 * @return the expression representing the term.
1248 */
1249 term[cvc5::api::Term& expr, cvc5::api::Term& expr2]
1250 @init {
1251 api::Kind kind = api::NULL_EXPR;
1252 cvc5::api::Term f;
1253 std::string name;
1254 cvc5::api::Sort type;
1255 ParseOp p;
1256 }
1257 : termNonVariable[expr, expr2]
1258
1259 // a qualified identifier (section 3.6 of SMT-LIB version 2.6)
1260
1261 | qualIdentifier[p]
1262 {
1263 expr = PARSER_STATE->parseOpToExpr(p);
1264 }
1265 ;
1266
1267 /**
1268 * Matches a non-variable term.
1269 * @return the expression expr representing the term or formula, and expr2, an
1270 * optional annotation for expr (for instance, for attributed expressions).
1271 */
1272 termNonVariable[cvc5::api::Term& expr, cvc5::api::Term& expr2]
1273 @init {
1274 Trace("parser") << "term: " << AntlrInput::tokenText(LT(1)) << std::endl;
1275 api::Kind kind = api::NULL_EXPR;
1276 std::string name;
1277 std::vector<cvc5::api::Term> args;
1278 std::vector< std::pair<std::string, cvc5::api::Sort> > sortedVarNames;
1279 cvc5::api::Term bvl;
1280 cvc5::api::Term f, f2, f3;
1281 std::string attr;
1282 cvc5::api::Term attexpr;
1283 std::vector<cvc5::api::Term> patexprs;
1284 std::vector<cvc5::api::Term> matchcases;
1285 std::unordered_set<std::string> names;
1286 std::vector< std::pair<std::string, cvc5::api::Term> > binders;
1287 cvc5::api::Sort type;
1288 cvc5::api::Sort type2;
1289 api::Term atomTerm;
1290 ParseOp p;
1291 std::vector<api::Sort> argTypes;
1292 }
1293 : LPAREN_TOK quantOp[kind]
1294 {
1295 if (!PARSER_STATE->isTheoryEnabled(theory::THEORY_QUANTIFIERS))
1296 {
1297 PARSER_STATE->parseError("Quantifier used in non-quantified logic.");
1298 }
1299 PARSER_STATE->pushScope();
1300 }
1301 boundVarList[bvl]
1302 term[f, f2] RPAREN_TOK
1303 {
1304 args.push_back(bvl);
1305
1306 PARSER_STATE->popScope();
1307 args.push_back(f);
1308 if(! f2.isNull()){
1309 args.push_back(f2);
1310 }
1311 expr = MK_TERM(kind, args);
1312 }
1313 | LPAREN_TOK SET_COMPREHENSION_TOK
1314 { PARSER_STATE->pushScope(); }
1315 boundVarList[bvl]
1316 {
1317 args.push_back(bvl);
1318 }
1319 term[f, f2] { args.push_back(f); }
1320 term[f, f2] {
1321 args.push_back(f);
1322 expr = MK_TERM(api::SET_COMPREHENSION, args);
1323 }
1324 RPAREN_TOK
1325 | LPAREN_TOK qualIdentifier[p]
1326 termList[args,expr] RPAREN_TOK
1327 {
1328 expr = PARSER_STATE->applyParseOp(p, args);
1329 }
1330 | /* a let or sygus let binding */
1331 LPAREN_TOK
1332 LET_TOK LPAREN_TOK
1333 { PARSER_STATE->pushScope(); }
1334 ( LPAREN_TOK symbol[name,CHECK_NONE,SYM_VARIABLE]
1335 term[expr, f2]
1336 RPAREN_TOK
1337 // this is a parallel let, so we have to save up all the contributions
1338 // of the let and define them only later on
1339 { if(names.count(name) == 1) {
1340 std::stringstream ss;
1341 ss << "warning: symbol `" << name << "' bound multiple times by let;"
1342 << " the last binding will be used, shadowing earlier ones";
1343 PARSER_STATE->warning(ss.str());
1344 } else {
1345 names.insert(name);
1346 }
1347 binders.push_back(std::make_pair(name, expr)); } )+
1348 { // now implement these bindings
1349 for (const std::pair<std::string, api::Term>& binder : binders)
1350 {
1351 {
1352 PARSER_STATE->defineVar(binder.first, binder.second);
1353 }
1354 }
1355 }
1356 RPAREN_TOK
1357 term[expr, f2]
1358 RPAREN_TOK
1359 { PARSER_STATE->popScope(); }
1360 | /* match expression */
1361 LPAREN_TOK MATCH_TOK term[expr, f2] {
1362 if( !expr.getSort().isDatatype() ){
1363 PARSER_STATE->parseError("Cannot match on non-datatype term.");
1364 }
1365 }
1366 LPAREN_TOK
1367 (
1368 // case with non-nullary pattern
1369 LPAREN_TOK LPAREN_TOK term[f, f2] {
1370 args.clear();
1371 PARSER_STATE->pushScope();
1372 // f should be a constructor
1373 type = f.getSort();
1374 Trace("parser-dt") << "Pattern head : " << f << " " << type << std::endl;
1375 if (!type.isConstructor())
1376 {
1377 PARSER_STATE->parseError("Pattern must be application of a constructor or a variable.");
1378 }
1379 api::Datatype dt = type.getConstructorCodomainSort().getDatatype();
1380 if (dt.isParametric())
1381 {
1382 // lookup constructor by name
1383 api::DatatypeConstructor dc = dt.getConstructor(f.toString());
1384 api::Term scons = dc.getInstantiatedConstructorTerm(expr.getSort());
1385 // take the type of the specialized constructor instead
1386 type = scons.getSort();
1387 }
1388 argTypes = type.getConstructorDomainSorts();
1389 }
1390 // arguments of the pattern
1391 ( symbol[name,CHECK_NONE,SYM_VARIABLE] {
1392 if (args.size() >= argTypes.size())
1393 {
1394 PARSER_STATE->parseError("Too many arguments for pattern.");
1395 }
1396 //make of proper type
1397 api::Term arg = PARSER_STATE->bindBoundVar(name, argTypes[args.size()]);
1398 args.push_back( arg );
1399 }
1400 )*
1401 RPAREN_TOK term[f3, f2] {
1402 // make the match case
1403 std::vector<cvc5::api::Term> cargs;
1404 cargs.push_back(f);
1405 cargs.insert(cargs.end(),args.begin(),args.end());
1406 api::Term c = MK_TERM(api::APPLY_CONSTRUCTOR,cargs);
1407 api::Term bvla = MK_TERM(api::VARIABLE_LIST,args);
1408 api::Term mc = MK_TERM(api::MATCH_BIND_CASE, bvla, c, f3);
1409 matchcases.push_back(mc);
1410 // now, pop the scope
1411 PARSER_STATE->popScope();
1412 }
1413 RPAREN_TOK
1414 // case with nullary or variable pattern
1415 | LPAREN_TOK symbol[name,CHECK_NONE,SYM_VARIABLE] {
1416 if (PARSER_STATE->isDeclared(name,SYM_VARIABLE))
1417 {
1418 f = PARSER_STATE->getVariable(name);
1419 type = f.getSort();
1420 if (!type.isConstructor() ||
1421 !type.getConstructorDomainSorts().empty())
1422 {
1423 PARSER_STATE->parseError("Must apply constructors of arity greater than 0 to arguments in pattern.");
1424 }
1425 // make nullary constructor application
1426 f = MK_TERM(api::APPLY_CONSTRUCTOR, f);
1427 }
1428 else
1429 {
1430 // it has the type of the head expr
1431 f = PARSER_STATE->bindBoundVar(name, expr.getSort());
1432 }
1433 }
1434 term[f3, f2] {
1435 api::Term mc;
1436 if (f.getKind() == api::VARIABLE)
1437 {
1438 api::Term bvlf = MK_TERM(api::VARIABLE_LIST, f);
1439 mc = MK_TERM(api::MATCH_BIND_CASE, bvlf, f, f3);
1440 }
1441 else
1442 {
1443 mc = MK_TERM(api::MATCH_CASE, f, f3);
1444 }
1445 matchcases.push_back(mc);
1446 }
1447 RPAREN_TOK
1448 )+
1449 RPAREN_TOK RPAREN_TOK {
1450 //now, make the match
1451 if (matchcases.empty())
1452 {
1453 PARSER_STATE->parseError("Must have at least one case in match.");
1454 }
1455 std::vector<api::Term> mchildren;
1456 mchildren.push_back(expr);
1457 mchildren.insert(mchildren.end(), matchcases.begin(), matchcases.end());
1458 expr = MK_TERM(api::MATCH, mchildren);
1459 }
1460
1461 /* attributed expressions */
1462 | LPAREN_TOK ATTRIBUTE_TOK term[expr, f2]
1463 ( attribute[expr, attexpr]
1464 { if( ! attexpr.isNull()) {
1465 patexprs.push_back( attexpr );
1466 }
1467 }
1468 )+ RPAREN_TOK
1469 {
1470 if(! patexprs.empty()) {
1471 if( !f2.isNull() && f2.getKind()==api::INST_PATTERN_LIST ){
1472 for( size_t i=0; i<f2.getNumChildren(); i++ ){
1473 patexprs.push_back( f2[i] );
1474 }
1475 }
1476 expr2 = MK_TERM(api::INST_PATTERN_LIST, patexprs);
1477 } else {
1478 expr2 = f2;
1479 }
1480 }
1481 | /* lambda */
1482 LPAREN_TOK HO_LAMBDA_TOK
1483 { PARSER_STATE->pushScope(); }
1484 boundVarList[bvl]
1485 term[f, f2] RPAREN_TOK
1486 {
1487 args.push_back(bvl);
1488 args.push_back(f);
1489 PARSER_STATE->popScope();
1490 expr = MK_TERM(api::LAMBDA, args);
1491 }
1492 | LPAREN_TOK TUPLE_CONST_TOK termList[args,expr] RPAREN_TOK
1493 {
1494 std::vector<api::Sort> sorts;
1495 std::vector<api::Term> terms;
1496 for (const api::Term& arg : args)
1497 {
1498 sorts.emplace_back(arg.getSort());
1499 terms.emplace_back(arg);
1500 }
1501 expr = SOLVER->mkTuple(sorts, terms);
1502 }
1503 | LPAREN_TOK TUPLE_PROJECT_TOK term[expr,expr2] RPAREN_TOK
1504 {
1505 std::vector<uint32_t> indices;
1506 api::Op op = SOLVER->mkOp(api::TUPLE_PROJECT, indices);
1507 expr = SOLVER->mkTerm(op, expr);
1508 }
1509 | /* an atomic term (a term with no subterms) */
1510 termAtomic[atomTerm] { expr = atomTerm; }
1511 ;
1512
1513
1514 /**
1515 * Matches a qualified identifier, which can be a combination of one or more of
1516 * the following, stored in the ParseOp utility class:
1517 * (1) A kind.
1518 * (2) A string name.
1519 * (3) An expression expr.
1520 * (4) A type t.
1521 *
1522 * A qualified identifier is the generic case of function heads.
1523 * With respect to the standard definition (Section 3.6 of SMT-LIB version 2.6)
1524 * of qualified identifiers, we additionally parse:
1525 * - "Array constant specifications" of the form (as const (Array T1 T2)),
1526 * which notice are used as function heads e.g. ((as const (Array Int Int)) 0)
1527 * specifies the constant array over integers consisting of constant 0. This
1528 * is handled as if it were a special case of an operator here.
1529 *
1530 * Examples:
1531 *
1532 * (Identifiers)
1533 *
1534 * - For declared functions f, we return (2).
1535 * - For indexed functions like testers (_ is C) and bitvector extract
1536 * (_ extract n m), we return (3) for the appropriate operator.
1537 * - For tuple selectors (_ tuple_select n) and updaters (_ tuple_update n), we
1538 * return (1) and (3). api::Kind is set to APPLY_SELECTOR or APPLY_UPDATER
1539 * respectively, and expr is set to n, which is to be interpreted by the
1540 * caller as the n^th generic tuple selector or updater. We do this since there
1541 * is no AST expression representing generic tuple select, and we do not have
1542 * enough type information at this point to know the type of the tuple we will
1543 * be selecting from.
1544 *
1545 * (Ascripted Identifiers)
1546 *
1547 * - For ascripted nullary parametric datatype constructors like
1548 * (as nil (List Int)), we return (APPLY_CONSTRUCTOR C) for the appropriate
1549 * specialized constructor C as (3).
1550 * - For ascripted non-nullary parametric datatype constructors like
1551 * (as cons (List Int)), we return the appropriate specialized constructor C
1552 * as (3).
1553 * - Overloaded non-parametric constructors (as C T) return the appropriate
1554 * expression, analogous to the parametric cases above.
1555 * - For other ascripted nullary constants like (as set.empty (Set T)),
1556 * (as sep.nil T), we return the appropriate expression (3).
1557 * - For array constant specifications (as const (Array T1 T2)), we return (1)
1558 * and (4), where kind is set to STORE_ALL and type is set to (Array T1 T2),
1559 * where this is to be interpreted by the caller as converting the next parsed
1560 * constant of type T2 to an Array of type (Array T1 T2) over that constant.
1561 * - For ascriptions on normal symbols (as f T), we return the appropriate
1562 * expression (3), which may involve disambiguating f based on type T if it is
1563 * overloaded.
1564 */
1565 qualIdentifier[cvc5::ParseOp& p]
1566 @init {
1567 api::Kind k;
1568 std::string baseName;
1569 cvc5::api::Term f;
1570 cvc5::api::Sort type;
1571 }
1572 : identifier[p]
1573 | LPAREN_TOK AS_TOK
1574 ( CONST_TOK sortSymbol[type, CHECK_DECLARED]
1575 {
1576 p.d_kind = api::CONST_ARRAY;
1577 PARSER_STATE->parseOpApplyTypeAscription(p, type);
1578 }
1579 | identifier[p]
1580 sortSymbol[type, CHECK_DECLARED]
1581 {
1582 PARSER_STATE->parseOpApplyTypeAscription(p, type);
1583 }
1584 )
1585 RPAREN_TOK
1586 ;
1587
1588 /**
1589 * Matches an identifier, which can be a combination of one or more of the
1590 * following, stored in the ParseOp utility class:
1591 * (1) A kind.
1592 * (2) A string name.
1593 * (3) An expression expr.
1594 * For examples, see documentation of qualIdentifier.
1595 */
1596 identifier[cvc5::ParseOp& p]
1597 @init {
1598 cvc5::api::Term f;
1599 cvc5::api::Term f2;
1600 std::vector<uint64_t> numerals;
1601 }
1602 : functionName[p.d_name, CHECK_NONE]
1603
1604 // indexed functions
1605
1606 | LPAREN_TOK INDEX_TOK
1607 ( TESTER_TOK term[f, f2]
1608 {
1609 if (f.getKind() == api::APPLY_CONSTRUCTOR && f.getNumChildren() == 1)
1610 {
1611 // for nullary constructors, must get the operator
1612 f = f[0];
1613 }
1614 if (!f.getSort().isConstructor())
1615 {
1616 PARSER_STATE->parseError(
1617 "Bad syntax for (_ is X), X must be a constructor.");
1618 }
1619 // get the datatype that f belongs to
1620 api::Sort sf = f.getSort().getConstructorCodomainSort();
1621 api::Datatype d = sf.getDatatype();
1622 // lookup by name
1623 api::DatatypeConstructor dc = d.getConstructor(f.toString());
1624 p.d_expr = dc.getTesterTerm();
1625 }
1626 | UPDATE_TOK term[f, f2]
1627 {
1628 if (!f.getSort().isSelector())
1629 {
1630 PARSER_STATE->parseError(
1631 "Bad syntax for (_ update X), X must be a selector.");
1632 }
1633 std::string sname = f.toString();
1634 // get the datatype that f belongs to
1635 api::Sort sf = f.getSort().getSelectorDomainSort();
1636 api::Datatype d = sf.getDatatype();
1637 // find the selector
1638 api::DatatypeSelector ds = d.getSelector(f.toString());
1639 // get the updater term
1640 p.d_expr = ds.getUpdaterTerm();
1641 }
1642 | TUPLE_PROJECT_TOK nonemptyNumeralList[numerals]
1643 {
1644 // we adopt a special syntax (_ tuple_project i_1 ... i_n) where
1645 // i_1, ..., i_n are numerals
1646 p.d_kind = api::TUPLE_PROJECT;
1647 std::vector<uint32_t> indices(numerals.size());
1648 for(size_t i = 0; i < numerals.size(); ++i)
1649 {
1650 // convert uint64_t to uint32_t
1651 indices[i] = numerals[i];
1652 }
1653 p.d_op = SOLVER->mkOp(api::TUPLE_PROJECT, indices);
1654 }
1655 | sym=SIMPLE_SYMBOL nonemptyNumeralList[numerals]
1656 {
1657 std::string opName = AntlrInput::tokenText($sym);
1658 api::Kind k = PARSER_STATE->getIndexedOpKind(opName);
1659 if (k == api::APPLY_SELECTOR || k == api::APPLY_UPDATER)
1660 {
1661 // we adopt a special syntax (_ tuple_select n) and (_ tuple_update n)
1662 // for tuple selectors and updaters
1663 if (numerals.size() != 1)
1664 {
1665 PARSER_STATE->parseError(
1666 "Unexpected syntax for tuple selector or updater.");
1667 }
1668 // The operator is dependent upon inferring the type of the arguments,
1669 // and hence the type is not available yet. Hence, we remember the
1670 // index as a numeral in the parse operator.
1671 p.d_kind = k;
1672 p.d_expr = SOLVER->mkInteger(numerals[0]);
1673 }
1674 else if (numerals.size() == 1)
1675 {
1676 p.d_op = SOLVER->mkOp(k, numerals[0]);
1677 }
1678 else if (numerals.size() == 2)
1679 {
1680 p.d_op = SOLVER->mkOp(k, numerals[0], numerals[1]);
1681 }
1682 else
1683 {
1684 PARSER_STATE->parseError(
1685 "Unexpected number of numerals for indexed symbol.");
1686 }
1687 }
1688 )
1689 RPAREN_TOK
1690 ;
1691
1692 /**
1693 * Matches an atomic term (a term with no subterms).
1694 * @return the expression expr representing the term or formula.
1695 */
1696 termAtomic[cvc5::api::Term& atomTerm]
1697 @init {
1698 cvc5::api::Sort t;
1699 std::string s;
1700 std::vector<uint64_t> numerals;
1701 }
1702 /* constants */
1703 : INTEGER_LITERAL
1704 {
1705 std::string intStr = AntlrInput::tokenText($INTEGER_LITERAL);
1706 atomTerm = SOLVER->mkInteger(intStr);
1707 }
1708 | DECIMAL_LITERAL
1709 {
1710 std::string realStr = AntlrInput::tokenText($DECIMAL_LITERAL);
1711 atomTerm = SOLVER->ensureTermSort(SOLVER->mkReal(realStr),
1712 SOLVER->getRealSort());
1713 }
1714
1715 // Constants using indexed identifiers, e.g. (_ +oo 8 24) (positive infinity
1716 // as a 32-bit floating-point constant)
1717 | LPAREN_TOK INDEX_TOK
1718 ( CHAR_TOK HEX_LITERAL
1719 {
1720 std::string hexStr = AntlrInput::tokenTextSubstr($HEX_LITERAL, 2);
1721 atomTerm = PARSER_STATE->mkCharConstant(hexStr);
1722 }
1723 | FMF_CARD_TOK sortSymbol[t,CHECK_DECLARED] INTEGER_LITERAL
1724 {
1725 uint32_t ubound = AntlrInput::tokenToUnsigned($INTEGER_LITERAL);
1726 atomTerm = SOLVER->mkCardinalityConstraint(t, ubound);
1727 }
1728 | sym=SIMPLE_SYMBOL nonemptyNumeralList[numerals]
1729 {
1730 atomTerm =
1731 PARSER_STATE->mkIndexedConstant(AntlrInput::tokenText($sym),
1732 numerals);
1733 }
1734 )
1735 RPAREN_TOK
1736
1737 // Bit-vector constants
1738 | HEX_LITERAL
1739 {
1740 Assert(AntlrInput::tokenText($HEX_LITERAL).find("#x") == 0);
1741 std::string hexStr = AntlrInput::tokenTextSubstr($HEX_LITERAL, 2);
1742 atomTerm = SOLVER->mkBitVector(hexStr.size() * 4, hexStr, 16);
1743 }
1744 | BINARY_LITERAL
1745 {
1746 Assert(AntlrInput::tokenText($BINARY_LITERAL).find("#b") == 0);
1747 std::string binStr = AntlrInput::tokenTextSubstr($BINARY_LITERAL, 2);
1748 atomTerm = SOLVER->mkBitVector(binStr.size(), binStr, 2);
1749 }
1750
1751 // String constant
1752 | str[s,false] { atomTerm = PARSER_STATE->mkStringConstant(s); }
1753
1754 // NOTE: Theory constants go here
1755
1756 // Empty tuple constant
1757 | TUPLE_CONST_TOK
1758 {
1759 atomTerm = SOLVER->mkTuple(std::vector<api::Sort>(),
1760 std::vector<api::Term>());
1761 }
1762 ;
1763
1764 /**
1765 * Read attribute
1766 */
1767 attribute[cvc5::api::Term& expr, cvc5::api::Term& retExpr]
1768 @init {
1769 api::Term sexpr;
1770 std::string s;
1771 cvc5::api::Term patexpr;
1772 std::vector<cvc5::api::Term> patexprs;
1773 cvc5::api::Term e2;
1774 bool hasValue = false;
1775 api::Kind k;
1776 }
1777 : KEYWORD ( simpleSymbolicExprNoKeyword[s] { hasValue = true; } )?
1778 {
1779 PARSER_STATE->attributeNotSupported(AntlrInput::tokenText($KEYWORD));
1780 }
1781 | ( ATTRIBUTE_PATTERN_TOK { k = api::INST_PATTERN; } |
1782 ATTRIBUTE_POOL_TOK { k = api::INST_POOL; } |
1783 ATTRIBUTE_INST_ADD_TO_POOL_TOK { k = api::INST_ADD_TO_POOL; } |
1784 ATTRIBUTE_SKOLEM_ADD_TO_POOL_TOK{ k = api::SKOLEM_ADD_TO_POOL; }
1785 )
1786 LPAREN_TOK
1787 ( term[patexpr, e2]
1788 { patexprs.push_back( patexpr ); }
1789 )+ RPAREN_TOK
1790 {
1791 retExpr = MK_TERM(k, patexprs);
1792 }
1793 | ATTRIBUTE_NO_PATTERN_TOK term[patexpr, e2]
1794 {
1795 retExpr = MK_TERM(api::INST_NO_PATTERN, patexpr);
1796 }
1797 | tok=( ATTRIBUTE_INST_LEVEL ) INTEGER_LITERAL
1798 {
1799 std::stringstream sIntLit;
1800 sIntLit << $INTEGER_LITERAL;
1801 api::Term keyword = SOLVER->mkString("quant-inst-max-level");
1802 api::Term n = SOLVER->mkInteger(sIntLit.str());
1803 retExpr = MK_TERM(api::INST_ATTRIBUTE, keyword, n);
1804 }
1805 | tok=( ATTRIBUTE_QUANTIFIER_ID_TOK ) symbol[s,CHECK_UNDECLARED,SYM_VARIABLE]
1806 {
1807 api::Term keyword = SOLVER->mkString("qid");
1808 // must create a variable whose name is the name of the quantified
1809 // formula, not a string.
1810 api::Term name = SOLVER->mkConst(SOLVER->getBooleanSort(), s);
1811 retExpr = MK_TERM(api::INST_ATTRIBUTE, keyword, name);
1812 }
1813 | ATTRIBUTE_NAMED_TOK symbol[s,CHECK_UNDECLARED,SYM_VARIABLE]
1814 {
1815 // notify that expression was given a name
1816 DefineFunctionCommand* defFunCmd =
1817 new DefineFunctionCommand(s, expr.getSort(), expr);
1818 defFunCmd->setMuted(true);
1819 PARSER_STATE->preemptCommand(defFunCmd);
1820 PARSER_STATE->notifyNamedExpression(expr, s);
1821 }
1822 ;
1823
1824 /**
1825 * Matches a sequence of terms and puts them into the formulas
1826 * vector.
1827 * @param formulas the vector to fill with terms
1828 * @param expr an cvc5::api::Term reference for the elements of the sequence
1829 */
1830 /* NOTE: We pass an cvc5::api::Term in here just to avoid allocating a fresh cvc5::api::Term every
1831 * time through this rule. */
1832 termList[std::vector<cvc5::api::Term>& formulas, cvc5::api::Term& expr]
1833 @declarations {
1834 cvc5::api::Term expr2;
1835 }
1836 : ( term[expr, expr2] { formulas.push_back(expr); } )+
1837 ;
1838
1839 /**
1840 * Matches a string, and strips off the quotes.
1841 */
1842 str[std::string& s, bool fsmtlib]
1843 : STRING_LITERAL
1844 {
1845 s = AntlrInput::tokenText($STRING_LITERAL);
1846 /* strip off the quotes */
1847 s = s.substr(1, s.size() - 2);
1848 for (size_t i = 0; i < s.size(); i++)
1849 {
1850 if ((unsigned)s[i] > 127 && !isprint(s[i]))
1851 {
1852 PARSER_STATE->parseError(
1853 "Extended/unprintable characters are not "
1854 "part of SMT-LIB, and they must be encoded "
1855 "as escape sequences");
1856 }
1857 }
1858 if (fsmtlib || PARSER_STATE->escapeDupDblQuote())
1859 {
1860 char* p_orig = strdup(s.c_str());
1861 char *p = p_orig, *q = p_orig;
1862 while (*q != '\0')
1863 {
1864 if (PARSER_STATE->escapeDupDblQuote() && *q == '"')
1865 {
1866 // Handle SMT-LIB >=2.5 standard escape '""'.
1867 ++q;
1868 Assert(*q == '"');
1869 }
1870 else if (!PARSER_STATE->escapeDupDblQuote() && *q == '\\')
1871 {
1872 ++q;
1873 // Handle SMT-LIB 2.0 standard escapes '\\' and '\"'.
1874 if (*q != '\\' && *q != '"')
1875 {
1876 Assert(*q != '\0');
1877 *p++ = '\\';
1878 }
1879 }
1880 *p++ = *q++;
1881 }
1882 *p = '\0';
1883 s = p_orig;
1884 free(p_orig);
1885 }
1886 }
1887 ;
1888
1889 quantOp[cvc5::api::Kind& kind]
1890 @init {
1891 Trace("parser") << "quant: " << AntlrInput::tokenText(LT(1)) << std::endl;
1892 }
1893 : EXISTS_TOK { $kind = api::EXISTS; }
1894 | FORALL_TOK { $kind = api::FORALL; }
1895 ;
1896
1897 /**
1898 * Matches a (possibly undeclared) function symbol (returning the string)
1899 * @param check what kind of check to do with the symbol
1900 */
1901 functionName[std::string& name, cvc5::parser::DeclarationCheck check]
1902 : symbol[name,check,SYM_VARIABLE]
1903 ;
1904
1905 /**
1906 * Matches a sequence of sort symbols and fills them into the given
1907 * vector.
1908 */
1909 sortList[std::vector<cvc5::api::Sort>& sorts]
1910 @declarations {
1911 cvc5::api::Sort t;
1912 }
1913 : ( sortSymbol[t,CHECK_DECLARED] { sorts.push_back(t); } )*
1914 ;
1915
1916 nonemptySortList[std::vector<cvc5::api::Sort>& sorts]
1917 @declarations {
1918 cvc5::api::Sort t;
1919 }
1920 : ( sortSymbol[t,CHECK_DECLARED] { sorts.push_back(t); } )+
1921 ;
1922
1923 /**
1924 * Matches a sequence of (variable,sort) symbol pairs and fills them
1925 * into the given vector.
1926 */
1927 sortedVarList[std::vector<std::pair<std::string, cvc5::api::Sort> >& sortedVars]
1928 @declarations {
1929 std::string name;
1930 cvc5::api::Sort t;
1931 }
1932 : ( LPAREN_TOK symbol[name,CHECK_NONE,SYM_VARIABLE]
1933 sortSymbol[t,CHECK_DECLARED] RPAREN_TOK
1934 { sortedVars.push_back(make_pair(name, t)); }
1935 )*
1936 ;
1937
1938 /**
1939 * Matches a sequence of (variable, sort) symbol pairs, registers them as bound
1940 * variables, and returns a term corresponding to the list of pairs.
1941 */
1942 boundVarList[cvc5::api::Term& expr]
1943 @declarations {
1944 std::vector<std::pair<std::string, cvc5::api::Sort>> sortedVarNames;
1945 }
1946 : LPAREN_TOK sortedVarList[sortedVarNames] RPAREN_TOK
1947 {
1948 std::vector<cvc5::api::Term> args =
1949 PARSER_STATE->bindBoundVars(sortedVarNames);
1950 expr = MK_TERM(api::VARIABLE_LIST, args);
1951 }
1952 ;
1953
1954 /**
1955 * Matches the sort symbol, which can be an arbitrary symbol.
1956 * @param check the check to perform on the name
1957 */
1958 sortName[std::string& name, cvc5::parser::DeclarationCheck check]
1959 : symbol[name,check,SYM_SORT]
1960 ;
1961
1962 sortSymbol[cvc5::api::Sort& t, cvc5::parser::DeclarationCheck check]
1963 @declarations {
1964 std::string name;
1965 std::vector<cvc5::api::Sort> args;
1966 std::vector<uint64_t> numerals;
1967 bool indexed = false;
1968 }
1969 : sortName[name,CHECK_NONE]
1970 {
1971 if(check == CHECK_DECLARED || PARSER_STATE->isDeclared(name, SYM_SORT)) {
1972 t = PARSER_STATE->getSort(name);
1973 } else {
1974 t = PARSER_STATE->mkUnresolvedType(name);
1975 }
1976 }
1977 | LPAREN_TOK (INDEX_TOK {indexed = true;} | {indexed = false;})
1978 symbol[name,CHECK_NONE,SYM_SORT]
1979 ( nonemptyNumeralList[numerals]
1980 {
1981 if (!indexed)
1982 {
1983 std::stringstream ss;
1984 ss << "SMT-LIB requires use of an indexed sort here, e.g. (_ " << name
1985 << " ...)";
1986 PARSER_STATE->parseError(ss.str());
1987 }
1988 if( name == "BitVec" ) {
1989 if( numerals.size() != 1 ) {
1990 PARSER_STATE->parseError("Illegal bitvector type.");
1991 }
1992 if(numerals.front() == 0) {
1993 PARSER_STATE->parseError("Illegal bitvector size: 0");
1994 }
1995 t = SOLVER->mkBitVectorSort(numerals.front());
1996 } else if ( name == "FloatingPoint" ) {
1997 if( numerals.size() != 2 ) {
1998 PARSER_STATE->parseError("Illegal floating-point type.");
1999 }
2000 if(!validExponentSize(numerals[0])) {
2001 PARSER_STATE->parseError("Illegal floating-point exponent size");
2002 }
2003 if(!validSignificandSize(numerals[1])) {
2004 PARSER_STATE->parseError("Illegal floating-point significand size");
2005 }
2006 t = SOLVER->mkFloatingPointSort(numerals[0],numerals[1]);
2007 } else {
2008 std::stringstream ss;
2009 ss << "unknown indexed sort symbol `" << name << "'";
2010 PARSER_STATE->parseError(ss.str());
2011 }
2012 }
2013 | sortList[args]
2014 { if( indexed ) {
2015 std::stringstream ss;
2016 ss << "Unexpected use of indexing operator `_' before `" << name
2017 << "', try leaving it out";
2018 PARSER_STATE->parseError(ss.str());
2019 }
2020 if(args.empty()) {
2021 PARSER_STATE->parseError("Extra parentheses around sort name not "
2022 "permitted in SMT-LIB");
2023 } else if(name == "Array" &&
2024 PARSER_STATE->isTheoryEnabled(theory::THEORY_ARRAYS) ) {
2025 if(args.size() != 2) {
2026 PARSER_STATE->parseError("Illegal array type.");
2027 }
2028 t = SOLVER->mkArraySort( args[0], args[1] );
2029 } else if(name == "Set" &&
2030 PARSER_STATE->isTheoryEnabled(theory::THEORY_SETS) ) {
2031 if(args.size() != 1) {
2032 PARSER_STATE->parseError("Illegal set type.");
2033 }
2034 t = SOLVER->mkSetSort( args[0] );
2035 }
2036 else if(name == "Bag" &&
2037 PARSER_STATE->isTheoryEnabled(theory::THEORY_BAGS) ) {
2038 if(args.size() != 1) {
2039 PARSER_STATE->parseError("Illegal bag type.");
2040 }
2041 t = SOLVER->mkBagSort( args[0] );
2042 }
2043 else if(name == "Seq" && !PARSER_STATE->strictModeEnabled() &&
2044 PARSER_STATE->isTheoryEnabled(theory::THEORY_STRINGS) ) {
2045 if(args.size() != 1) {
2046 PARSER_STATE->parseError("Illegal sequence type.");
2047 }
2048 t = SOLVER->mkSequenceSort( args[0] );
2049 } else if (name == "Tuple" && !PARSER_STATE->strictModeEnabled()) {
2050 t = SOLVER->mkTupleSort(args);
2051 } else if(check == CHECK_DECLARED ||
2052 PARSER_STATE->isDeclared(name, SYM_SORT)) {
2053 t = PARSER_STATE->getSort(name, args);
2054 } else {
2055 // make unresolved type
2056 if(args.empty()) {
2057 t = PARSER_STATE->mkUnresolvedType(name);
2058 Trace("parser-param") << "param: make unres type " << name
2059 << std::endl;
2060 } else {
2061 t = PARSER_STATE->mkUnresolvedTypeConstructor(name,args);
2062 t = t.instantiate( args );
2063 Trace("parser-param")
2064 << "param: make unres param type " << name << " " << args.size()
2065 << " " << PARSER_STATE->getArity( name ) << std::endl;
2066 }
2067 }
2068 }
2069 ) RPAREN_TOK
2070 | LPAREN_TOK HO_ARROW_TOK sortList[args] RPAREN_TOK
2071 {
2072 if(args.size()<2) {
2073 PARSER_STATE->parseError("Arrow types must have at least 2 arguments");
2074 }
2075 //flatten the type
2076 api::Sort rangeType = args.back();
2077 args.pop_back();
2078 t = PARSER_STATE->mkFlatFunctionType( args, rangeType );
2079 }
2080 ;
2081
2082 /**
2083 * Matches a list of symbols, with check and type arguments as for the
2084 * symbol[] rule below.
2085 */
2086 symbolList[std::vector<std::string>& names,
2087 cvc5::parser::DeclarationCheck check,
2088 cvc5::parser::SymbolType type]
2089 @declarations {
2090 std::string id;
2091 }
2092 : ( symbol[id,check,type] { names.push_back(id); } )*
2093 ;
2094
2095 /**
2096 * Matches an symbol and sets the string reference parameter id.
2097 * @param id string to hold the symbol
2098 * @param check what kinds of check to do on the symbol
2099 * @param type the intended namespace for the symbol
2100 */
2101 symbol[std::string& id,
2102 cvc5::parser::DeclarationCheck check,
2103 cvc5::parser::SymbolType type]
2104 : SIMPLE_SYMBOL
2105 { id = AntlrInput::tokenText($SIMPLE_SYMBOL);
2106 if(!PARSER_STATE->isAbstractValue(id)) {
2107 // if an abstract value, SolverEngine handles declaration
2108 PARSER_STATE->checkDeclaration(id, check, type);
2109 }
2110 }
2111 | QUOTED_SYMBOL
2112 { id = AntlrInput::tokenText($QUOTED_SYMBOL);
2113 /* strip off the quotes */
2114 id = id.substr(1, id.size() - 2);
2115 if(!PARSER_STATE->isAbstractValue(id)) {
2116 // if an abstract value, SolverEngine handles declaration
2117 PARSER_STATE->checkDeclaration(id, check, type);
2118 }
2119 }
2120 | UNTERMINATED_QUOTED_SYMBOL
2121 ( EOF
2122 { PARSER_STATE->unexpectedEOF("unterminated |quoted| symbol"); }
2123 | '\\'
2124 { PARSER_STATE->unexpectedEOF("backslash not permitted in |quoted| "
2125 "symbol"); }
2126 )
2127 ;
2128
2129 /**
2130 * Matches a nonempty list of numerals.
2131 * @param numerals the (empty) vector to house the numerals.
2132 */
2133 nonemptyNumeralList[std::vector<uint64_t>& numerals]
2134 : ( INTEGER_LITERAL
2135 { numerals.push_back(AntlrInput::tokenToUnsigned($INTEGER_LITERAL)); }
2136 )+
2137 ;
2138
2139 /**
2140 * Parses a datatype definition
2141 */
2142 datatypeDef[bool isCo, std::vector<cvc5::api::DatatypeDecl>& datatypes,
2143 std::vector< cvc5::api::Sort >& params]
2144 @init {
2145 std::string id;
2146 }
2147 /* This really needs to be CHECK_NONE, or mutually-recursive
2148 * datatypes won't work, because this type will already be
2149 * "defined" as an unresolved type; don't worry, we check
2150 * below. */
2151 : symbol[id,CHECK_NONE,SYM_SORT] { PARSER_STATE->pushScope(); }
2152 {
2153 datatypes.push_back(SOLVER->mkDatatypeDecl(id, params, isCo));
2154 }
2155 ( LPAREN_TOK constructorDef[datatypes.back()] RPAREN_TOK )+
2156 { PARSER_STATE->popScope(); }
2157 ;
2158
2159 /**
2160 * Parses a constructor defintion for type
2161 */
2162 constructorDef[cvc5::api::DatatypeDecl& type]
2163 @init {
2164 std::string id;
2165 cvc5::api::DatatypeConstructorDecl* ctor = NULL;
2166 }
2167 : symbol[id,CHECK_NONE,SYM_VARIABLE]
2168 {
2169 ctor = new api::DatatypeConstructorDecl(
2170 SOLVER->mkDatatypeConstructorDecl(id));
2171 }
2172 ( LPAREN_TOK selector[*ctor] RPAREN_TOK )*
2173 { // make the constructor
2174 type.addConstructor(*ctor);
2175 Trace("parser-idt") << "constructor: " << id.c_str() << std::endl;
2176 delete ctor;
2177 }
2178 ;
2179
2180 selector[cvc5::api::DatatypeConstructorDecl& ctor]
2181 @init {
2182 std::string id;
2183 cvc5::api::Sort t, t2;
2184 }
2185 : symbol[id,CHECK_NONE,SYM_SORT] sortSymbol[t,CHECK_NONE]
2186 {
2187 ctor.addSelector(id, t);
2188 Trace("parser-idt") << "selector: " << id.c_str()
2189 << " of type " << t << std::endl;
2190 }
2191 ;
2192
2193 // Base SMT-LIB tokens
2194 ASSERT_TOK : 'assert';
2195 CHECK_SAT_TOK : 'check-sat';
2196 CHECK_SAT_ASSUMING_TOK : 'check-sat-assuming';
2197 DECLARE_FUN_TOK : 'declare-fun';
2198 DECLARE_SORT_TOK : 'declare-sort';
2199 DEFINE_FUN_TOK : 'define-fun';
2200 DEFINE_FUN_REC_TOK : 'define-fun-rec';
2201 DEFINE_FUNS_REC_TOK : 'define-funs-rec';
2202 DEFINE_SORT_TOK : 'define-sort';
2203 GET_VALUE_TOK : 'get-value';
2204 GET_ASSIGNMENT_TOK : 'get-assignment';
2205 GET_ASSERTIONS_TOK : 'get-assertions';
2206 GET_PROOF_TOK : 'get-proof';
2207 GET_UNSAT_ASSUMPTIONS_TOK : 'get-unsat-assumptions';
2208 GET_UNSAT_CORE_TOK : 'get-unsat-core';
2209 GET_DIFFICULTY_TOK : 'get-difficulty';
2210 GET_LEARNED_LITERALS_TOK : { !PARSER_STATE->strictModeEnabled() }? 'get-learned-literals';
2211 EXIT_TOK : 'exit';
2212 RESET_TOK : 'reset';
2213 RESET_ASSERTIONS_TOK : 'reset-assertions';
2214 LET_TOK : 'let';
2215 ATTRIBUTE_TOK : '!';
2216 LPAREN_TOK : '(';
2217 RPAREN_TOK : ')';
2218 INDEX_TOK : '_';
2219 SET_LOGIC_TOK : 'set-logic';
2220 SET_INFO_TOK : 'set-info';
2221 GET_INFO_TOK : 'get-info';
2222 SET_OPTION_TOK : 'set-option';
2223 GET_OPTION_TOK : 'get-option';
2224 PUSH_TOK : 'push';
2225 POP_TOK : 'pop';
2226 AS_TOK : 'as';
2227 CONST_TOK : { !PARSER_STATE->strictModeEnabled() }? 'const';
2228
2229 // extended commands
2230 DECLARE_CODATATYPE_TOK : { PARSER_STATE->v2_6() || PARSER_STATE->sygus() }? 'declare-codatatype';
2231 DECLARE_DATATYPE_TOK : { PARSER_STATE->v2_6() || PARSER_STATE->sygus() }? 'declare-datatype';
2232 DECLARE_DATATYPES_2_5_TOK : { !( PARSER_STATE->v2_6() || PARSER_STATE->sygus() ) }?'declare-datatypes';
2233 DECLARE_DATATYPES_TOK : { PARSER_STATE->v2_6() || PARSER_STATE->sygus() }?'declare-datatypes';
2234 DECLARE_CODATATYPES_2_5_TOK : { !( PARSER_STATE->v2_6() || PARSER_STATE->sygus() ) }?'declare-codatatypes';
2235 DECLARE_CODATATYPES_TOK : { PARSER_STATE->v2_6() || PARSER_STATE->sygus() }?'declare-codatatypes';
2236 PAR_TOK : { PARSER_STATE->v2_6() || PARSER_STATE->sygus() }?'par';
2237 SET_COMPREHENSION_TOK : { PARSER_STATE->isTheoryEnabled(theory::THEORY_SETS) }?'set.comprehension';
2238 TESTER_TOK : { ( PARSER_STATE->v2_6() || PARSER_STATE->sygus() ) && PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }?'is';
2239 UPDATE_TOK : { ( PARSER_STATE->v2_6() || PARSER_STATE->sygus() ) && PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }?'update';
2240 MATCH_TOK : { ( PARSER_STATE->v2_6() || PARSER_STATE->sygus() ) && PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }?'match';
2241 GET_MODEL_TOK : 'get-model';
2242 BLOCK_MODEL_TOK : 'block-model';
2243 BLOCK_MODEL_VALUES_TOK : 'block-model-values';
2244 ECHO_TOK : 'echo';
2245 DECLARE_SORTS_TOK : 'declare-sorts';
2246 DECLARE_FUNS_TOK : 'declare-funs';
2247 DECLARE_PREDS_TOK : 'declare-preds';
2248 DECLARE_CONST_TOK : 'declare-const';
2249 DEFINE_CONST_TOK : 'define-const';
2250 SIMPLIFY_TOK : 'simplify';
2251 INCLUDE_TOK : 'include';
2252 GET_QE_TOK : 'get-qe';
2253 GET_QE_DISJUNCT_TOK : 'get-qe-disjunct';
2254 GET_ABDUCT_TOK : 'get-abduct';
2255 GET_ABDUCT_NEXT_TOK : 'get-abduct-next';
2256 GET_INTERPOL_TOK : 'get-interpol';
2257 GET_INTERPOL_NEXT_TOK : 'get-interpol-next';
2258 DECLARE_HEAP : 'declare-heap';
2259 DECLARE_POOL : 'declare-pool';
2260
2261 // SyGuS commands
2262 SYNTH_FUN_TOK : { PARSER_STATE->sygus() }?'synth-fun';
2263 SYNTH_INV_TOK : { PARSER_STATE->sygus()}?'synth-inv';
2264 CHECK_SYNTH_TOK : { PARSER_STATE->sygus()}?'check-synth';
2265 CHECK_SYNTH_NEXT_TOK : { PARSER_STATE->sygus()}?'check-synth-next';
2266 DECLARE_VAR_TOK : { PARSER_STATE->sygus()}?'declare-var';
2267 CONSTRAINT_TOK : { PARSER_STATE->sygus()}?'constraint';
2268 ASSUME_TOK : { PARSER_STATE->sygus()}?'assume';
2269 INV_CONSTRAINT_TOK : { PARSER_STATE->sygus()}?'inv-constraint';
2270 SET_FEATURE_TOK : { PARSER_STATE->sygus() }? 'set-feature';
2271 SYGUS_CONSTANT_TOK : { PARSER_STATE->sygus() }? 'Constant';
2272 SYGUS_VARIABLE_TOK : { PARSER_STATE->sygus() }? 'Variable';
2273
2274 // attributes
2275 ATTRIBUTE_PATTERN_TOK : ':pattern';
2276 ATTRIBUTE_NO_PATTERN_TOK : ':no-pattern';
2277 ATTRIBUTE_POOL_TOK : ':pool';
2278 ATTRIBUTE_INST_ADD_TO_POOL_TOK : ':inst-add-to-pool';
2279 ATTRIBUTE_SKOLEM_ADD_TO_POOL_TOK : ':skolem-add-to-pool';
2280 ATTRIBUTE_NAMED_TOK : ':named';
2281 ATTRIBUTE_INST_LEVEL : ':quant-inst-max-level';
2282 ATTRIBUTE_QUANTIFIER_ID_TOK : ':qid';
2283
2284 // operators (NOTE: theory symbols go here)
2285 EXISTS_TOK : 'exists';
2286 FORALL_TOK : 'forall';
2287
2288 CHAR_TOK : { PARSER_STATE->isTheoryEnabled(theory::THEORY_STRINGS) }? 'char';
2289 TUPLE_CONST_TOK: { PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }? 'tuple';
2290 TUPLE_PROJECT_TOK: { PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }? 'tuple_project';
2291 FMF_CARD_TOK: { !PARSER_STATE->strictModeEnabled() && PARSER_STATE->hasCardinalityConstraints() }? 'fmf.card';
2292
2293 HO_ARROW_TOK : { PARSER_STATE->isHoEnabled() }? '->';
2294 HO_LAMBDA_TOK : { PARSER_STATE->isHoEnabled() }? 'lambda';
2295
2296 /**
2297 * A sequence of printable ASCII characters (except backslash) that starts
2298 * and ends with | and does not otherwise contain |.
2299 *
2300 * You shouldn't generally use this in parser rules, as the |quoting|
2301 * will be part of the token text. Use the symbol[] parser rule instead.
2302 */
2303 QUOTED_SYMBOL
2304 : '|' ~('|' | '\\')* '|'
2305 ;
2306 UNTERMINATED_QUOTED_SYMBOL
2307 : '|' ~('|' | '\\')*
2308 ;
2309
2310 /**
2311 * Matches a keyword from the input. A keyword is a simple symbol prefixed
2312 * with a colon.
2313 */
2314 KEYWORD
2315 : ':' (ALPHA | DIGIT | SYMBOL_CHAR)+
2316 ;
2317
2318 /**
2319 * Matches a "simple" symbol: a non-empty sequence of letters, digits and
2320 * the characters + - / * = % ? ! . $ ~ & ^ < > @ that does not start with a
2321 * digit, and is not the special reserved symbols '!' or '_'.
2322 */
2323 SIMPLE_SYMBOL
2324 : ALPHA (ALPHA | DIGIT | SYMBOL_CHAR)*
2325 | SYMBOL_CHAR (ALPHA | DIGIT | SYMBOL_CHAR)+
2326 | SYMBOL_CHAR_NOUNDERSCORE_NOATTRIBUTE
2327 ;
2328
2329 /**
2330 * Matches and skips whitespace in the input.
2331 */
2332 WHITESPACE
2333 : (' ' | '\t' | '\f' | '\r' | '\n')+ { SKIP(); }
2334 ;
2335
2336 /**
2337 * Matches an integer constant from the input (non-empty sequence of
2338 * digits, with no leading zeroes).
2339 */
2340 INTEGER_LITERAL
2341 : NUMERAL
2342 ;
2343
2344 /**
2345 * Match an integer constant. In non-strict mode, this is any sequence
2346 * of digits. In strict mode, non-zero integers can't have leading
2347 * zeroes.
2348 */
2349 fragment NUMERAL
2350 @init {
2351 char *start = (char*) GETCHARINDEX();
2352 }
2353 : DIGIT+
2354 { Trace("parser-extra") << "NUMERAL: "
2355 << (uintptr_t)start << ".." << GETCHARINDEX()
2356 << " strict? " << (bool)(PARSER_STATE->strictModeEnabled())
2357 << " ^0? " << (bool)(*start == '0')
2358 << " len>1? " << (bool)(start < (char*)(GETCHARINDEX() - 1))
2359 << std::endl; }
2360 { !PARSER_STATE->strictModeEnabled() ||
2361 *start != '0' ||
2362 start == (char*)(GETCHARINDEX() - 1) }?
2363 ;
2364
2365 /**
2366 * Matches a decimal constant from the input.
2367 */
2368 DECIMAL_LITERAL
2369 : NUMERAL '.' DIGIT+
2370 ;
2371
2372 /**
2373 * Matches a hexadecimal constant.
2374 */
2375 HEX_LITERAL
2376 : '#x' HEX_DIGIT+
2377 ;
2378
2379 /**
2380 * Matches a binary constant.
2381 */
2382 BINARY_LITERAL
2383 : '#b' ('0' | '1')+
2384 ;
2385
2386 /**
2387 * Matches a double-quoted string literal. Depending on the language that is
2388 * being parsed, different escape sequences are supported:
2389 *
2390 * For SMT-LIB 2.0 the sequence \" is interpreted as a double quote (") and the
2391 * sequence \\ is interpreted as a backslash (\).
2392 *
2393 * For SMT-LIB >=2.5 and SyGuS a double-quote inside a string is escaped with
2394 * "", e.g., "This is a string literal with "" a single, embedded double
2395 * quote."
2396 *
2397 * You shouldn't generally use this in parser rules, as the quotes
2398 * will be part of the token text. Use the str[] parser rule instead.
2399 */
2400 STRING_LITERAL
2401 : { !PARSER_STATE->escapeDupDblQuote() }?=>
2402 '"' ('\\' . | ~('\\' | '"'))* '"'
2403 | { PARSER_STATE->escapeDupDblQuote() }?=>
2404 '"' (~('"') | '""')* '"'
2405 ;
2406
2407 /**
2408 * Matches the comments and ignores them
2409 */
2410 COMMENT
2411 : ';' (~('\n' | '\r'))* { SKIP(); }
2412 ;
2413
2414 /**
2415 * Matches any letter ('a'-'z' and 'A'-'Z').
2416 */
2417 fragment
2418 ALPHA
2419 : 'a'..'z'
2420 | 'A'..'Z'
2421 ;
2422
2423 /**
2424 * Matches the digits (0-9)
2425 */
2426 fragment DIGIT : '0'..'9';
2427
2428 fragment HEX_DIGIT : DIGIT | 'a'..'f' | 'A'..'F';
2429
2430 /**
2431 * Matches the characters that may appear as a one-character "symbol"
2432 * (which excludes _ and !, which are reserved words in SMT-LIB).
2433 */
2434 fragment SYMBOL_CHAR_NOUNDERSCORE_NOATTRIBUTE
2435 : '+' | '-' | '/' | '*' | '=' | '%' | '?' | '.' | '$' | '~'
2436 | '&' | '^' | '<' | '>' | '@'
2437 ;
2438
2439 /**
2440 * Matches the characters that may appear in a "symbol" (i.e., an identifier)
2441 */
2442 fragment SYMBOL_CHAR
2443 : SYMBOL_CHAR_NOUNDERSCORE_NOATTRIBUTE | '_' | '!'
2444 ;