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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <math.h>
#include "Operator.hpp"
#include "Expression.hpp"
#include "MaxStrategy.hpp"
#include "EquationSystem.hpp"
#include "Complete.hpp"
extern "C" {
#include "EquationSystemParser.h"
#include "EquationSystemLexer.h"
}
using namespace std;
template<typename T>
std::pair<Expression<T>*, Expression<T>*> treeToExpression(pANTLR3_BASE_TREE node, EquationSystem<T>& system, bool negative=false) {
ANTLR3_UINT32 num = node->getChildCount(node);
string name = (char*) node->getText(node)->chars;
// leaf node - variable
if (num == 0) {
return std::pair<Expression<T>*, Expression<T>*>(
system.newExpression(system.newVariable(name + "_l")),
system.newExpression(system.newVariable(name + "_u")));
}
// a range itself
if (name == "RANGE") {
pANTLR3_BASE_TREE childNode;
string childName;
T firstValue, secondValue;
childNode = (pANTLR3_BASE_TREE) node->getChild(node, 0);
childName = (char*) childNode->getText(childNode)->chars;
stringstream firstStream(childName);
if (!(firstStream >> firstValue)) {
throw "Invalid number.";
}
if (negative) firstValue = -firstValue;
childNode = (pANTLR3_BASE_TREE) node->getChild(node, 1);
childName = (char*) childNode->getText(childNode)->chars;
stringstream secondStream(childName);
if (!(secondStream >> secondValue)) {
throw "Invalid number.";
}
if (negative) secondValue = -secondValue;
return std::pair<Expression<T>*, Expression<T>*>(
system.newExpression(new Constant<T>(firstValue)),
system.newExpression(new Constant<T>(secondValue)));
}
if (name == "-") {
negative = true;
}
// subexpressions
std::vector<Expression<T>*> firstArgs;
std::vector<Expression<T>*> secondArgs;
pANTLR3_BASE_TREE childNode;
for (unsigned int i = 0; i < num; ++i) {
childNode = (pANTLR3_BASE_TREE) node->getChild(node, i);
std::pair<Expression<T>*, Expression<T>*> expr = treeToExpression(childNode, system, negative);
firstArgs.push_back(expr.first);
secondArgs.push_back(expr.second);
}
// other operators
if (name == "-") {
//if (firstArgs.size() == 1) { // secondArgs.size() == firstArgs.size()
return std::pair<Expression<T>*, Expression<T>*>(
secondArgs[0],
firstArgs[0]);
}
if (name == "max") {
return std::pair<Expression<T>*, Expression<T>*>(
system.newMaxExpression(firstArgs),
system.newMaxExpression(secondArgs));
} else {
// we need two because expressions delete their operator
// bit of a silly design by me
Operator<T>* firstOp = NULL;
Operator<T>* secondOp = NULL;
if (name == "min") {
firstOp = new Minimum<T>();
secondOp = new Minimum<T>();
} else if (name == "+") {
firstOp = new Addition<T>();
secondOp = new Addition<T>();
} else if (name == ";") {
firstOp = new Comma<T>();
secondOp = new Comma<T>();
} else if (name == "GUARD") {
firstOp = new Guard<T>();
secondOp = new Guard<T>();
} else {
std::cerr << "Unknown leaf node type: " << name << std::endl;
throw "Unknown leaf node type";
}
return std::pair<Expression<T>*, Expression<T>*>(
system.newExpression(firstOp, firstArgs),
system.newExpression(secondOp, secondArgs));
}
}
template<typename T>
void treeToSystem(pANTLR3_BASE_TREE node, EquationSystem<T>& system) {
ANTLR3_UINT32 num = node->getChildCount(node);
if (num % 2 == 1)
throw "Big problem here.";
pANTLR3_BASE_TREE varNode;
pANTLR3_BASE_TREE exprNode;
for (unsigned int i = 0; i < num; i += 2) {
varNode = (pANTLR3_BASE_TREE) node->getChild(node, i);
exprNode = (pANTLR3_BASE_TREE) node->getChild(node, i+1);
Variable<T>* var;
vector<Expression<T>*> args;
string varName = (char*) varNode->getText(varNode)->chars;
std::pair<Expression<T>*, Expression<T>*> exprs = treeToExpression(exprNode, system);
var = system.newVariable(varName + "_l");
args.push_back(system.newExpression(new Constant<T>(-infinity<T>())));
args.push_back(exprs.first);
system[*var] = system.newMaxExpression(args);
args.clear();
var = system.newVariable(varName + "_u");
args.push_back(system.newExpression(new Constant<T>(-infinity<T>())));
args.push_back(exprs.second);
system[*var] = system.newMaxExpression(args);
}
}
typedef Complete<int> ZBar;
int main (int argc, char* argv[]) {
FixpointAlgorithm<ZBar>* algorithm = NULL;
if (argc > 2 && !strcmp(argv[2], "naive")) {
algorithm = new NaiveFixpoint<ZBar>();
cout << "Naive fixpoint" << endl;
} else {
algorithm = new RecursiveFixpoint<ZBar>();
cout << "Recursive fixpoint" << endl;
}
pANTLR3_INPUT_STREAM input;
pEquationSystemLexer lex;
pANTLR3_COMMON_TOKEN_STREAM tokens;
pEquationSystemParser parser;
input = antlr3FileStreamNew((pANTLR3_UINT8)argv[1], ANTLR3_ENC_8BIT);
lex = EquationSystemLexerNew(input);
tokens = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lex));
parser = EquationSystemParserNew(tokens);
EquationSystemParser_equation_system_return ret = parser -> equation_system(parser);
EquationSystem<ZBar> system;
treeToSystem<ZBar>(ret.tree, system);
// DO MORE HERE
VariableAssignment<ZBar> rho = system.minFixpoint(*algorithm);
std::cout << rho << std::endl;
const std::vector<Variable<ZBar>*> vars = system.vars();
for (unsigned int i = 0, size = vars.size(); i < size; ++i) {
cout << vars[i]->name() << " = " << rho[*vars[i]] << endl;
}
parser -> free(parser);
tokens -> free(tokens);
lex -> free(lex);
input -> close(input);
return 0;
}
|