/* * File: PrismGrammar.h * Author: nafur * * Created on April 30, 2013, 5:20 PM */ #ifndef PRISMGRAMMAR_H #define PRISMGRAMMAR_H // All classes of the intermediate representation are used. #include "src/ir/IR.h" #include "src/parser/prismparser/Includes.h" #include "src/parser/prismparser/Tokens.h" #include "src/parser/prismparser/IdentifierGrammars.h" #include "src/parser/prismparser/VariableState.h" #include "src/parser/prismparser/ConstBooleanExpressionGrammar.h" #include "src/parser/prismparser/ConstDoubleExpressionGrammar.h" #include "src/parser/prismparser/ConstIntegerExpressionGrammar.h" #include "src/parser/prismparser/BooleanExpressionGrammar.h" #include "src/parser/prismparser/IntegerExpressionGrammar.h" // Used for file input. #include #include namespace storm { namespace parser { namespace prism { using namespace storm::ir; using namespace storm::ir::expressions; /*! * The Boost spirit grammar for the PRISM language. Returns the intermediate representation of * the input that complies with the PRISM syntax. */ class PrismGrammar : public qi::grammar< Iterator, Program(), qi::locals< std::map>, std::map>, std::map>, std::map, std::map> >, Skipper> { public: /*! * Default constructor that creates an empty and functional grammar. */ PrismGrammar(); /*! * Puts all sub-grammars into the mode for performing the second run. A two-run model was chosen * because modules can involve variables that are only declared afterwards, so the first run * creates all variables and the second one tries to parse the full model. */ void prepareForSecondRun(); /*! * Resets all sub-grammars, i.e. puts them into an initial state. */ void resetGrammars(); private: std::shared_ptr state; struct qi::symbols moduleMap_; // The starting point of the grammar. qi::rule< Iterator, Program(), qi::locals< std::map>, std::map>, std::map>, std::map, std::map> >, Skipper> start; qi::rule modelTypeDefinition; qi::rule>&, std::map>&, std::map>&), Skipper> constantDefinitionList; qi::rule(), Skipper> moduleDefinitionList; // Rules for module definition. qi::rule, std::vector, std::map, std::map>, Skipper> moduleDefinition; qi::rule>, Skipper> moduleRenaming; // Rules for variable definitions. qi::rule&, std::vector&, std::map&, std::map&), Skipper> variableDefinition; qi::rule&, std::map&), qi::locals>, Skipper> booleanVariableDefinition; qi::rule&, std::map&), qi::locals>, Skipper> integerVariableDefinition; // Rules for command definitions. qi::rule, Skipper> commandDefinition; qi::rule(), Skipper> updateListDefinition; qi::rule, std::map>, Skipper> updateDefinition; qi::rule&, std::map&), Skipper> assignmentDefinitionList; qi::rule&, std::map&), Skipper> assignmentDefinition; // Rules for variable/command names. qi::rule commandName; qi::rule unassignedLocalBooleanVariableName; qi::rule unassignedLocalIntegerVariableName; // Rules for reward definitions. qi::rule&), Skipper> rewardDefinitionList; qi::rule&), qi::locals, std::vector>, Skipper> rewardDefinition; qi::rule stateRewardDefinition; qi::rule, Skipper> transitionRewardDefinition; // Rules for label definitions. qi::rule>&), Skipper> labelDefinitionList; qi::rule>&), Skipper> labelDefinition; // Rules for constant definitions. qi::rule(), Skipper> constantDefinition; qi::rule>&, std::map>&, std::map>&), Skipper> undefinedConstantDefinition; qi::rule(), Skipper> definedConstantDefinition; qi::rule>&), qi::locals>, Skipper> undefinedBooleanConstantDefinition; qi::rule>&), qi::locals>, Skipper> undefinedIntegerConstantDefinition; qi::rule>&), qi::locals>, Skipper> undefinedDoubleConstantDefinition; qi::rule(), Skipper> definedBooleanConstantDefinition; qi::rule(), Skipper> definedIntegerConstantDefinition; qi::rule(), Skipper> definedDoubleConstantDefinition; // Rules for variable recognition. qi::rule(), Skipper> booleanVariableCreatorExpression; qi::rule(), qi::locals>, Skipper> integerVariableCreatorExpression; storm::parser::prism::keywordsStruct keywords_; storm::parser::prism::modelTypeStruct modelType_; storm::parser::prism::relationalOperatorStruct relations_; /*! * Adds a constant of type integer with the given name and value. * * @param name The name of the constant. * @param value An expression definining the value of the constant. */ std::shared_ptr addIntegerConstant(std::string const& name, std::shared_ptr const& value); /*! * Adds a label with the given name and expression to the given label-to-expression map. * * @param name The name of the label. * @param expression The expression associated with the label. * @param nameToExpressionMap The map to which the label is added. */ void addLabel(std::string const& name, std::shared_ptr const& value, std::map>& nameToExpressionMap); /*! * Adds a boolean assignment for the given variable with the given expression and adds it to the * provided variable-to-assignment map. * * @param variable The name of the variable that the assignment targets. * @param expression The expression that is assigned to the variable. * @param variableToAssignmentMap The map to which the assignment is added. */ void addBooleanAssignment(std::string const& variable, std::shared_ptr const& expression, std::map& variableToAssignmentMap); /*! * Adds a boolean assignment for the given variable with the given expression and adds it to the * provided variable-to-assignment map. * * @param variable The name of the variable that the assignment targets. * @param expression The expression that is assigned to the variable. * @param variableToAssignmentMap The map to which the assignment is added. */ void addIntegerAssignment(std::string const& variable, std::shared_ptr const& value, std::map& variableToAssignmentMap); /*! * Creates a module by renaming, i.e. takes the module given by the old name, creates a new module * with the given name which renames all identifiers according to the given mapping. * * @param name The name of the new module. * @param oldName The name of the module that is to be copied (modulo renaming). * @param renaming A mapping from identifiers to their new names. */ Module renameModule(std::string const& name, std::string const& oldName, std::map& renaming); /*! * Creates a new module with the given name, boolean and integer variables and commands. * * @param name The name of the module to create. * @param booleanVariables The boolean variables of the module. * @param integerVariables The integer variables of the module. * @param booleanVariableToLocalIndexMap A mapping of boolean variables to module-local indices. * @param integerVariableToLocalIndexMap A mapping of boolean variables to module-local indices. * @param commands The commands associated with this module. */ Module createModule(std::string const& name, std::vector const& booleanVariables, std::vector const& integerVariables, std::map const& booleanVariableToLocalIndexMap, std::map const& integerVariableToLocalIndexMap, std::vector const& commands); /*! * Creates an integer variable with the given name, domain and initial value and adds it to the * provided list of integer variables and the given mappings. * * @param name The name of the integer variable. * @param lower The expression that defines the lower bound of the domain. * @param upper The expression that defines the upper bound of the domain. * @param init The expression that defines the initial value of the variable. * @param integerVariableToGlobalIndexMap A mapping of integer variables to global indices. */ void createIntegerVariable(std::string const& name, std::shared_ptr const& lower, std::shared_ptr const& upper, std::shared_ptr const& init, std::vector& integerVariables, std::map& integerVariableToGlobalIndexMap); /*! * Creates an boolean variable with the given name and initial value and adds it to the * provided list of boolean variables and the given mappings. * * @param name The name of the boolean variable. * @param init The expression that defines the initial value of the variable. * @param booleanVariableToGlobalIndexMap A mapping of boolean variables to global indices. */ void createBooleanVariable(std::string const& name, std::shared_ptr const& init, std::vector& booleanVariables, std::map& booleanVariableToGlobalIndexMap); }; } // namespace prism } // namespace parser } // namespace storm #endif /* PRISMGRAMMAR_H */