-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction_models.c
More file actions
3531 lines (3079 loc) · 116 KB
/
Copy pathfunction_models.c
File metadata and controls
3531 lines (3079 loc) · 116 KB
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Stranger
* Copyright (C) 2013-2014 University of California Santa Barbara.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335,
* USA.
*
* Authors: Muath Alkhalaf
*/
#include "stranger.h"
#include "stranger_lib_internal.h"
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <limits.h>
#include "utility.h"
/*=====================================================================*/
/* Data structure for transition relation matrix
*/
void removeTransitionOnChar(char* transitions, char* charachter, int var, char** result, int* pSize);
struct transition_type{
int from;
int to;
struct transition_type *next;
};
struct transition_list_type{
int count;
struct transition_type *head;
struct transition_type *tail;
};
struct transition_type *transition_new_it(int from, int to) {
struct transition_type *tmp;
tmp = (struct transition_type *) malloc(sizeof(struct transition_type));
tmp->from = from;
tmp->to = to;
tmp->next = NULL;
return tmp;
}
struct transition_list_type *transition_new_ilt() {
struct transition_list_type *tmp;
tmp = (struct transition_list_type *) malloc(sizeof(struct transition_list_type));
tmp->count = 0;
tmp->head = tmp->tail = NULL;
return tmp;
}
struct transition_list_type *transition_add_data(struct transition_list_type *list, struct transition_type *data)
{
if (data == NULL)
return list;
if (list == NULL)
list = transition_new_ilt();
if (list->count > 0) {
list->tail->next = data;
list->tail = list->tail->next;
list->count++;
} else {
list->head = list->tail = data;
list->count = 1;
}
return list;
}
int transition_check_value(list, from, to)
struct transition_list_type *list;int from; int to; {
struct transition_type *tmp = NULL;
if (list != NULL)
for (tmp = list->head; tmp != NULL; tmp = tmp->next)
if (tmp->from == from && tmp->to == to)
return 1;
return 0;
}
struct transition_list_type *transition_remove_value(struct transition_list_type *list, int from, int to)
{
struct transition_type *tmp = NULL;
struct transition_type *del = NULL;
if (transition_check_value(list, from, to) > 0) {
for (tmp = list->head; tmp != NULL; tmp = tmp->next)
if ((tmp->from == from && tmp->to == to) && (list->count == 1)) { //remove tmp
list->count = 0;
list->head = list->tail = NULL;
free(tmp);
return list;
} else if ((tmp->from == from && tmp->to == to) && (list->head == tmp)) {
list->count--;
list->head = list->head->next;
free(tmp);
return list;
} else if ((tmp->next != NULL) && (tmp->next->from == from && tmp->next->to == to)) {
if (tmp->next->next == NULL) { //remove tail
list->count--;
list->tail = tmp;
list->tail->next = NULL;
free(tmp->next);
return list;
} else {
list->count--;
del = tmp->next;
tmp->next = tmp->next->next;
free(del);
return list;
}
}
}
return list;
}
int transition_check_data(list, data)
struct transition_list_type *list;struct transition_type *data; {
struct transition_type *tmp = NULL;
if ((list != NULL) && (data != NULL))
for (tmp = list->head; tmp != NULL; tmp = tmp->next)
if (tmp->from == data->from && tmp->to == data->to)
return 1;
return 0;
}
//no duplicate
struct transition_list_type *transition_enqueue(struct transition_list_type *list, int from, int to)
{
if (!transition_check_value(list, from, to))
list = transition_add_data(list, transition_new_it(from, to));
return list;
}
struct transition_type *transition_dequeue(struct transition_list_type *list)
{
struct transition_type *data = NULL;
struct transition_type *i = NULL;
if ((list == NULL) || (list->count == 0))
return NULL;
else
data = list->head;
if (list->count == 1) {
list->count = 0;
list->head = list->tail = NULL;
} else {
list->head = list->head->next;
list->count--;
}
i = data;
free(data);
return i;
}
void transition_free_ilt(struct transition_list_type *list) {
struct transition_type *tmp = transition_dequeue(list);
while (tmp != NULL)
tmp = transition_dequeue(list);
free(list);
}
void transition_print_ilt(struct transition_list_type *list) {
if (list == NULL){
printf("list is empty.\n");
return;
}
struct transition_type *tmp = list->head;
while (tmp != NULL) {
printf("-> from: %d, to:%d", tmp->from, tmp->to);
tmp = tmp->next;
}
}
/*
DFA *dfaPreRightTrim(DFA *M, char c, int var, int *oldIndices) {
DFA *result;
paths state_paths, pp;
trace_descr tp;
int i, j, k;
char *exeps;
int *to_states;
int sink;
long max_exeps;
char *statuces;
int len;
int ns = M->ns;
int *indices;
char* lambda = bintostr(c, var);
len = var + 1;
max_exeps = 1 << len; //maybe exponential
sink = find_sink(M);
assert(sink>-1);
indices = allocateArbitraryIndex(len);
char* symbol = (char *) malloc((len + 1) * sizeof(char));//len+1 since we need extra bit
exeps = (char *) malloc(max_exeps * (len + 1) * sizeof(char));
to_states = (int *) malloc(max_exeps * sizeof(int));
statuces = (char *) malloc((ns + 1) * sizeof(char));
strcpy(symbol, lambda); symbol[var] = '1'; symbol[len] = '\0';
dfaSetup(ns, len, indices);
for (i = 0; i < M->ns; i++) {
state_paths = pp = make_paths(M->bddm, M->q[i]);
k = 0;
while (pp) {
if (pp->to != sink) {
to_states[k] = pp->to;
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != indices[j]); tp
= tp->next)
;
if (tp) {
if (tp->value)
exeps[k * (len + 1) + j] = '1';
else
exeps[k * (len + 1) + j] = '0';
} else
exeps[k * (len + 1) + j] = 'X';
}
exeps[k * (len + 1) + j] = '0';// for init state extrabit 1 goes to self loop for lambda. Everthing else goes to sink
exeps[k * (len + 1) + len] = '\0';// if no extrabit will overwrite assign in prev line
k++;
}
pp = pp->next;
}
kill_paths(state_paths);
// if accept state create a self loop on lambda
if (M->f[i] == 1){
dfaAllocExceptions(k+1);
dfaStoreException(i, symbol);
}
else
dfaAllocExceptions(k);
for (k--; k >= 0; k--)
dfaStoreException(to_states[k], exeps + k * (len + 1));
dfaStoreState(sink);
if (M->f[i] == -1)
statuces[i] = '-';
else if (M->f[i] == 1)
statuces[i] = '+';
else
statuces[i] = '-';// DO NOT USE don't care
}
statuces[ns] = '\0';
DFA* tmpM = dfaBuild(statuces);
result = dfaProject(tmpM, ((unsigned)var));
dfaFree(tmpM); tmpM = NULL;
tmpM = dfaMinimize(result);
dfaFree(result);result = NULL;
free(exeps);
free(symbol);
free(lambda);
free(to_states);
free(statuces);
free(indices);
return tmpM;
}
*/
/*
DFA *dfaPreLeftTrim(DFA *M, char c, int var, int *oldIndices) {
DFA *result;
paths state_paths, pp;
trace_descr tp;
int i, j, k;
char *exeps;
int *to_states;
int sink;
long max_exeps;
char *statuces;
int len;
int ns = M->ns;
int *indices;
char* lambda = bintostr(c, var);
boolean extraBitNeeded = FALSE;
sink = find_sink(M);
assert(sink>-1);
//printf("\n\n SINK %d\n\n\n", sink);
char* symbol = (char *) malloc((var + 2) * sizeof(char));//var+2 since we may need extra bit
//construct the added paths for the initial state
state_paths = pp = make_paths(M->bddm, M->q[M->s]);
//printf("\n\n INIT %d \n\n", M1->s);
while (pp) {
if (pp->to != sink) {
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != oldIndices[j]); tp
= tp->next)
;
if (tp) {
if (tp->value)
symbol[j] = '1';
else
symbol[j] = '0';
} else
symbol[j] = 'X';
}
symbol[j] = '\0';
if (isIncludeLambda(symbol, lambda, var)){
if (pp->to == M->q[M->s]){
result = dfaCopy(M);
kill_paths(state_paths);
free(symbol);
free(lambda);
return result;
}
else{
extraBitNeeded = TRUE;
break;
}
}
}
pp = pp->next;
}
kill_paths(state_paths);
len = extraBitNeeded? var + 1: var;
indices = extraBitNeeded? allocateArbitraryIndex(len) : oldIndices;
max_exeps = 1 << len; //maybe exponential
dfaSetup(ns, len, indices);
exeps = (char *) malloc(max_exeps * (len + 1) * sizeof(char));
to_states = (int *) malloc(max_exeps * sizeof(int));
statuces = (char *) malloc((ns + 1) * sizeof(char));
for (i = 0; i < M->ns; i++) {
//construct the added paths for the initial state
state_paths = pp = make_paths(M->bddm, M->q[i]);
//printf("\n\n INIT %d \n\n", M1->s);
k = 0;
while (pp) {
if (pp->to != sink) {
to_states[k] = pp->to;
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != indices[j]); tp
= tp->next)
;
if (tp) {
if (tp->value)
exeps[k * (len + 1) + j] = '1';
else
exeps[k * (len + 1) + j] = '0';
} else
exeps[k * (len + 1) + j] = 'X';
}
exeps[k * (len + 1) + j] = '0';// for init state extrabit 1 goes to self loop for lambda. Everthing else goes to sink
exeps[k * (len + 1) + len] = '\0';// if no extrabit will overwrite assign in prev line
k++;
}
pp = pp->next;
}
kill_paths(state_paths);
if (i == M->s){
strcpy(symbol, lambda);
if (extraBitNeeded){
symbol[var] = '1';
symbol[len] = '\0';
}
dfaAllocExceptions(k+1);
dfaStoreException(M->s, symbol);
}
else
dfaAllocExceptions(k);
for (k--; k >= 0; k--)
dfaStoreException(to_states[k], exeps + k * (len + 1));
dfaStoreState(sink);
if (M->f[i] == -1)
statuces[i] = '-';
else if (M->f[i] == 1)
statuces[i] = '+';
else
statuces[i] = '0';
}
statuces[ns] = '\0';
DFA* tmpM = dfaBuild(statuces);
free(exeps);
free(symbol);
free(to_states);
free(statuces);
free(lambda);
if (extraBitNeeded){
free(indices);
result = dfaProject(tmpM, ((unsigned)var));
dfaFree(tmpM); tmpM = NULL;
tmpM = dfaMinimize(result);
dfaFree(result);result = NULL;
return tmpM;
}
else
{
result = dfaMinimize(tmpM);
dfaFree(tmpM);tmpM = NULL;
return result;
}
}
*/
struct transition_list_type *getTransitionRelationMatrix(DFA* M, char *lambda,
int var, int* indices) {
paths state_paths, pp;
trace_descr tp;
int j;
int sink = find_sink(M);
char *symbol = (char *) malloc((var+1)*sizeof(char));
struct transition_list_type *finallist = NULL;
int i;
for (i = 0; i < M->ns; i++) {
state_paths = pp = make_paths(M->bddm, M->q[i]);
while (pp) {
if (pp->to != sink) {
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != indices[j]); tp =
tp->next)
;
if (tp) {
if (tp->value)
symbol[j] = '1';
else
symbol[j] = '0';
} else
symbol[j] = 'X';
}
symbol[j] = '\0';
if (isIncludeLambda(symbol, lambda, var)) {
finallist = transition_enqueue(finallist, i, pp->to);
}
}
pp = pp->next;
} //end while
kill_paths(state_paths);
}
// printf("list of states reachable on \\s:");
// transition_print_ilt(finallist);
// printf("\n");
free(symbol);
return finallist;
}
struct int_list_type * findReversePathsOnLambda(struct transition_list_type *transition_relation, struct int_list_type *finallist, int current_state){
finallist = enqueue(finallist, current_state);
struct transition_type *tmp2;
// for all transition relation on lambda
for (tmp2 = transition_relation->head; tmp2 != NULL; tmp2 = tmp2->next) {
// if current transition has current state as destination then follow transition in reverse if has not been followed before
if ((current_state == tmp2->to) && (!check_value(finallist, tmp2->from))){
finallist = findReversePathsOnLambda(transition_relation, finallist, tmp2->from);
}
}
return finallist;
}
struct int_list_type *states_reach_accept_lambda(DFA* M, char* lambda, int var, int* indices){
struct transition_list_type *transition_relation = getTransitionRelationMatrix(M, lambda, var, indices);
if (transition_relation == NULL)
return NULL;
struct int_list_type *finallist=NULL;
// for each accepting state get list that reach the accepting state on lambda
struct transition_type *tmp = transition_relation->head;
while (tmp != NULL) {
// for each accept state
if (M->f[tmp->to] == 1){
// if accept state has not been processed before
if (!check_value(finallist, tmp->to)){
finallist = findReversePathsOnLambda(transition_relation, finallist, tmp->to);
}
}
tmp = tmp->next;
}
// free unneeded memory
transition_free_ilt(transition_relation);
// printf("states that reach an accepting state on lambda: ");
// print_ilt(finallist);
// printf("\n");
return finallist;
}
DFA *dfaRightTrim(DFA *M, char c, int var, int *oldindices) {
DFA *result = NULL;
DFA *tmpM = NULL;
char* lambda = bintostr(c, var);
int aux = 1;
struct int_list_type *states = NULL;
int maxCount = 0;
int *indices = oldindices; //indices is updated if you need to add auxiliary bits
paths state_paths, pp;
trace_descr tp;
int i, j, z, k;
char *exeps;
int *to_states;
long max_exeps;
char *statuces;
int len = var;
int sink, new_accept_state;
boolean split_char;
int numOfChars;
char** charachters;
int size;
char *symbol;
states = states_reach_accept_lambda(M, lambda, var, indices);
if (states == NULL ){
free(lambda);
return dfaCopy(M);
}
symbol = (char *) malloc((var + 1) * sizeof(char));
maxCount = states->count;
if (maxCount > 0) { //Need auxiliary bits when there exist some outgoing edges
aux = 1;
len = var + aux; // extra aux bits
indices = allocateArbitraryIndex(len);
}
max_exeps = 1 << len; //maybe exponential
sink = find_sink(M);
assert(sink > -1);
new_accept_state = M->ns;
numOfChars = 1 << var;
charachters = (char**) malloc(numOfChars * (sizeof(char*)));
//pairs[i] is the list of all reachable states by \sharp1 \bar \sharp0 from i
dfaSetup(M->ns + 1, len, indices); //add one new accept state
exeps = (char *) malloc(max_exeps * (len + 1) * sizeof(char)); //plus 1 for \0 end of the string
to_states = (int *) malloc(max_exeps * sizeof(int));
statuces = (char *) malloc((M->ns + 2) * sizeof(char)); //plus 2, one for the new accept state and one for \0 end of the string
// for each original state
for (i = 0; i < M->ns; i++) {
state_paths = pp = make_paths(M->bddm, M->q[i]);
k = 0;
// for each transition out from current state (state i)
while (pp) {
if (pp->to != sink) {
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != indices[j]); tp =
tp->next)
;
if (tp) {
if (tp->value)
symbol[j] = '1';
else
symbol[j] = '0';
} else
symbol[j] = 'X';
}
symbol[var] = '\0';
// first copy to original destination without removing lambda
to_states[k] = pp->to;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + var] = '0';
exeps[k * (len + 1) + len] = '\0';
k++;
split_char = check_value(states, pp->to);
if (split_char == TRUE) {
// second copy to new accept state after removing lambda
if (!isIncludeLambda(symbol, lambda, var)) {
// no lambda send as it is
to_states[k] = new_accept_state; // destination new accept state
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + var] = '1';
exeps[k * (len + 1) + len] = '\0';
k++;
} else {
// remove lambda then send copy to new accept state
removeTransitionOnChar(symbol, lambda, var, charachters,
&size);
for (z = 0; z < size; z++) {
// first copy of non-bamda char to original destination
// printf("%s, ", charachters[z]);
to_states[k] = new_accept_state; // destination new accept state
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = charachters[z][j];
exeps[k * (len + 1) + var] = '1';
exeps[k * (len + 1) + len] = '\0';
k++;
free(charachters[z]);
}
// printf("\n");
}
}
}
pp = pp->next;
} //end while
dfaAllocExceptions(k);
for (k--; k >= 0; k--)
dfaStoreException(to_states[k], exeps + k * (len + 1));
dfaStoreState(sink);
statuces[i] = '-';
kill_paths(state_paths);
} // end for each original state
// add new accept state
dfaAllocExceptions(0);
dfaStoreState(sink);
statuces[new_accept_state] = '+';
statuces[M->ns + 1] = '\0';
result = dfaBuild(statuces);
// printf("dfaAfterRightTrimBeforeMinimize\n");
// dfaPrintGraphviz(result, len, indices);
if( DEBUG_SIZE_INFO )
printf("\t peak : right_trim : states %d : bddnodes %u : before projection \n", result->ns, bdd_size(result->bddm) );
j = len - 1;
tmpM = dfaProject(result, (unsigned) j);
dfaFree(result);result = NULL;
if( DEBUG_SIZE_INFO )
printf("\t peak : right_trim : states %d : bddnodes %u : after projection \n", tmpM->ns, bdd_size(tmpM->bddm) );
result = dfaMinimize(tmpM);
dfaFree(tmpM);tmpM = NULL;
// if original accept epsilon or start state reaches an accept state on lambda (\s+ is an element of L(M))
//then add epsilon to language
if (M->f[M->s] == 1 || check_value(states, M->s)){
tmpM = dfa_union_empty_M(result, var, indices);
dfaFree(result); result = NULL;
result = tmpM;
}
free(exeps);
//printf("FREE ToState\n");
free(to_states);
//printf("FREE STATUCES\n");
free(statuces);
free(charachters);
free_ilt(states);
free(lambda);
free(symbol);
if (maxCount > 0) free(indices);
return result;
}
DFA *dfaPreRightTrim(DFA *M, char c, int var, int *oldIndices)
{
DFA *result = NULL;
DFA *tmpM = NULL;
char* lambda = bintostr(c, var);
int *indices = oldIndices; //indices is updated if you need to add auxiliary bits
paths state_paths, pp;
trace_descr tp;
int i, j, k, z;
char *exeps;
int *to_states;
long max_exeps;
char *statuces;
int len;
int sink;
int size = 0;
int numOfChars;
char** charachters;
char *symbol;
sink=find_sink(M);
assert(sink >-1);
symbol=(char *)malloc((var+1)*sizeof(char));
/**************************************************
* Add a new start state with space self loop *
**************************************************/
len = var + 1;
indices = allocateArbitraryIndex(len);
max_exeps=1<<len; //maybe exponential
numOfChars = 1<<var;
charachters = (char**) malloc(numOfChars * (sizeof (char*)));
unsigned shift = 0;
if (M->f[M->s] == 1){
//if start state is accepting then
//one new start state and one new accept state
dfaSetup(M->ns+2, len, indices); //add one new initial state as start state
shift = 1;
statuces=(char *)malloc((M->ns+3)*sizeof(char));//two states + null
}
else {
dfaSetup(M->ns+1, len, indices); //add one new initial state as start state
shift = 0;
statuces=(char *)malloc((M->ns+2)*sizeof(char));//1 state + null
}
unsigned newStart = 0;
unsigned newAcceptState = M->ns + shift;
exeps=(char *)malloc(max_exeps*(len+1)*sizeof(char)); //plus 1 for \0 end of the string
to_states=(int *)malloc(max_exeps*sizeof(int));
//printf("Before Replace Char\n");
//dfaPrintVerbose(M);
//if start state is an accept state then add a new start state
//Reason: since original start is accepting then
//preimage may have \s* that is removed by right
//trim. Example: rightTrim(\s*(ab)*)= (ab)*|\s*(ab)+
if (M->f[M->s] == 1){
//construct the added paths for the initial state
state_paths = pp = make_paths(M->bddm, M->q[M->s]);
//printf("\n\n INIT %d \n\n", M1->s);
k=0;
/****** Copy transitions from original start state to new one ********/
//reset pp
while (pp) {
if (pp->to != sink) {
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != indices[j]); tp = tp->next);
if (tp) {
if (tp->value)
symbol[j] = '1';
else
symbol[j] = '0';
} else
symbol[j] = 'X';
}
symbol[j] = '\0';
//case -1- copying a transition from old start to a state that is not accepting
//Transition may have lambda on them
if (M->f[pp->to] != 1){
//if start state does not go to and accept state
//start state will have lambde on a self cycle so take care of lambda to other states
if (!isIncludeLambda(symbol, lambda, var)) { // Only Consider Non-lambda case
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
#if MORE_WORDS_LESS_NDTRANS == 1
exeps[k * (len + 1) + j] = 'X';//<-- only if len > var this will matter
#else
exeps[k * (len + 1) + j] = '0';//<-- only if len > var this will matter
#endif
exeps[k * (len + 1) + len] = '\0';
k++;
}
else {
#if MORE_WORDS_LESS_NDTRANS == 1
removeTransitionOnChar(symbol, lambda, var, charachters, &size);
for (i = 0; i < size; i++)
{
// printf("%s, ", charachters[i]);
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = charachters[i][j];
exeps[k * (len + 1) + j] = 'X';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
free(charachters[i]);
k++;
}
// printf("\n");
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = lambda[j];
exeps[k * (len + 1) + j] = '0';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
k++;
#else
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + j] = '0';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
k++;
#endif
}
}
else {
//copy -1- to original accept state
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + j] = '0';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
k++;
//copy -2- to new accept state. Do not copy lambda transitions (reason is complex but true)
if (!isIncludeLambda(symbol, lambda, var)) { // Only Consider Non-lambda case
//copy -2- to new accept state no lambda deletion
to_states[k] = newAcceptState;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + j] = '1';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
k++;
}
else {
//copy -2- to new accept state with lambda deletion
removeTransitionOnChar(symbol, lambda, var, charachters, &size);
for (i = 0; i < size; i++)
{
// printf("%s, ", charachters[i]);
to_states[k] = newAcceptState;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = charachters[i][j];
exeps[k * (len + 1) + j] = '1';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
free(charachters[i]);
k++;
}
// printf("\n");
}
}
} //end if
pp = pp->next;
} //end while
kill_paths(state_paths);
//add self cycle on lambda
to_states[k] = newStart;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = lambda[j];
exeps[k * (len + 1) + j] = '1';//<-- only if len > var then this is the other lambda
exeps[k * (len + 1) + len] = '\0';
k++;
//original out trans from origianl start state + 1 for lambda self cycle
dfaAllocExceptions(k);
//copy out edges from original start state
for(k--;k>=0;k--)
dfaStoreException(to_states[k],exeps+k*(len+1));
dfaStoreState(sink+shift);
// new one should be accepting
statuces[0]='+';
}//end if start state is an accept state
/**************************************************
* Add remaining states from old automaton *
**************************************************/
//for the rest of states
//1- shift one state
//2- change accept to non accept
//3- if a state goes to an accept state then
//copy transition to new accept state excep
//transitions on space
for (i = 0; i < M->ns; i++) {
state_paths = pp = make_paths(M->bddm, M->q[i]);
k = 0;
while (pp) {
if (pp->to != sink) {
for (j = 0; j < var; j++) {
//the following for loop can be avoided if the indices are in order
for (tp = pp->trace; tp && (tp->index != indices[j]); tp =
tp->next)
;
if (tp) {
if (tp->value)
symbol[j] = '1';
else
symbol[j] = '0';
} else
symbol[j] = 'X';
}
//case -1- copying a transition from old start to a state that is not accepting
//Transition may have lambda on them
if (M->f[pp->to] != 1){
//start state will have lambde on a self cycle so take care of lambda to other states
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
#if MORE_WORDS_LESS_NDTRANS == 1
exeps[k * (len + 1) + j] = 'X';//<-- only if len > var this will matter
#else
exeps[k * (len + 1) + j] = '0';//<-- only if len > var this will matter
#endif
exeps[k * (len + 1) + len] = '\0';
k++;
}
else {
//copy -1- to original accept state
to_states[k] = pp->to + shift;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + j] = '0';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
k++;
//copy -2- to new accept state. Do not copy lambda transitions (reason is complex but true)
if (!isIncludeLambda(symbol, lambda, var)) { // Only Consider Non-lambda case
//copy -2- to new accept state no lambda deletion
to_states[k] = newAcceptState;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = symbol[j];
exeps[k * (len + 1) + j] = '1';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
k++;
}
else {
//copy -2- to new accept state with lambda deletion
removeTransitionOnChar(symbol, lambda, var, charachters, &size);
for (z = 0; z < size; z++)
{
// printf("%s, ", charachters[i]);
to_states[k] = newAcceptState;
for (j = 0; j < var; j++)
exeps[k * (len + 1) + j] = charachters[z][j];
exeps[k * (len + 1) + j] = '1';//<-- only if len > var this will matter
exeps[k * (len + 1) + len] = '\0';
free(charachters[z]);
k++;
}
// printf("\n");
}
}
}
pp = pp->next;
} //end while
kill_paths(state_paths);
dfaAllocExceptions(k);
for (k--; k >= 0; k--)
dfaStoreException(to_states[k], exeps + k * (len + 1));
dfaStoreState(sink + shift);
statuces[i + shift] = '-';