-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cpp
107 lines (92 loc) · 2.51 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Parser.hpp"
#include "Automata.hpp"
#include <iostream>
#include <sstream>
#include <string>
#include <exception>
using namespace std;
int linenumber=0;
istream& getfline(istream& is,string& str, char delim='\n')
{
do {
is.peek();
getline(is,str,delim);
if(delim!=' ')
linenumber++;
} while(str.empty()||str[0]=='%');
return is;
}
int lttoi(string s)
{
if(!s.compare("_"))
return 0;
int res;
try {
res = stoi(s)+1;
}
catch(exception& e) {
cerr << "Syntax error at line: " << linenumber << endl << "Exception: " << string(e.what()) << endl;
exit(-1);
}
return res;
}
ExplicitAutomaton* Parser::parseFile(std::istream &file)
{
string line;
getfline(file,line);
int size = stoi(line);
int type;
getfline(file,line);
if(!line.compare("p"))
type=PROB;
else {
if (!line.compare("c"))
type=CLASSICAL;
else
type=stoi(line);
}
string alphabet;
getfline(file,alphabet);
ExplicitAutomaton* ret = new ExplicitAutomaton(size,alphabet.length());
ret->matrices.clear();//backward compatibility
ret->type = type;
ret->alphabet = alphabet;
getfline(file,line);
try
{
ret->initialState = stoi(line);
// cout << "Initial state: " << stoi(line) << endl;
}
catch(const exception & exc){ throw runtime_error("Error while parsing initial states, could not parse '" + line + "' to integer"); }
ret->finalStates.clear();
getfline(file,line);
istringstream iss(line);
while(getfline(iss,line,' ')) {
try
{
ret->finalStates.push_back(stoi(line));
}
catch(const exception & exc)
{
throw runtime_error("Error while parsing final states, could not parse line" + line + " to integer");
}
}
for(int i=0;i<alphabet.length();i++) {
ExplicitMatrix mat(size);
string lt;
getfline(file,lt);
for(int j=0;j<size;j++) {
getfline(file,line);
istringstream iss2(line);
for(int k=0;k<size;k++) {
getfline(iss2,line,' ');
if(type <= 0)
mat.coefficients[j][k]=lttoi(line);
else
mat.coefficients[j][k]=MultiCounterAut::coef_to_char(line,type);
}
}
ret->matrices.push_back(mat);
}
return ret;
}