-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_prolog_hornclauses.y
701 lines (635 loc) · 18.8 KB
/
parser_prolog_hornclauses.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
%{
#include <stdio.h>
#include <string.h>
#define CHUNK 1024 /*read 1024 bytes at a time */
#define ABSOLUTE_DEPENDENCY 1
#define G_INDEPENDENCY 2
#define I_INDEPENDENCY 3
#define GI_INDEPENDENCY 4
#define ABSOLUTE_INDEPENDENCY 5
int problem_counter;
int line_counter = 1;
struct variable *var_head;
struct variable *var_tail;
struct partial_problem *pp_head;
struct partial_problem *pp_tail;
struct variable {
char *name;
struct variable *next;
};
struct partial_problem {
struct variable *var;
struct node *node;
struct partial_problem *next;
struct partial_problem *prev;
};
struct node {
int index;
char type;
struct variable *vars;
struct output *out;
struct node *next;
struct node *prev;
};
struct output {
int port;
char type;
struct node *target;
struct output *next;
};
struct dependency {
int type;
struct variable *g_vars;
struct variable *i_vars;
};
void yyerror(char *message);
void gen_var_node(char *var_name);
void gen_partial_problem_node(char type, char *info);
struct variable *gen_var_from_char(char *info);
struct node *gen_node(char type, struct output *output, struct variable *vars);
void insert_node_before(struct node *current, struct node *new);
void insert_node_after(struct node *current, struct node *new);
struct output *gen_output(int port, char type, struct node *target);
void insert_output(struct output *current, struct output *new);
void add_output(struct node *current, int port, char type, struct node *target);
struct node *gen_a_node(struct node *current);
struct node *connect_with_entry(struct node *left, struct node *right);
struct node *gen_absolute_dependency(struct node *left, struct node *right);
struct node *gen_g_independency(struct node *left, struct node *right, struct variable *vars);
struct node *gen_i_independency(struct node *left, struct node *right, struct variable *vars);
struct node *gen_g_i_independency(struct node *left, struct node *right, struct variable *g_vars, struct variable *i_vars);
struct node *get_last_node(struct partial_problem *pp);
void add_variable(struct variable *current, char *new);
struct dependency *check_dependency(struct partial_problem *entry, struct partial_problem *current, struct partial_problem *check);
struct node *connect_and_number_nodes(struct partial_problem *pp);
void print_table();
int print_table_entries(struct node *node,FILE *output_stream);
void schwinn(struct partial_problem *current_pp);
int table_counter = 1;
%}
%union{
char *str;
int num;
}
%start S
%token IMPLIES DOT
%token PLUS MINUS EQUALS NOT IS
%token UNEQUALS SMALLER SMALLER_EQUALS GREATER GREATER_EQUALS
%token COMMA OPEN_PARA CLOSE_PARA OPEN_BRA CLOSE_BRA PIPE ASTERIX COLON SLASH
%token NEW_LINE_FEED
%token <num> NUMBER
%token <str> CONST_ID VAR_ID
%left PLUS MINUS ASTERIX SLASH
%left UNEQUALS SMALLER SMALLER_EQUALS GREATER GREATER_EQUALS
%%
S: S E
| E
| S NEW_LINE_FEED
| NEW_LINE_FEED;
E: RULE
| FACT;
RULE: SR IMPLIES FACT_LIST DOT;
FACT: SR DOT;
SR: CONST_ID OPEN_PARA ARG_LIST CLOSE_PARA {gen_partial_problem_node('E',$1); var_head = 0;};
AR: CONST_ID OPEN_PARA ARG_LIST CLOSE_PARA {gen_partial_problem_node('U', $1); var_head = 0;}
| ARITHMETIC_EXP {gen_partial_problem_node('U',""); var_head = 0;};
ARITHMETIC_EXP: VAR_ID OPERATOR ARITHMETIC_REST;
OPERATOR: PLUS
| MINUS
| EQUALS
| SMALLER_EQUALS
| SMALLER
| GREATER_EQUALS
| GREATER
| UNEQUALS
| ASTERIX
| SLASH
| IS;
ARITHMETIC_REST: VAR_ID
| NUMBER
| CONST_ID
| ARITHMETIC_EXP;
ARG_LIST: ARG COMMA ARG_LIST
| ARG;
FACT_LIST: AR COMMA FACT_LIST
|AR;
LIST: OPEN_BRA HEAD_CONTENT REST_LIST
| OPEN_BRA CLOSE_BRA;
REST_LIST: PIPE ARG CLOSE_BRA
| COMMA ARG REST_LIST
| CLOSE_BRA;
HEAD_CONTENT: VAR_ID {gen_var_node($1);}
|NUMBER
|CONST_ID;
ARG: CONST_ID
|NUMBER
|LIST
|VAR_ID{gen_var_node($1);};
%%
void gen_var_node(char *var_name){
struct variable *ptr = malloc(sizeof(struct variable));
ptr->name = var_name;
ptr->next = 0;
if (!var_head){
var_head = ptr;
var_tail = ptr;
} else{
var_tail->next = ptr;
var_tail = ptr;
}
}
void gen_partial_problem_node(char type, char *info){
struct partial_problem *ptr = malloc(sizeof(struct partial_problem));
ptr->var = var_head;
ptr->next = 0;
ptr->prev = 0;
ptr->node = gen_node(type,0,var_head);
if (!pp_head){
pp_head = ptr;
pp_tail = ptr;
} else{
pp_tail->next = ptr;
ptr->prev = pp_tail;
pp_tail = ptr;
}
}
struct variable *gen_var_from_char(char *info) {
struct variable *var = malloc(sizeof(struct variable));
var->next = 0;
var->name = info;
return var;
}
struct node *gen_node(char type, struct output *output, struct variable *vars) {
struct node *tmp = malloc(sizeof(struct node));
tmp->type = type;
tmp->out = output;
tmp->vars = vars;
tmp->next = 0;
tmp->prev = 0;
return tmp;
}
void insert_node_before(struct node *current, struct node *new) {
new->prev = current->prev;
current->prev->next = new;
new->next = current;
current->prev = new;
}
void insert_node_after(struct node *current, struct node *new) {
new->next = current->next;
if(current->next != 0) {
current->next->prev = new;
}
new->prev = current;
current->next = new;
}
struct output *gen_output(int port, char type, struct node *target) {
struct output *tmp = malloc(sizeof(struct output));
tmp->port = port;
tmp->type = type;
tmp->target = target;
tmp->next = 0;
return tmp;
}
void insert_output(struct output *current, struct output *new) {
new->next = current->next;
current->next = new;
}
void add_output(struct node *current, int port, char type, struct node *target) {
if(current->out != 0) {
struct output *last = current->out;
while(last->next != 0) {
last = last->next;
}
insert_output(last,gen_output(port,type,target));
} else {
current->out = gen_output(port,type,target);
}
}
struct node *gen_a_node(struct node *current) {
if(current->type == 'T') {
current->type = 'A';
return current;
} else {
struct node *a_node = gen_node('A',0,0);
insert_node_after(current,a_node);
add_output(current,1,0,a_node);
return a_node;
}
}
struct node *gen_tmp_node(struct node *current) {
struct node *tmp_node = gen_node('T',0,0);
insert_node_after(current,tmp_node);
add_output(current,1,0,tmp_node);
return tmp_node;
}
struct node *connect_with_entry(struct node *left, struct node *right) {
struct node *u_node = gen_node('U',0,0);
if(right->type == 'T') {
gen_a_node(right);
}
insert_node_after(left,u_node);
if(left->type == 'E') {
add_output(left,2,'L',u_node);
} else {
add_output(left,2,0,u_node);
}
add_output(right,1,0,u_node);
return u_node;
}
struct node *gen_absolute_dependency(struct node *left, struct node *right) {
if(left->type == 'A' && left->out != 0) {
struct node *c_node = gen_node('C',left->out,0);
left->out = gen_output(1,0,c_node);
insert_node_after(left,c_node);
left = c_node;
}
struct node *u_node;
if(right->type == 'T') {
right->type = 'U';
add_output(left,1,0,right);
u_node = right;
} else {
u_node = gen_node('U',0,0);
insert_node_after(right,u_node);
add_output(left,1,0,u_node);
add_output(right,2,0,u_node);
}
return gen_tmp_node(u_node);
}
struct node *gen_g_independency(struct node *left, struct node *right, struct variable *vars) {
if(left->type == 'A' && left->out != 0) {
struct node *c_node = gen_node('C',left->out,0);
left->out = gen_output(1,0,c_node);
insert_node_after(left,c_node);
left = c_node;
}
struct node *g_node;
if(right->type == 'T') {
right->type = 'G';
right->vars = vars;
g_node = right;
} else {
g_node = gen_node('G',0,vars);
insert_node_after(right,g_node);
add_output(right,1,0,g_node);
}
struct node *u_node = gen_node('U',0,0);
insert_node_after(g_node,u_node);
add_output(left,1,0,u_node);
add_output(g_node,2,'L',u_node);
struct node *tmp_node = gen_tmp_node(u_node);
add_output(g_node,1,'R',u_node->out->target);
return tmp_node;
}
struct node *gen_i_independency(struct node *left, struct node *right, struct variable *vars) {
if(left->type == 'A' && left->out != 0) {
struct node *c_node = gen_node('C',left->out,0);
left->out = gen_output(1,0,c_node);
insert_node_after(left,c_node);
left = c_node;
}
struct node *i_node;
if(right->type == 'T') {
right->type = 'I';
right->vars = vars;
i_node = right;
} else {
i_node = gen_node('I',0,vars);
insert_node_after(right,i_node);
add_output(right,1,0,i_node);
}
struct node *u_node = gen_node('U',0,0);
insert_node_after(i_node,u_node);
add_output(left,1,0,u_node);
add_output(i_node,2,'L',u_node);
struct node *tmp_node = gen_tmp_node(u_node);
add_output(i_node,1,'R',u_node->out->target);
return tmp_node;
}
struct node *gen_g_i_independency(struct node *left, struct node *right, struct variable *g_vars, struct variable *i_vars) {
if(left->type == 'A' && left->out != 0) {
struct node *c_node = gen_node('C',left->out,0);
left->out = gen_output(1,0,c_node);
insert_node_after(left,c_node);
left = c_node;
}
struct node *g_node;
if(right->type == 'T') {
right->type = 'G';
right->vars = g_vars;
g_node = right;
} else {
g_node = gen_node('G',0,g_vars);
insert_node_after(right,g_node);
add_output(right,1,0,g_node);
}
struct node *u_node = gen_node('U',0,0);
struct node *i_node = gen_node('I',0,i_vars);
insert_node_after(g_node,i_node);
insert_node_after(i_node,u_node);
add_output(left,1,0,u_node);
add_output(g_node,2,'L',u_node);
add_output(g_node,1,'R',i_node);
add_output(i_node,2,'L',u_node);
struct node *tmp_node = gen_tmp_node(u_node);
add_output(i_node,1,'R',u_node->out->target);
return tmp_node;
}
struct node *get_last_node(struct partial_problem *pp) {
struct node *last_node = pp->node;
while(last_node->next != 0) {
last_node = last_node->next;
}
return last_node;
}
void add_variable(struct variable *current, char *new) {
struct variable * tmp = malloc(sizeof(struct variable));
tmp->name = new;
tmp->next = 0;
while(current->next != 0) {
current = current->next;
}
current->next = tmp;
}
struct dependency *check_dependency(struct partial_problem *entry, struct partial_problem *current, struct partial_problem *check) {
struct variable *entry_var = entry->var;
struct variable *current_var = current->var;
struct variable *check_var = check->var;
struct variable *check_equals = 0;
struct variable *check_different = 0;
struct variable *current_different = 0;
struct dependency *depend = malloc(sizeof(struct dependency));
depend->type = 0;
depend->i_vars = 0;
depend->g_vars = 0;
//check for equals between current and check
while(current_var != 0) {
check_var = check->var;
while(check_var != 0) {
if(strcmp(current_var->name,check_var->name) == 0) {
if(check_equals == 0) {
check_equals = malloc(sizeof(struct variable));
check_equals->name = check_var->name;
check_equals->next = 0;
} else {
add_variable(check_equals,check_var->name);
}
}
check_var = check_var->next;
}
current_var = current_var->next;
}
//check for G independency/absolute dependency
int found;
struct variable *tmp_check_equals = check_equals;
while(tmp_check_equals != 0) {
found = 0;
entry_var = entry->var;
while(entry_var != 0) {
if(strcmp(entry_var->name,tmp_check_equals->name) == 0) {
found = 1;
if(depend->g_vars == 0) {
depend->g_vars = malloc(sizeof(struct variable));
depend->g_vars->name = entry_var->name;
depend->type = G_INDEPENDENCY;
depend->g_vars->next = 0;
} else {
add_variable(depend->g_vars,entry_var->name);
}
}
entry_var = entry_var->next;
}
if(!found) {
depend->type = ABSOLUTE_DEPENDENCY;
return depend;
}
tmp_check_equals = tmp_check_equals->next;
}
//look for all that are in current but not in check
current_var = current->var;
while(current_var != 0) {
found = 0;
tmp_check_equals = check_equals;
while(tmp_check_equals != 0) {
if(strcmp(current_var->name,tmp_check_equals->name) == 0) {
found = 1;
}
tmp_check_equals = tmp_check_equals->next;
}
if(!found) {
if(current_different == 0) {
current_different = malloc(sizeof(struct variable));
current_different->name = current_var->name;
current_different->next = 0;
} else {
add_variable(current_different,current_var->name);
}
}
current_var = current_var->next;
}
//check for I independency on current site and absolute independency
while(current_different != 0) {
entry_var = entry->var;
while(entry_var != 0) {
if(strcmp(current_different->name,entry_var->name) == 0) {
if(depend->i_vars == 0) {
depend->i_vars = malloc(sizeof(struct variable));
depend->i_vars->name = entry_var->name;
if(depend->type == G_INDEPENDENCY) {
depend->type = GI_INDEPENDENCY;
} else {
depend->type = I_INDEPENDENCY;
}
depend->i_vars->next = 0;
} else {
add_variable(depend->i_vars,entry_var->name);
}
}
entry_var = entry_var->next;
}
current_different = current_different->next;
}
if(depend->type == 0) {
depend->type = ABSOLUTE_INDEPENDENCY;
return depend;
}
if(depend->type == GI_INDEPENDENCY || depend->type == I_INDEPENDENCY) {
//look for all that are in check but not in current
check_var = check->var;
while(check_var != 0) {
found = 0;
tmp_check_equals = check_equals;
while(tmp_check_equals != 0) {
if(strcmp(check_var->name,tmp_check_equals->name) == 0) {
found = 1;
}
tmp_check_equals = tmp_check_equals->next;
}
if(!found) {
if(check_different == 0) {
check_different = malloc(sizeof(struct variable));
check_different->name = check_var->name;
check_different->next = 0;
} else {
add_variable(check_different,check_var->name);
}
}
check_var = check_var->next;
}
//check for i independency on check site
while(check_different != 0) {
entry_var = entry->var;
while(entry_var != 0) {
if(strcmp(check_different->name,entry_var->name) == 0) {
if(depend->i_vars == 0) {
depend->i_vars = malloc(sizeof(struct variable));
depend->i_vars->name = entry_var->name;
if(depend->type == G_INDEPENDENCY) {
depend->type = GI_INDEPENDENCY;
} else {
depend->type = I_INDEPENDENCY;
}
depend->i_vars->next = 0;
} else {
add_variable(depend->i_vars,entry_var->name);
}
}
entry_var = entry_var->next;
}
check_different = check_different->next;
}
}
return depend;
}
void schwinn(struct partial_problem *current_pp) {
struct partial_problem *e_problem = current_pp;
struct node *e_node = e_problem->node;
current_pp = current_pp->next;
//part 2.1.1
if(current_pp != 0) {
add_output(e_node,1,'R',current_pp->node);
struct node *left_u_node = connect_with_entry(e_node,gen_a_node(current_pp->node));
//part 2.1.2
current_pp = current_pp->next;
if(current_pp != 0) {
if(current_pp->node->type == 'U'){ //second partial problem
struct node *c_node = gen_node('C',0,0);
insert_node_after(e_node,c_node);
add_output(c_node,1,0,e_node->out->target);
e_node->out->target = c_node;
while(current_pp != 0) {
printf("In Loop");
if(current_pp->node->type == 'U') {
add_output(c_node,1,0,current_pp->node);
struct partial_problem *left_problem = current_pp->prev;
struct node *right_node = current_pp->node;
int absolute_independency = 1;
while(left_problem->node->type != 'E') {
struct dependency *depend = check_dependency(e_problem,current_pp,left_problem);
if(depend->type == ABSOLUTE_DEPENDENCY) {
right_node = gen_absolute_dependency(get_last_node(left_problem),right_node);
absolute_independency = 0;
} else if(depend->type == G_INDEPENDENCY) {
right_node = gen_g_independency(get_last_node(left_problem),right_node,depend->g_vars);
absolute_independency = 0;
} else if(depend->type == I_INDEPENDENCY) {
right_node = gen_i_independency(get_last_node(left_problem),right_node,depend->i_vars);
absolute_independency = 0;
} else if(depend->type == GI_INDEPENDENCY) {
right_node = gen_g_i_independency(get_last_node(left_problem),right_node,depend->g_vars,depend->i_vars);
absolute_independency = 0;
}
left_problem = left_problem->prev;
}
if(absolute_independency) {
right_node = gen_a_node(right_node);
}
left_u_node = connect_with_entry(left_u_node,right_node);
current_pp = current_pp->next;
} else {
break;
}
}
}
struct node *r_node = gen_node('R',0,0);
insert_node_after(left_u_node,r_node);
add_output(left_u_node,1,0,r_node);
} else {
struct node *r_node = gen_node('R',0,0);
insert_node_after(left_u_node,r_node);
add_output(left_u_node,1,0,r_node);
}
} else {
struct node *r_node = gen_node('R',0,0);
insert_node_after(e_node,r_node);
add_output(e_node,1,0,r_node);
}
}
int main(int argc, char **argv) {
extern FILE *yyin;
var_head = 0;
var_tail = 0;
pp_head = 0;
pp_tail = 0;
yyin = fopen("input_file.txt","r");
yyparse();
fclose(yyin);
printf("Starting Schwinn...\n");
schwinn(pp_head);
printf("Printing Node-Table...\n");
print_table();
printf("Success. Terminating...\n");
return 0;
}
struct node *connect_and_number_nodes(struct partial_problem *pp) {
struct node *head = pp->node;
struct node *current = head;
int index = 1;
while(pp!=0) {
while(current->next!=0) {
current->index = index;
index++;
current = current->next;
}
pp = pp->next;
if(pp!=0) {
current->next = pp->node;
}
current->index = index;
index++;
current = current->next;
}
return head;
}
void print_table() {
struct node *current = connect_and_number_nodes(pp_head);
FILE *table_out;
table_out = fopen("output_table.txt","a+");
printf("\nCongrats. You seem to have a clue about Horn clauses.\n");
while(current != 0) {
print_table_entry(current,table_out);
current = current->next;
}
fclose(table_out);
}
int print_table_entry(struct node *node,FILE *output_stream){
fprintf(output_stream,"%-5d%-3c",node->index,node->type);
struct output *out = node->out;
while(out!=0) {
if(out->type != 0) {
fprintf(output_stream,"%c:(%d,%d) ",out->type,out->target->index,out->port);
} else {
fprintf(output_stream,"(%d,%d) ",out->target->index,out->port);
}
out = out->next;
}
struct variable *vars = node->vars;
while(vars!=0) {
fprintf(output_stream,"%s,",vars->name);
vars = vars->next;
}
fprintf(output_stream,"\n");
}
void yyerror (char *message){
printf("\nThis is not a Horn clause. Please start the program again\n");
}