-
Notifications
You must be signed in to change notification settings - Fork 0
/
stic.y
466 lines (414 loc) · 9.6 KB
/
stic.y
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* SRML to ISPL compiler */
/* */
%{
#include "heading.h"
int yyerror(char *s);
int yylex(void);
extern FILE *coba;
extern FILE *comb;
extern vector<string> rpn;
extern bool cgs_flag;
//bool cgs_flag=false;
env_ag env_ag;
goals goals;
vector<agent> ag;
string temp_ag;
vector<string> temp_var;
//ACTION STRUCT BUFFER
vector<action> vec_ac_temp;
//initstates struct buffer
vector<init> initStates_buff;
//protocol struct buffer
vector<protocol> agPro_buff;
//vector<update_act> rpn_ac;
vector<string> rpn_ac;
//update struct buffer
vector<update> update_buff;
//env evo buffer
env_evo seed_buff;
//LTL goal formulae
vector<string> ltl_goal;
stack<string> ltl_stack;
vector<string> environment_vars;
unsigned int ag_count=0;
unsigned int var_count=0;
unsigned int up_count=0;
unsigned int init_count=0;
unsigned int af_count=0;
string init_temp;
stack<bool> bvr;
vector<string>obsvars_enum;
void print_env();
int print_ag(vector<agent>& ag, unsigned int ag_count,unsigned int var_count);
bool formula_eval(vector<string>& rpn);
void guard_checker(vector<agent>& ag, int ag_count, int var_count);
int check_locality(vector<agent>& ag, int ag_count);
void find_conac();
string build_ltl(vector <string>& ltl_goal);
string evoEnv_builder(vector<agent>& ag, unsigned int ag_count,unsigned int var_count);
int insert_act(vector<agent>& ag, int ag_count);
int insert_obsvar(vector<agent>& ag, unsigned int ag_count,unsigned int var_count);
%}
%union{
int int_val;
string *op_val;
char *sval;
bool boolean;
}
%start input
%token <sval> MODULE
%token <sval> ISI
%type <sval> id_y
%type <sval> var_y
%type <sval> init_y
%type <sval> update_y
%type <sval> init_command
%type <sval> update_command
%type <sval> init_condition
%type <sval> update_condition
%type <sval> init_next_state
%type <sval> update_next_state
%type <sval> formula_assign
%type <sval> formula_guard
%token <sval> CONTROL
%token <sval> VAR
%token <sval> INIT
%token <sval> UPDATE
%token <sval> END
%token <sval> COMMA
%token <sval> DCOL
%token <sval> ASSIGN
%token <sval> TRUE
%token <sval> FALSE
%token <sval> EQUALS
%token <sval> NEXT
%token <sval> SEMICOLON
%token <sval> GOAL
%left <sval> OR
%left <sval> AND
%token <sval> IMPLIES
%token <sval> IFF
%token <sval> LB RB
%right <sval> NOT
%token <sval> LTL
%token <sval> G
%token <sval> F
%token <sval> X
%token <sval> U
%token <sval> ENVIRONMENT
%token <sval> PROPF
%token <sval> SL
%type <sval> goal_form
%type <sval> gf
%%
input: MODULE id_y CONTROL var_y INIT init_y UPDATE update_y GOAL goal_form {
ag.push_back(agent());
ag[ag_count].agName = temp_ag;
unsigned int i;
for(i=0;i<var_count;i++)
{
ag[ag_count].varName.push_back($2);
ag[ag_count].varName[i]=temp_var[i];
ag[ag_count].varpro.push_back(temp_var[i]);
}
//check locality of vars
//check_locality(ag, ag_count);
//com_pro(ag, var_count, ag_count);
//com(ag, var_count, ag_count);
ag[ag_count].initStates = initStates_buff;
ag[ag_count].ag_up = update_buff;
//print_ag(ag,ag_count,var_count);
//cout << "A" << endl;
//cout << cgs_flag << endl;
if(!cgs_flag)
{
print_ag(ag,ag_count,var_count);
for (unsigned int j=0; j<ag[ag_count].ag_up.size();j++)
{
reverse(ag[ag_count].ag_up[j].guard_formula.begin(),ag[ag_count].ag_up[j].guard_formula.end());
}
}
vec_ac_temp.clear();
initStates_buff.clear();
update_buff.clear();
ag_count++;
init_count=0;
var_count=0;
//cout << "\nUPCOUNT: " << up_count;
up_count=0;
}
| MODULE id_y CONTROL var_y INIT init_y UPDATE update_y{
ag.push_back(agent());
ag[ag_count].agName = temp_ag;
//cgs_flag = true;
//cout << cgs_flag << endl;
unsigned int i;
for(i=0;i<var_count;i++)
{
ag[ag_count].varName.push_back($2);
ag[ag_count].varName[i]=temp_var[i];
environment_vars.push_back(temp_var[i]);
ag[ag_count].varpro.push_back(temp_var[i]);
}
//com_pro(ag, var_count, ag_count);
//com(ag, var_count, ag_count);
//cout << "B" << endl;
if(cgs_flag)
for (unsigned int g=0; g<ag_count; g++)
{ //cout << cgs_flag << endl;
print_ag(ag,g,ag[g].varName.size());
for (unsigned int j=0; j<ag[ag_count].ag_up.size();j++)
{
reverse(ag[ag_count].ag_up[j].guard_formula.begin(),ag[ag_count].ag_up[j].guard_formula.end());
}
}
//cout << ag_count;
//print_ag(ag,0,1);
//print_ag(ag,1,1);
ag[ag_count].initStates = initStates_buff;
ag[ag_count].ag_up = update_buff;
print_ag(ag,ag_count,var_count);
for (unsigned int j=0; j<ag[ag_count].ag_up.size();j++)
{
reverse(ag[ag_count].ag_up[j].guard_formula.begin(),ag[ag_count].ag_up[j].guard_formula.end());
}
vec_ac_temp.clear();
initStates_buff.clear();
update_buff.clear();
ag_count++;
init_count=0;
var_count=0;
//cout << "\nUPCOUNT: " << up_count;
up_count=0;
}
| input input{
}
| input PROPF goal_form
;
var_y: ISI {
temp_var.push_back($1);
temp_var[var_count] = $1;
var_count++;
//cout << $1 << "\n";
}
| var_y COMMA var_y {
}
;
id_y:ISI {temp_ag = $1;}
;
init_y: DCOL init_command
| init_y DCOL init_command
;
init_command: init_condition ASSIGN init_next_state
;
update_command: update_condition ASSIGN update_next_state {
update_buff.push_back(update());
}
;
init_condition: TRUE {}
| FALSE
;
formula_guard: ISI {
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back($1);
}
| TRUE
{
{
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back("true");
}
}
| FALSE
{
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back("false");
}
| formula_guard OR formula_guard {
if(update_buff.empty())
update_buff.push_back(update()); update_buff[up_count].guard_formula.push_back("or");}
| formula_guard AND formula_guard {
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back("and");
}
| formula_guard IMPLIES formula_guard {
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back("implies");}
| formula_guard IFF formula_guard {
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back("iff");}
| NOT formula_guard {
if(update_buff.empty())
update_buff.push_back(update());
update_buff[up_count].guard_formula.push_back("not");}
| LB formula_guard RB
;
formula_assign: ISI
{
if(update_buff[up_count].act_formula.empty())
update_buff[up_count].act_formula.push_back(update_act());
update_buff[up_count].act_formula[af_count].assign_formula.push_back($1);
}
| TRUE
{
if(update_buff[up_count].act_formula.empty())
update_buff[up_count].act_formula.push_back(update_act());
update_buff[up_count].act_formula[af_count].assign_formula.push_back("true");
}
| FALSE
{
if(update_buff[up_count].act_formula.empty())
update_buff[up_count].act_formula.push_back(update_act());
update_buff[up_count].act_formula[af_count].assign_formula.push_back("false");
}
| formula_assign OR formula_assign
{
update_buff[up_count].act_formula[af_count].assign_formula.push_back("or");
}
| formula_assign AND formula_assign
{
update_buff[up_count].act_formula[af_count].assign_formula.push_back("and");
}
| formula_assign IMPLIES formula_assign
{
update_buff[up_count].act_formula[af_count].assign_formula.push_back("implies");
}
| formula_assign IFF formula_assign
{
update_buff[up_count].act_formula[af_count].assign_formula.push_back("iff");
}
| NOT formula_assign
{
update_buff[up_count].act_formula[af_count].assign_formula.push_back("not");
}
| LB formula_assign RB
;
update_condition:
formula_guard
| update_condition COMMA update_condition
;
init_next_state: ISI NEXT EQUALS TRUE {
if(initStates_buff.empty())
initStates_buff.push_back(init());
initStates_buff[init_count].t_in.push_back($1);
}
| ISI NEXT EQUALS FALSE {
if(initStates_buff.empty())
initStates_buff.push_back(init());
initStates_buff[init_count].f_in.push_back($1);
}
| init_next_state COMMA init_next_state
| init_next_state SEMICOLON{
init_count++;
initStates_buff.push_back(init());
}
;
update_next_state:
ISI NEXT EQUALS formula_assign
{
update_buff[up_count].act_formula.push_back(update_act());
update_buff[up_count].act_formula[af_count].next_var = $1;
af_count++;
}
| update_next_state COMMA update_next_state
{
}
| update_next_state SEMICOLON{
}
;
update_y: DCOL update_command {
up_count++;
af_count=0;
}
| update_y DCOL update_command {
up_count++;
af_count=0;
}
;
gf:
ISI
{
ltl_goal.push_back($1);
}
| TRUE
{
ltl_goal.push_back("true");
}
| FALSE
{
ltl_goal.push_back("false");
}
| gf OR gf
{
ltl_goal.push_back("or");
}
| gf AND gf
{
ltl_goal.push_back("and");
}
| gf IMPLIES gf
{
ltl_goal.push_back("implies");
}
| gf IFF gf
{
ltl_goal.push_back("iff");
}
| G gf
{
ltl_goal.push_back("G");
}
| F gf
{
ltl_goal.push_back("F");
}
| X gf
{
ltl_goal.push_back("X");
}
| gf U gf
{
ltl_goal.push_back("U");
}
| NOT gf
{
ltl_goal.push_back("not");
}
| LB gf RB
{
}
;
goal_form: DCOL gf SEMICOLON
{
reverse(ltl_goal.begin(),ltl_goal.end());
string res = build_ltl(ltl_goal);
goals.formulae.push_back(res);
ltl_goal.clear();
}
| goal_form SL gf
{
reverse(ltl_goal.begin(),ltl_goal.end());
string res = build_ltl(ltl_goal);
goals.formulae.push_back(res);
ltl_goal.clear();
}
;
%%
int yyerror(string s)
{
extern int yylineno; // defined and maintained in lex.c
extern char *yytext; // defined and maintained in lex.c
cerr << "ERROR: " << s << " at symbol \"" << yytext;
cerr << "\" on line " << yylineno << endl;
exit(1);
}
int yyerror(char *s)
{
return yyerror(string(s));
}