From 215519d099aee88bf53bddc71f071382484d29c0 Mon Sep 17 00:00:00 2001 From: Andrew Reynolds Date: Thu, 14 Oct 2021 15:32:37 -0500 Subject: [PATCH] Improve parser for tuple select (#7364) --- src/parser/smt2/Smt2.g | 17 +++++------------ src/parser/smt2/smt2.cpp | 13 +++++++++---- src/parser/smt2/smt2.h | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/parser/smt2/Smt2.g b/src/parser/smt2/Smt2.g index 478edb651..bff4f72ab 100644 --- a/src/parser/smt2/Smt2.g +++ b/src/parser/smt2/Smt2.g @@ -1648,7 +1648,7 @@ identifier[cvc5::ParseOp& p] if (!f.getSort().isConstructor()) { PARSER_STATE->parseError( - "Bad syntax for test (_ is X), X must be a constructor."); + "Bad syntax for (_ is X), X must be a constructor."); } // get the datatype that f belongs to api::Sort sf = f.getSort().getConstructorCodomainSort(); @@ -1662,7 +1662,7 @@ identifier[cvc5::ParseOp& p] if (!f.getSort().isSelector()) { PARSER_STATE->parseError( - "Bad syntax for test (_ update X), X must be a selector."); + "Bad syntax for (_ update X), X must be a selector."); } std::string sname = f.toString(); // get the datatype that f belongs to @@ -1673,13 +1673,6 @@ identifier[cvc5::ParseOp& p] // get the updater term p.d_expr = ds.getUpdaterTerm(); } - | TUPLE_SEL_TOK m=INTEGER_LITERAL - { - // we adopt a special syntax (_ tupSel n) - p.d_kind = api::APPLY_SELECTOR; - // put m in expr so that the caller can deal with this case - p.d_expr = SOLVER->mkInteger(AntlrInput::tokenToUnsigned($m)); - } | TUPLE_PROJECT_TOK nonemptyNumeralList[numerals] { // we adopt a special syntax (_ tuple_project i_1 ... i_n) where @@ -1697,9 +1690,10 @@ identifier[cvc5::ParseOp& p] { std::string opName = AntlrInput::tokenText($sym); api::Kind k = PARSER_STATE->getIndexedOpKind(opName); - if (k == api::APPLY_UPDATER) + if (k == api::APPLY_SELECTOR || k == api::APPLY_UPDATER) { - // we adopt a special syntax (_ tuple_update n) for tuple updaters + // we adopt a special syntax (_ tuple_select n) and (_ tuple_update n) + // for tuple selectors and updaters if (numerals.size() != 1) { PARSER_STATE->parseError( @@ -2314,7 +2308,6 @@ FORALL_TOK : 'forall'; CHAR_TOK : { PARSER_STATE->isTheoryEnabled(theory::THEORY_STRINGS) }? 'char'; TUPLE_CONST_TOK: { PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }? 'tuple'; -TUPLE_SEL_TOK: { PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }? 'tuple_select'; TUPLE_PROJECT_TOK: { PARSER_STATE->isTheoryEnabled(theory::THEORY_DATATYPES) }? 'tuple_project'; HO_ARROW_TOK : { PARSER_STATE->isHoEnabled() }? '->'; diff --git a/src/parser/smt2/smt2.cpp b/src/parser/smt2/smt2.cpp index b186c2b2a..a69542b17 100644 --- a/src/parser/smt2/smt2.cpp +++ b/src/parser/smt2/smt2.cpp @@ -135,6 +135,11 @@ void Smt2::addDatatypesOperators() { Parser::addOperator(api::APPLY_UPDATER); addOperator(api::DT_SIZE, "dt.size"); + // Notice that tuple operators, we use the generic APPLY_SELECTOR and + // APPLY_UPDATER kinds. These are processed based on the context + // in which they are parsed, e.g. when parsing identifiers. + addIndexedOperator( + api::APPLY_SELECTOR, api::APPLY_SELECTOR, "tuple_select"); addIndexedOperator(api::APPLY_UPDATER, api::APPLY_UPDATER, "tuple_update"); } } @@ -288,7 +293,7 @@ void Smt2::addOperator(api::Kind kind, const std::string& name) Debug("parser") << "Smt2::addOperator( " << kind << ", " << name << " )" << std::endl; Parser::addOperator(kind); - operatorKindMap[name] = kind; + d_operatorKindMap[name] = kind; } void Smt2::addIndexedOperator(api::Kind tKind, @@ -302,11 +307,11 @@ void Smt2::addIndexedOperator(api::Kind tKind, api::Kind Smt2::getOperatorKind(const std::string& name) const { // precondition: isOperatorEnabled(name) - return operatorKindMap.find(name)->second; + return d_operatorKindMap.find(name)->second; } bool Smt2::isOperatorEnabled(const std::string& name) const { - return operatorKindMap.find(name) != operatorKindMap.end(); + return d_operatorKindMap.find(name) != d_operatorKindMap.end(); } bool Smt2::isTheoryEnabled(theory::TheoryId theory) const @@ -437,7 +442,7 @@ void Smt2::reset() { d_logicSet = false; d_seenSetLogic = false; d_logic = LogicInfo(); - operatorKindMap.clear(); + d_operatorKindMap.clear(); d_lastNamedTerm = std::pair(); } diff --git a/src/parser/smt2/smt2.h b/src/parser/smt2/smt2.h index fd68732fe..8d8f8febe 100644 --- a/src/parser/smt2/smt2.h +++ b/src/parser/smt2/smt2.h @@ -50,7 +50,7 @@ class Smt2 : public Parser bool d_seenSetLogic; LogicInfo d_logic; - std::unordered_map operatorKindMap; + std::unordered_map d_operatorKindMap; /** * Maps indexed symbols to the kind of the operator (e.g. "extract" to * BITVECTOR_EXTRACT). -- 2.30.2