From 840a9b6e078035dd8b887c11984cf7f8b0313642 Mon Sep 17 00:00:00 2001 From: gereon Date: Tue, 26 Mar 2013 20:15:12 +0100 Subject: [PATCH] Somewhat works now. Still has at least one bug and segfaults afterwards :-) --- src/ir/BooleanVariable.cpp | 3 ++- src/ir/BooleanVariable.h | 2 +- src/ir/IntegerVariable.cpp | 8 ++++--- src/ir/IntegerVariable.h | 2 +- src/ir/Module.cpp | 20 ++++++++++++++---- src/ir/Variable.cpp | 6 ++++-- src/ir/Variable.h | 2 +- src/ir/expressions/BinaryRelationExpression.h | 2 ++ src/ir/expressions/VariableExpression.h | 1 + src/parser/PrismParser.cpp | 21 +++++++++---------- 10 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/ir/BooleanVariable.cpp b/src/ir/BooleanVariable.cpp index 4ef83a879..f233100cd 100644 --- a/src/ir/BooleanVariable.cpp +++ b/src/ir/BooleanVariable.cpp @@ -25,7 +25,8 @@ BooleanVariable::BooleanVariable(uint_fast64_t index, std::string variableName, // Nothing to do here. } -BooleanVariable::BooleanVariable(const BooleanVariable& var, const std::string& newName) : Variable(var, newName) { +BooleanVariable::BooleanVariable(const BooleanVariable& var, const std::string& newName, const std::map& renaming, const std::map& bools, const std::map& ints) + : Variable(var, newName, renaming, bools, ints) { } // Build a string representation of the variable. diff --git a/src/ir/BooleanVariable.h b/src/ir/BooleanVariable.h index fcf5ce602..bf70f01d1 100644 --- a/src/ir/BooleanVariable.h +++ b/src/ir/BooleanVariable.h @@ -35,7 +35,7 @@ public: BooleanVariable(uint_fast64_t index, std::string variableName, std::shared_ptr initialValue = std::shared_ptr(nullptr)); - BooleanVariable(const BooleanVariable& var, const std::string& newName); + BooleanVariable(const BooleanVariable& var, const std::string& newName, const std::map& renaming, const std::map& bools, const std::map& ints); /*! * Retrieves a string representation of this variable. diff --git a/src/ir/IntegerVariable.cpp b/src/ir/IntegerVariable.cpp index d96b2221b..f98ff8861 100644 --- a/src/ir/IntegerVariable.cpp +++ b/src/ir/IntegerVariable.cpp @@ -21,13 +21,15 @@ IntegerVariable::IntegerVariable() : lowerBound(), upperBound() { } // Initializes all members according to the given values. -IntegerVariable::IntegerVariable(uint_fast64_t index, std::string variableName, std::shared_ptr lowerBound, std::shared_ptr upperBound, std::shared_ptr initialValue) : Variable(index, variableName, initialValue), lowerBound(lowerBound), upperBound(upperBound) { +IntegerVariable::IntegerVariable(uint_fast64_t index, std::string variableName, std::shared_ptr lowerBound, std::shared_ptr upperBound, std::shared_ptr initialValue) + : Variable(index, variableName, initialValue), lowerBound(lowerBound), upperBound(upperBound) { if (this->getInitialValue() == nullptr) { this->setInitialValue(lowerBound); } } -IntegerVariable::IntegerVariable(const IntegerVariable& var, const std::string& newName) : Variable(var, newName) { +IntegerVariable::IntegerVariable(const IntegerVariable& var, const std::string& newName, const std::map& renaming, const std::map& bools, const std::map& ints) + : Variable(var, newName, renaming, bools, ints), lowerBound(var.lowerBound->clone(renaming, bools, ints)), upperBound(var.upperBound->clone(renaming, bools, ints)) { } // Return lower bound for variable. @@ -44,7 +46,7 @@ std::shared_ptr IntegerVariable::getUppe // Build a string representation of the variable. std::string IntegerVariable::toString() const { std::stringstream result; - result << this->getName() << ": [" << lowerBound->toString() << ".." << upperBound->toString() << "]"; + result << "int_" << this->getName() << ": [" << lowerBound->toString() << ".." << upperBound->toString() << "]"; if (this->getInitialValue() != nullptr) { result << " init " + this->getInitialValue()->toString(); } diff --git a/src/ir/IntegerVariable.h b/src/ir/IntegerVariable.h index a31ece3e5..8233cbe09 100644 --- a/src/ir/IntegerVariable.h +++ b/src/ir/IntegerVariable.h @@ -37,7 +37,7 @@ public: */ IntegerVariable(uint_fast64_t index, std::string variableName, std::shared_ptr lowerBound, std::shared_ptr upperBound, std::shared_ptr initialValue = std::shared_ptr(nullptr)); - IntegerVariable(const IntegerVariable& var, const std::string& newName); + IntegerVariable(const IntegerVariable& var, const std::string& newName, const std::map& renaming, const std::map& bools, const std::map& ints); /*! * Retrieves the lower bound for this integer variable. diff --git a/src/ir/Module.cpp b/src/ir/Module.cpp index dcacfe198..592c92164 100644 --- a/src/ir/Module.cpp +++ b/src/ir/Module.cpp @@ -36,22 +36,34 @@ Module::Module(std::string moduleName, std::vector b Module::Module(const Module& module, const std::string& moduleName, const std::map& renaming, const VariableAdder& adder) : moduleName(moduleName) { + std::cout << "Renaming module " << module.moduleName << " to " << moduleName << " with " << renaming.size() << " renamings:" << std::endl; + for (auto it: renaming) { + std::cout << "\t" << it.first << " -> " << it.second << std::endl; + } this->booleanVariables.reserve(module.booleanVariables.size()); for (BooleanVariable it: module.booleanVariables) { if (renaming.count(it.getName()) > 0) { - this->booleanVariables.emplace_back(it, renaming.at(it.getName())); - //this->booleanVariablesToIndexMap[renaming.at(it.getName())] = (*boolAdder)(it.getName(), it.getInitialValue()); this->booleanVariablesToIndexMap[renaming.at(it.getName())] = adder.addBooleanVariable(it.getName(), it.getInitialValue()); } else std::cerr << "ERROR: " << moduleName << "." << it.getName() << " was not renamed!" << std::endl; } this->integerVariables.reserve(module.integerVariables.size()); for (IntegerVariable it: module.integerVariables) { if (renaming.count(it.getName()) > 0) { - this->integerVariables.emplace_back(it, renaming.at(it.getName())); - //this->integerVariablesToIndexMap[renaming.at(it.getName())] = (*intAdder)(it.getName(), it.getLowerBound(), it.getUpperBound(), it.getInitialValue()); this->integerVariablesToIndexMap[renaming.at(it.getName())] = adder.addIntegerVariable(it.getName(), it.getLowerBound(), it.getUpperBound(), it.getInitialValue()); } else std::cerr << "ERROR: " << moduleName << "." << it.getName() << " was not renamed!" << std::endl; } + this->booleanVariables.reserve(module.booleanVariables.size()); + for (BooleanVariable it: module.booleanVariables) { + if (renaming.count(it.getName()) > 0) { + this->booleanVariables.emplace_back(it, renaming.at(it.getName()), renaming, this->booleanVariablesToIndexMap, this->integerVariablesToIndexMap); + } else std::cerr << "ERROR: " << moduleName << "." << it.getName() << " was not renamed!" << std::endl; + } + this->integerVariables.reserve(module.integerVariables.size()); + for (IntegerVariable it: module.integerVariables) { + if (renaming.count(it.getName()) > 0) { + this->integerVariables.emplace_back(it, renaming.at(it.getName()), renaming, this->booleanVariablesToIndexMap, this->integerVariablesToIndexMap); + } else std::cerr << "ERROR: " << moduleName << "." << it.getName() << " was not renamed!" << std::endl; + } this->commands.reserve(module.commands.size()); for (Command cmd: module.commands) { diff --git a/src/ir/Variable.cpp b/src/ir/Variable.cpp index caef3aad0..1d762b4b4 100644 --- a/src/ir/Variable.cpp +++ b/src/ir/Variable.cpp @@ -9,6 +9,7 @@ #include #include +#include namespace storm { @@ -24,8 +25,9 @@ Variable::Variable(uint_fast64_t index, std::string variableName, std::shared_pt // Nothing to do here. } -Variable::Variable(const Variable& var, const std::string& newName) : Variable(var.index, newName, var.initialValue) { - // Nothing to do here +Variable::Variable(const Variable& var, const std::string& newName, const std::map& renaming, const std::map& bools, const std::map& ints) + : Variable(var.index, newName, var.initialValue->clone(renaming, bools, ints)) { + std::cout << "Cloning Variable " << var.variableName << " to " << newName << std::endl; } // Return the name of the variable. diff --git a/src/ir/Variable.h b/src/ir/Variable.h index 2348346c3..e144d06aa 100644 --- a/src/ir/Variable.h +++ b/src/ir/Variable.h @@ -40,7 +40,7 @@ public: * @param var Variable to copy. * @param newName New name of this variable. */ - Variable(const Variable& var, const std::string& newName); + Variable(const Variable& var, const std::string& newName, const std::map& renaming, const std::map& bools, const std::map& ints); /*! * Retrieves the name of the variable. diff --git a/src/ir/expressions/BinaryRelationExpression.h b/src/ir/expressions/BinaryRelationExpression.h index 38a09b179..76471c17e 100644 --- a/src/ir/expressions/BinaryRelationExpression.h +++ b/src/ir/expressions/BinaryRelationExpression.h @@ -9,6 +9,7 @@ #define BINARYRELATIONEXPRESSION_H_ #include "src/ir/expressions/BinaryExpression.h" +#include namespace storm { @@ -29,6 +30,7 @@ public: } virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { + std::cout << "Cloning " << this->getLeft()->toString() << " ~ " << this->getRight()->toString() << std::endl; return std::shared_ptr(new BinaryRelationExpression(this->getLeft()->clone(renaming, bools, ints), this->getRight()->clone(renaming, bools, ints), this->relationType)); } diff --git a/src/ir/expressions/VariableExpression.h b/src/ir/expressions/VariableExpression.h index bbdb11ea6..b7160f929 100644 --- a/src/ir/expressions/VariableExpression.h +++ b/src/ir/expressions/VariableExpression.h @@ -34,6 +34,7 @@ public: } virtual std::shared_ptr clone(const std::map& renaming, const std::map& bools, const std::map& ints) { + std::cout << "Cloning VarExpr " << this->variableName << std::endl; if (renaming.count(this->variableName) > 0) { std::string newName = renaming.at(this->variableName); if (this->getType() == bool_) { diff --git a/src/parser/PrismParser.cpp b/src/parser/PrismParser.cpp index 948f32ad6..8930f251e 100644 --- a/src/parser/PrismParser.cpp +++ b/src/parser/PrismParser.cpp @@ -222,9 +222,9 @@ struct PrismParser::PrismGrammar : qi::grammar> freeIdentifierName [phoenix::clear(phoenix::ref(localBooleanVariables_)), phoenix::clear(phoenix::ref(localIntegerVariables_))] - >> freeIdentifierName >> *(variableDefinition(qi::_a, qi::_b, qi::_c, qi::_d)) > +commandDefinition > qi::lit("endmodule")) + >> *(variableDefinition(qi::_a, qi::_b, qi::_c, qi::_d)) >> +commandDefinition > qi::lit("endmodule")) [ phoenix::bind(moduleNames_.add, qi::_1, qi::_1), qi::_val = phoenix::construct(qi::_1, qi::_a, qi::_b, qi::_c, qi::_d, qi::_2), @@ -233,18 +233,18 @@ struct PrismParser::PrismGrammar : qi::grammar::*moduleFinder)(const std::string&) const = &qi::symbols::find; moduleDefinition.name("module"); - moduleRenaming = (qi::lit("module") - [phoenix::clear(phoenix::ref(localRenamings_))] - >> freeIdentifierName >> qi::lit("=") > moduleNames_ > qi::lit("[") > *( - (identifierName > qi::lit("=") > identifierName)[phoenix::insert(phoenix::ref(localRenamings_), phoenix::construct>(qi::_1, qi::_2))] + moduleRenaming = (qi::lit("module") >> freeIdentifierName >> qi::lit("=") + > moduleNames_ > qi::lit("[") > *( + (identifierName > qi::lit("=") > identifierName >> -qi::lit(","))[phoenix::insert(qi::_a, phoenix::construct>(qi::_1, qi::_2))] ) > qi::lit("]") > qi::lit("endmodule")) [ phoenix::bind(moduleNames_.add, qi::_1, qi::_1), - qi::_val = phoenix::construct(*phoenix::bind(moduleFinder, moduleMap_, qi::_2), qi::_1, localRenamings_, VariableAdder(this)), + qi::_val = phoenix::construct(*phoenix::bind(moduleFinder, moduleMap_, qi::_2), qi::_1, qi::_a, VariableAdder(this)), phoenix::bind(moduleMap_.add, qi::_1, qi::_val) ]; - moduleDefinitionList %= +moduleDefinition; + moduleRenaming.name("renamed module"); + moduleDefinitionList %= +(moduleDefinition | moduleRenaming); moduleDefinitionList.name("module list"); // This block defines all entities that are needed for parsing constant definitions. @@ -357,7 +357,7 @@ struct PrismParser::PrismGrammar : qi::grammar, std::vector, std::map, std::map>, Skipper> moduleDefinition; - qi::rule, std::vector, std::map, std::map>, Skipper> moduleRenaming; + qi::rule>, Skipper> moduleRenaming; // Rules for variable definitions. qi::rule&, std::vector&, std::map&, std::map&), Skipper> variableDefinition; @@ -512,8 +512,6 @@ struct PrismParser::PrismGrammar : qi::grammar> integerConstants_, booleanConstants_, doubleConstants_; struct qi::symbols moduleMap_; - std::map localRenamings_; - // A structure representing the identity function over identifier names. struct variableNamesStruct : qi::symbols { } integerVariableNames_, booleanVariableNames_, commandNames_, labelNames_, allConstantNames_, moduleNames_, localBooleanVariables_, localIntegerVariables_, assignedLocalBooleanVariables_, assignedLocalIntegerVariables_; @@ -575,6 +573,7 @@ std::shared_ptr PrismParser::parse(std::istream& inputStream result = std::shared_ptr(new storm::ir::Program()); // Second run. qi::phrase_parse(positionIteratorBegin2, positionIteratorEnd, grammar, boost::spirit::ascii::space | qi::lit("//") >> *(qi::char_ - qi::eol) >> qi::eol, *result); + std::cout << "Here is the parsed grammar: " << std::endl << result->toString() << std::endl; } catch(const qi::expectation_failure& e) { // If the parser expected content different than the one provided, display information // about the location of the error.