Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
FormulaParser.cpp
Go to the documentation of this file.
2
3#include <fstream>
4
6
8
10
11// If the parser fails due to ill-formed data, this exception is thrown.
13
15#include "storm/io/file.h"
18
19namespace storm {
20namespace parser {
21
22FormulaParser::FormulaParser() : manager(new storm::expressions::ExpressionManager()), grammar(new FormulaParserGrammar(manager)) {
23 // Intentionally left empty.
24}
25
26FormulaParser::FormulaParser(std::shared_ptr<storm::expressions::ExpressionManager const> const& manager)
27 : manager(manager), grammar(new FormulaParserGrammar(manager)) {
28 // Intentionally left empty.
29}
30
31FormulaParser::FormulaParser(std::shared_ptr<storm::expressions::ExpressionManager> const& manager)
32 : manager(manager), grammar(new FormulaParserGrammar(manager)) {
33 // Intentionally left empty.
34}
35
37 : manager(program.getManager().getSharedPointer()), grammar(new FormulaParserGrammar(program.getManager().getSharedPointer())) {}
38
40 : manager(program.getManager().getSharedPointer()), grammar(new FormulaParserGrammar(program.getManager().getSharedPointer())) {}
41
43 other.grammar->getIdentifiers().for_each(
44 [this](std::string const& name, storm::expressions::Expression const& expression) { this->addIdentifierExpression(name, expression); });
45}
46
48 this->manager = other.manager;
49 this->grammar = std::shared_ptr<FormulaParserGrammar>(new FormulaParserGrammar(this->manager));
50 other.grammar->getIdentifiers().for_each(
51 [=](std::string const& name, storm::expressions::Expression const& expression) { this->addIdentifierExpression(name, expression); });
52 return *this;
53}
54
55std::shared_ptr<storm::logic::Formula const> FormulaParser::parseSingleFormulaFromString(std::string const& formulaString) const {
56 std::vector<storm::jani::Property> property = parseFromString(formulaString);
57 STORM_LOG_THROW(property.size() == 1, storm::exceptions::WrongFormatException,
58 "Expected exactly one formula, but found " << property.size() << " instead.");
59 return property.front().getRawFormula();
60}
61
62std::vector<storm::jani::Property> FormulaParser::parseFromFile(std::string const& filename) const {
63 // Open file and initialize result.
64 std::ifstream inputFileStream;
65 storm::io::openFile(filename, inputFileStream);
66
67 std::vector<storm::jani::Property> properties;
68
69 // Now try to parse the contents of the file.
70 try {
71 std::string fileContent((std::istreambuf_iterator<char>(inputFileStream)), (std::istreambuf_iterator<char>()));
72 properties = parseFromString(fileContent);
73 } catch (std::exception& e) {
74 // In case of an exception properly close the file before passing exception.
75 storm::io::closeFile(inputFileStream);
76 throw e;
77 }
78
79 // Close the stream in case everything went smoothly and return result.
80 storm::io::closeFile(inputFileStream);
81 return properties;
82}
83
84std::vector<storm::jani::Property> FormulaParser::parseFromString(std::string const& formulaString) const {
85 PositionIteratorType first(formulaString.begin());
86 PositionIteratorType iter = first;
87 PositionIteratorType last(formulaString.end());
88
89 // Create empty result;
90 std::vector<storm::jani::Property> result;
91
92 // Create grammar.
93 try {
94 // Start parsing.
95 bool succeeded = qi::phrase_parse(
96 iter, last, *grammar, storm::spirit_encoding::space_type() | qi::lit("//") >> *(qi::char_ - (qi::eol | qi::eoi)) >> (qi::eol | qi::eoi), result);
97 STORM_LOG_THROW(succeeded, storm::exceptions::WrongFormatException, "Could not parse formula: " << formulaString);
98 STORM_LOG_DEBUG("Parsed formula successfully.");
99 } catch (qi::expectation_failure<PositionIteratorType> const& e) {
100 STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, e.what_);
101 }
102
103 return result;
104}
105
106void FormulaParser::addIdentifierExpression(std::string const& identifier, storm::expressions::Expression const& expression) {
107 // Record the mapping and hand it over to the grammar.
108 grammar->addIdentifierExpression(identifier, expression);
109}
110
111} // namespace parser
112} // namespace storm
boost::spirit::line_pos_iterator< BaseIteratorType > PositionIteratorType
std::vector< storm::jani::Property > parseFromString(std::string const &propertyString) const
Parses the property given by the provided string.
void addIdentifierExpression(std::string const &identifier, storm::expressions::Expression const &expression)
Adds an identifier and the expression it is supposed to be replaced with.
FormulaParser & operator=(FormulaParser const &other)
std::vector< storm::jani::Property > parseFromFile(std::string const &filename) const
Parses the properties in the given file.
std::shared_ptr< storm::logic::Formula const > parseSingleFormulaFromString(std::string const &formulaString) const
Parses the formula given by the provided string.
#define STORM_LOG_DEBUG(message)
Definition logging.h:23
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void closeFile(std::ofstream &stream)
Close the given file after writing.
Definition file.h:47
void openFile(std::string const &filepath, std::ofstream &filestream, bool append=false, bool silent=false)
Open the given file for writing.
Definition file.h:18
LabParser.cpp.
Definition cli.cpp:18