1
Fork 0
mirror of https://github.com/Jaxan/hybrid-ads.git synced 2025-04-27 23:17:44 +02:00

Rewrote the parser to accept a bit more (and to be read easier as well)

This commit is contained in:
Joshua Moerman 2015-04-29 15:39:13 +02:00
parent 22208275fd
commit 432540f148

View file

@ -1,18 +1,16 @@
#include "read_mealy_from_dot.hpp" #include "read_mealy_from_dot.hpp"
#include "mealy.hpp" #include "mealy.hpp"
#include <boost/algorithm/string/trim.hpp>
#include <fstream> #include <fstream>
#include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
using namespace std; using namespace std;
template <typename T> static string easy_substr(string const & s, size_t begin, size_t end){
T get(istream& in){ return s.substr(begin, end - begin);
T t;
in >> t;
return t;
} }
mealy read_mealy_from_dot(std::istream & in, translation & t){ mealy read_mealy_from_dot(std::istream & in, translation & t){
@ -22,30 +20,28 @@ mealy read_mealy_from_dot(std::istream & in, translation & t){
state max_state = 0; state max_state = 0;
string line; string line;
stringstream ss;
while(getline(in, line)){ while(getline(in, line)){
using boost::algorithm::trim_copy;
const auto npos = std::string::npos;
if(line.find("}") != string::npos) break; if(line.find("}") != string::npos) break;
const auto i = line.find("->"); // parse states
if(i == string::npos) continue; const auto arrow_pos = line.find("->");
const auto bracket_pos = line.find('[');
if(arrow_pos == npos || bracket_pos == npos) continue;
// get from and to state const auto lh = trim_copy(easy_substr(line, 0, arrow_pos));
ss.str(line); const auto rh = trim_copy(easy_substr(line, arrow_pos+2, bracket_pos));
ss.seekg(0);
const auto lh = get<string>(ss);
const auto arrow = get<string>(ss);
const auto rh = get<string>(ss);
// get label // parse input/output
const auto l1 = line.find('\"'); const auto quote1_pos = line.find('\"', bracket_pos);
const auto l2 = line.find('\"', l1+1); const auto slash_pos = line.find('/', quote1_pos);
if(l1 == string::npos || l2 == string::npos) continue; const auto quote2_pos = line.find('\"', slash_pos);
ss.str(line.substr(l1+1, l2-(l1+1))); if(quote1_pos == npos || slash_pos == npos || quote2_pos == npos) continue;
ss.seekg(0);
const auto input = get<string>(ss); const auto input = trim_copy(easy_substr(line, quote1_pos+1, slash_pos));
const auto slash = get<string>(ss); const auto output = trim_copy(easy_substr(line, slash_pos+1, quote2_pos));
const auto output = get<string>(ss);
// make fresh indices, if needed // make fresh indices, if needed
if(state_indices.count(lh) < 1) state_indices[lh] = max_state++; if(state_indices.count(lh) < 1) state_indices[lh] = max_state++;