-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
117 lines (97 loc) · 2.76 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
108
109
110
111
112
113
114
115
116
117
#include "parser.h"
using namespace std;
void fatal(const char* msg) {
cerr << msg << endl;
exit(1);
}
inline void parseBasicRule(Program &p, istream &in) {
Literal head;
int bodySize, negativeBodySize;
in >> head >> bodySize >> negativeBodySize;
vector<Literal> negativeBody(negativeBodySize), positiveBody(bodySize - negativeBodySize);
for (int i = 0; i < negativeBodySize; i++)
in >> negativeBody[i];
for (int i = 0; i < bodySize - negativeBodySize; i++)
in >> positiveBody[i];
p.addRule(make_shared<BasicRule>(head, positiveBody, negativeBody));
}
inline void parseChoiceRule(Program &p, istream &in) {
int headSize, bodySize, negativeBodySize;
in >> headSize;
vector<Literal> headVec(headSize);
for (int i = 0; i < headSize; i++)
in >> headVec[i];
in >> bodySize >> negativeBodySize;
vector<Literal> negativeBody(negativeBodySize), positiveBody(bodySize - negativeBodySize);
for (int i = 0; i < negativeBodySize; i++)
in >> negativeBody[i];
for (int i = 0; i < bodySize - negativeBodySize; i++)
in >> positiveBody[i];
p.addRule(make_shared<ChoiceRule>(headVec, positiveBody, negativeBody));
}
inline void parseRules(Program &p, istream &in) {
int ruleType;
in >> ruleType;
while (ruleType != 0) {
switch (ruleType) {
case 1:
parseBasicRule(p, in);
break;
case 3:
parseChoiceRule(p, in);
break;
default:
fatal("unsupported rule type");
}
in >> ruleType;
}
}
inline void parseLiteralNames(Program &p, istream &in) {
Literal l;
in >> l;
while (l != 0) {
string name;
in >> name;
p.nameLiteral(l, name);
in >> l;
}
}
inline void parsePositiveTargets(Program &p, istream &in) {
string token;
Literal l;
in >> token;
if (token != "B+")
fatal("Parser error: expected B+");
in >> l;
while (l != 0) {
p.positiveTargets.push_back(l);
in >> l;
}
}
inline void parseNegativeTargets(Program &p, istream &in) {
string token;
Literal l;
in >> token;
if (token != "B-")
fatal("Parser error: expected B-");
in >> l;
while (l != 0) {
p.negativeTargets.push_back(l);
in >> l;
}
}
void parseGroundedASP(Program &p, istream &in) {
parseRules(p, in);
parseLiteralNames(p, in);
parsePositiveTargets(p, in);
parseNegativeTargets(p, in);
}
void parseProbabilities(Program &p, istream &in) {
while (in.good()) {
string literal;
double probability;
in >> literal >> probability;
if (literal != "")
p.assignProbability(literal, probability);
}
}