Commit 1fd76ee9 authored by Amaury Fauchille's avatar Amaury Fauchille Committed by Alexandre Duret-Lutz
Browse files

word: implement twa word parsing

* spot/twaalgos/word.hh: add parse_word method and a new constructor
* spot/twaalgos/word.cc: implement word parsing
* python/spot/__init__.py: add parse_word method binding
* tests/python/word.ipynb: add word parsing tests
parent 61b1f200
...@@ -860,3 +860,7 @@ def sat_minimize(aut, acc=None, colored=False, ...@@ -860,3 +860,7 @@ def sat_minimize(aut, acc=None, colored=False,
args += ',dichotomy'; args += ',dichotomy';
from spot.impl import sat_minimize as sm from spot.impl import sat_minimize as sm
return sm(aut, args, state_based) return sm(aut, args, state_based)
def parse_word(word, dic=_bdd_dict):
from spot.impl import parse_word as pw
return pw(word, dic)
// -*- coding: utf-8 -*- // -*- coding: utf-8 -*-
// Copyright (C) 2013, 2014, 2015 Laboratoire de Recherche et Développement // Copyright (C) 2013, 2014, 2015, 2016 Laboratoire de Recherche et
// de l'Epita (LRDE). // Développement de l'Epita (LRDE).
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
// //
...@@ -20,10 +20,13 @@ ...@@ -20,10 +20,13 @@
#include <spot/twaalgos/word.hh> #include <spot/twaalgos/word.hh>
#include <spot/twa/bddprint.hh> #include <spot/twa/bddprint.hh>
#include <spot/twa/bdddict.hh> #include <spot/twa/bdddict.hh>
#include <spot/tl/parse.hh>
#include <spot/tl/simplify.hh>
#include <spot/tl/apcollect.hh>
namespace spot namespace spot
{ {
twa_word::twa_word(const twa_run_ptr run) twa_word::twa_word(const twa_run_ptr& run)
: dict_(run->aut->get_dict()) : dict_(run->aut->get_dict())
{ {
for (auto& i: run->prefix) for (auto& i: run->prefix)
...@@ -33,6 +36,11 @@ namespace spot ...@@ -33,6 +36,11 @@ namespace spot
dict_->register_all_variables_of(run->aut, this); dict_->register_all_variables_of(run->aut, this);
} }
twa_word::twa_word(const bdd_dict_ptr& dict)
: dict_(dict)
{
}
void void
twa_word::simplify() twa_word::simplify()
{ {
...@@ -105,4 +113,143 @@ namespace spot ...@@ -105,4 +113,143 @@ namespace spot
os << '}'; os << '}';
return os; return os;
} }
namespace
{
static void word_parse_error(const std::string& word,
size_t i, parse_error_list pel)
{
for (auto& err: pel)
{
err.first.begin.column += i;
err.first.end.column += i;
}
std::ostringstream os;
format_parse_errors(os, word, pel);
throw parse_error(os.str());
}
static void word_parse_error(const std::string& word, size_t i,
const std::string& message)
{
if (i == std::string::npos)
i = word.size();
std::ostringstream s;
s << ">>> " << word << '\n';
for (auto j = i + 4; j > 0; --j)
s << ' ';
s << '^' << '\n';
s << message << '\n';
throw parse_error(s.str());
}
static size_t skip_next_formula(const std::string& word, size_t begin)
{
bool quoted = false;
auto size = word.size();
for (auto j = begin; j < size; ++j)
{
auto c = word[j];
if (!quoted && (c == ';' || c == '}'))
return j;
if (c == '"')
quoted = !quoted;
else if (quoted && c == '\\')
++j;
}
if (quoted)
word_parse_error(word, word.size(), "Unclosed string");
return std::string::npos;
}
}
twa_word_ptr parse_word(const std::string& word, const bdd_dict_ptr& dict)
{
atomic_prop_set aps;
parse_error_list pel;
tl_simplifier tls(dict);
twa_word_ptr tw = make_twa_word(dict);
size_t i = 0;
auto ind = i;
auto extract_bdd =
[&](typename twa_word::seq_t& seq)
{
auto sub = word.substr(i, ind - i);
formula f = spot::parse_infix_boolean(sub, pel);
if (!pel.empty())
word_parse_error(word, i, pel);
atomic_prop_collect(f, &aps);
seq.push_back(tls.as_bdd(f));
if (word[ind] == '}')
return true;
// Skip blanks after semi-colon
i = word.find_first_not_of(' ', ind + 1);
return false;
};
// Parse the prefix part. Can be empty.
while (word.substr(i, 6) != std::string("cycle{"))
{
ind = skip_next_formula(word, i);
if (ind == std::string::npos)
word_parse_error(word, word.size(),
"A twa_word must contain a cycle");
if (word[ind] == '}')
word_parse_error(word, ind, "Expected ';' delimiter :"
"'}' stands for ending a cycle");
// Exract formula, convert it to bdd and add it to the prefix sequence
extract_bdd(tw->prefix);
if (i == std::string::npos)
word_parse_error(word, ind + 1, "Missing cycle in formula");
}
// Consume "cycle{"
i += 6;
while (true)
{
ind = skip_next_formula(word, i);
if (ind == std::string::npos)
word_parse_error(word, word.size(),
"Missing ';' or '}' after formula");
// Extract formula, convert it to bdd and add it to the cycle sequence
// Break if an '}' is encountered
if (extract_bdd(tw->cycle))
break;
if (i == std::string::npos)
word_parse_error(word, ind + 1,
"Missing end of cycle character : '}'");
}
if (ind != word.size() - 1)
word_parse_error(word, ind + 1, "Input should be finished after cycle");
for (auto ap: aps)
dict->register_proposition(ap, tw.get());
return tw;
}
twa_graph_ptr twa_word::as_automaton() const
{
twa_graph_ptr aut = make_twa_graph(dict_);
aut->prop_weak(true);
aut->prop_deterministic(true);
size_t i = 0;
aut->new_states(prefix.size() + cycle.size());
for (auto b: prefix)
{
aut->new_edge(i, i + 1, b);
++i;
}
size_t j = i;
auto b = cycle.begin();
auto end = --cycle.end();
for (; b != end; ++b)
{
aut->new_edge(i, i + 1, *b);
++i;
}
// Close the loop
aut->new_edge(i, j, *b);
return aut;
}
} }
// -*- coding: utf-8 -*- // -*- coding: utf-8 -*-
// Copyright (C) 2013, 2014, 2015 Laboratoire de Recherche et // Copyright (C) 2013, 2014, 2015, 2016 Laboratoire de Recherche et
// Développement de l'Epita (LRDE). // Développement de l'Epita (LRDE).
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
...@@ -28,7 +28,8 @@ namespace spot ...@@ -28,7 +28,8 @@ namespace spot
/// \brief An infinite word stored as a lasso. /// \brief An infinite word stored as a lasso.
struct SPOT_API twa_word final struct SPOT_API twa_word final
{ {
twa_word(const twa_run_ptr run); twa_word(const bdd_dict_ptr& dict);
twa_word(const twa_run_ptr& run);
~twa_word() ~twa_word()
{ {
dict_->unregister_all_my_variables(this); dict_->unregister_all_my_variables(this);
...@@ -45,10 +46,26 @@ namespace spot ...@@ -45,10 +46,26 @@ namespace spot
return dict_; return dict_;
} }
twa_graph_ptr as_automaton() const;
SPOT_API SPOT_API
friend std::ostream& operator<<(std::ostream& os, const twa_word& w); friend std::ostream& operator<<(std::ostream& os, const twa_word& w);
private: private:
bdd_dict_ptr dict_; bdd_dict_ptr dict_;
}; };
typedef std::shared_ptr<twa_word> twa_word_ptr;
inline twa_word_ptr make_twa_word(const bdd_dict_ptr& dict)
{
return std::make_shared<twa_word>(dict);
}
inline twa_word_ptr make_twa_word(const twa_run_ptr& run)
{
return std::make_shared<twa_word>(run);
}
SPOT_API
twa_word_ptr parse_word(const std::string& word, const bdd_dict_ptr& dict);
} }
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:e975b9a64657f38248c434a89f29b28f0bf90f9c40b7e8afdd7459734cbdcd38" "signature": "sha256:6461a5fa3f321dac24ae47d88c90f911822e003b658d05d4ef7c102ff64e058e"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
...@@ -139,7 +139,7 @@ ...@@ -139,7 +139,7 @@
"</svg>\n" "</svg>\n"
], ],
"text": [ "text": [
"<spot.impl.twa_graph; proxy of <Swig Object of type 'std::shared_ptr< spot::twa_graph > *' at 0x7f4f0c2e0810> >" "<spot.impl.twa_graph; proxy of <Swig Object of type 'std::shared_ptr< spot::twa_graph > *' at 0x7fdd0c5d9330> >"
] ]
} }
], ],
...@@ -294,6 +294,224 @@ ...@@ -294,6 +294,224 @@
} }
], ],
"prompt_number": 7 "prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(spot.parse_word('a; b; cycle{a&b}'))\n",
"print(spot.parse_word('cycle{a&bb|bac&(aaa|bbb)}'))\n",
"print(spot.parse_word('a; b;b; qiwuei;\"a;b&c;a\" ;cycle{a}'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a; b; cycle{a & b}\n",
"cycle{(a & bb) | (aaa & bac) | (bac & bbb)}\n",
"a; b; b; qiwuei; \"a;b&c;a\"; cycle{a}\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(spot.parse_word('a; b&!a; b'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "\n>>> a; b&!a; b\n ^\nA twa_word must contain a cycle\n (<string>)",
"output_type": "pyerr",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<string>\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; b&!a; b\n ^\nA twa_word must contain a cycle\n\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(spot.parse_word('a; b; c}'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "\n>>> a; b; c}\n ^\nExpected ';' delimiter :'}' stands for ending a cycle\n (<string>)",
"output_type": "pyerr",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<string>\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; b; c}\n ^\nExpected ';' delimiter :'}' stands for ending a cycle\n\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(spot.parse_word('a; cycle{}'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "\n>>> a; cycle{}\n ^\nempty input\n\n (<string>)",
"output_type": "pyerr",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<string>\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; cycle{}\n ^\nempty input\n\n\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(spot.parse_word('a; cycle{!a}; a'))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "\n>>> a; cycle{!a}; a\n ^\nInput should be finished after cycle\n (<string>)",
"output_type": "pyerr",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<string>\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m \n>>> a; cycle{!a}; a\n ^\nInput should be finished after cycle\n\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w1 = spot.parse_word('a; !a; cycle{a; !a; a}')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w1.as_automaton()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"svg": [
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
" -->\n",
"<!-- Title: G Pages: 1 -->\n",
"<svg width=\"406pt\" height=\"74pt\"\n",
" viewBox=\"0.00 0.00 406.00 74.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 70)\">\n",
"<title>G</title>\n",
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-70 402,-70 402,4 -4,4\"/>\n",
"<!-- I -->\n",
"<!-- 0 -->\n",
"<g id=\"node2\" class=\"node\"><title>0</title>\n",
"<ellipse fill=\"#ffffaa\" stroke=\"black\" cx=\"56\" cy=\"-18\" rx=\"18\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"56\" y=\"-14.3\" font-family=\"Lato\" font-size=\"14.00\">0</text>\n",
"</g>\n",
"<!-- I&#45;&gt;0 -->\n",
"<g id=\"edge1\" class=\"edge\"><title>I&#45;&gt;0</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M1.15491,-18C2.79388,-18 17.1543,-18 30.6317,-18\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"37.9419,-18 30.9419,-21.1501 34.4419,-18 30.9419,-18.0001 30.9419,-18.0001 30.9419,-18.0001 34.4419,-18 30.9418,-14.8501 37.9419,-18 37.9419,-18\"/>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g id=\"node3\" class=\"node\"><title>1</title>\n",
"<ellipse fill=\"#ffffaa\" stroke=\"black\" cx=\"135\" cy=\"-18\" rx=\"18\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"135\" y=\"-14.3\" font-family=\"Lato\" font-size=\"14.00\">1</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g id=\"edge2\" class=\"edge\"><title>0&#45;&gt;1</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M74.0888,-18C84.5562,-18 98.1196,-18 109.693,-18\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"116.959,-18 109.959,-21.1501 113.459,-18 109.959,-18.0001 109.959,-18.0001 109.959,-18.0001 113.459,-18 109.959,-14.8501 116.959,-18 116.959,-18\"/>\n",
"<text text-anchor=\"start\" x=\"92\" y=\"-21.8\" font-family=\"Lato\" font-size=\"14.00\">a</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g id=\"node4\" class=\"node\"><title>2</title>\n",
"<ellipse fill=\"#ffffaa\" stroke=\"black\" cx=\"218\" cy=\"-18\" rx=\"18\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"218\" y=\"-14.3\" font-family=\"Lato\" font-size=\"14.00\">2</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g id=\"edge3\" class=\"edge\"><title>1&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M153.178,-18C164.669,-18 179.959,-18 192.693,-18\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"199.847,-18 192.847,-21.1501 196.347,-18 192.847,-18.0001 192.847,-18.0001 192.847,-18.0001 196.347,-18 192.847,-14.8501 199.847,-18 199.847,-18\"/>\n",
"<text text-anchor=\"start\" x=\"171\" y=\"-21.8\" font-family=\"Lato\" font-size=\"14.00\">!a</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g id=\"node5\" class=\"node\"><title>3</title>\n",
"<ellipse fill=\"#ffffaa\" stroke=\"black\" cx=\"297\" cy=\"-48\" rx=\"18\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"297\" y=\"-44.3\" font-family=\"Lato\" font-size=\"14.00\">3</text>\n",
"</g>\n",
"<!-- 2&#45;&gt;3 -->\n",
"<g id=\"edge4\" class=\"edge\"><title>2&#45;&gt;3</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M234.954,-24.216C246.105,-28.5603 261.15,-34.4223 273.515,-39.2395\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"280.066,-41.7919 272.4,-42.1858 276.805,-40.5213 273.544,-39.2507 273.544,-39.2507 273.544,-39.2507 276.805,-40.5213 274.687,-36.3156 280.066,-41.7919 280.066,-41.7919\"/>\n",
"<text text-anchor=\"start\" x=\"254\" y=\"-36.8\" font-family=\"Lato\" font-size=\"14.00\">a</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g id=\"node6\" class=\"node\"><title>4</title>\n",
"<ellipse fill=\"#ffffaa\" stroke=\"black\" cx=\"380\" cy=\"-18\" rx=\"18\" ry=\"18\"/>\n",
"<text text-anchor=\"middle\" x=\"380\" y=\"-14.3\" font-family=\"Lato\" font-size=\"14.00\">4</text>\n",
"</g>\n",
"<!-- 3&#45;&gt;4 -->\n",
"<g id=\"edge5\" class=\"edge\"><title>3&#45;&gt;4</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M314.003,-42.073C326.017,-37.6233 342.638,-31.4675 356.064,-26.4947\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"362.753,-24.0174 357.283,-29.4026 359.471,-25.233 356.189,-26.4487 356.189,-26.4487 356.189,-26.4487 359.471,-25.233 355.095,-23.4948 362.753,-24.0174 362.753,-24.0174\"/>\n",
"<text text-anchor=\"start\" x=\"333\" y=\"-37.8\" font-family=\"Lato\" font-size=\"14.00\">!a</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;2 -->\n",
"<g id=\"edge6\" class=\"edge\"><title>4&#45;&gt;2</title>\n",
"<path fill=\"none\" stroke=\"black\" d=\"M362.276,-13.9842C349.433,-11.115 331.209,-7.50646 315,-6 299.069,-4.51934 294.922,-4.4234 279,-6 266.86,-7.20214 253.537,-9.74069 242.536,-12.1747\"/>\n",
"<polygon fill=\"black\" stroke=\"black\" points=\"235.677,-13.744 241.798,-9.11188 239.089,-12.9632 242.501,-12.1825 242.501,-12.1825 242.501,-12.1825 239.089,-12.9632 243.204,-15.2532 235.677,-13.744 235.677,-13.744\"/>\n",
"<text text-anchor=\"start\" x=\"293.5\" y=\"-9.8\" font-family=\"Lato\" font-size=\"14.00\">a</text>\n",
"</g>\n",
"</g>\n",
"</svg>\n"
],
"text": [
"<spot.impl.twa_graph; proxy of <Swig Object of type 'std::shared_ptr< spot::twa_graph > *' at 0x7fdd0c5d96c0> >"
]
}
],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
} }
], ],
"metadata": {} "metadata": {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment