-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlane_tracing.c
3699 lines (3047 loc) · 89.2 KB
/
lane_tracing.c
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
/*
This file is part of Hyacc, a LR(0)/LALR(1)/LR(1)/LR(k) parser generator.
Copyright (C) 2008, 2009 Xin Chen. [email protected]
Hyacc 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.
Hyacc 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 Hyacc; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Lane tracing functions.
*
* @Author: Xin Chen
* @Created on: 2/28/2008
* @Last modified: 3/24/2009
*/
#include "stack_config.h"
#include "lane_tracing.h"
#define DEBUG_PHASE_1 0
#define DEBUG_RESOLVE_CONFLICT 0
#define DEBUG_PHASE_2 0
#define DEBUG_GET_LANEHEAD 0
#define DEBUG_PHASE_2_GET_TBL 0
#define DEBUG_PHASE_2_REGENERATE2 0
#define DEBUG_PHASE_2_REGENERATE 0
#define DEBUG_GET_ORIGINATOR 0
#define DEBUG_ORIGIN 0
/*
* Variables used for lane tracing. Start.
*/
static stack * LANE;
static stack * STACK;
static int TRACE_FURTHER;
static int TEST_FAILED;
/*
* Current final config that is traced in trace_back() function.
* Used for phase 2 state combining.
*/
Configuration * cur_red_config;
static BOOL GRAMMAR_AMBIGUOUS = FALSE;
#define FLAG_ON 1
#define FLAG_OFF 0
static Configuration * LT_MARKER = (Configuration *) -1;
static Configuration * LT_ZERO = 0;
extern BOOL in_lanetracing;
BOOL IN_EDGE_PUSHING_LANE_TRACING;
SymbolList EDGE_PUSHING_CONTEXT_GENERATED;
/*
* Variables used for lane tracing. End.
*/
static void get_originators(Configuration * c0, Configuration * c);
static void get_transitors(Configuration * c0, Configuration * c);
static void DO_LOOP();
static void CHECK_LANE_TOP();
static int findSuccessorStateNo(int state_no, SymbolTblNode * snode);
void my_writeState(State * s);
static void set_transitors_pass_thru_on(
Configuration * cur_config, Configuration * o);
static BOOL inheritParentContext(State * s, State * parent);
/*
* Initialize this to NULL at the beginning of phase2_regeneration2().
* This list is in the order of cluster insertion.
*/
LT_cluster * all_clusters;
LT_cluster * all_clusters_tail;
LT_cluster * new_cluster;
/*
* Initialized to TRUE. If in regeneration context conflicts occur,
* set this to FALSE, which means the grammar is NOT LR(1).
*/
BOOL all_pairwise_disjoint;
/*
* Initialize this to NULL at the beginning of lane_tracing_phase2().
* This list is in INC order on from_state->state_no.
*/
LT_tbl_entry * LT_tbl;
/* declaration of functions */
void dump_LT_tbl();
LT_tbl_entry * LT_tbl_findEntry(int from_state);
BOOL cluster_trace_new_chain_all(int parent_state, LT_tbl_entry * e);
void dump_llist_context_set(llist_context_set * contxt_set)
{
llist_context_set * c = contxt_set;
SymbolNode * n;
if (c == NULL) return;
for (; c != NULL; c = c->next) {
// first write the config.
printf("%d.%d:", c->config->owner->state_no, c->config->ruleID);
// then write the context symbol list.
printf("{ ");
for (n = c->ctxt; n != NULL; n = n->next) {
printf("%s ", n->snode->symbol);
}
printf("} ");
}
}
/*
* Assumption: contxt_set is in INC order.
*/
llist_context_set * llist_context_set_create(
Configuration * config) //, SymbolList contxt_set)
{
llist_context_set * s;
HYY_NEW(s, llist_context_set, 1);
s->config = config;
s->ctxt = NULL; //cloneSymbolList(contxt_set);
s->next = NULL;
return s;
}
void llist_context_set_destroy(llist_context_set * s)
{
freeSymbolNodeList(s->ctxt);
free(s);
}
/*
* Add (merge) contxt_set to c->ctxt.
*/
void llist_context_set_addContext(
llist_context_set * c, SymbolList contxt_set)
{
SymbolNode * n;
int exist;
if (NULL == c) return;
for (n = contxt_set; n != NULL; n = n->next) {
//if (strlen(n->snode->symbol) == 0) continue; // ignore empty string.
c->ctxt = insertSymbolList_unique_inc(c->ctxt, n->snode, & exist);
}
}
/*
* Return a clone of c.
*/
llist_context_set * llist_context_set_clone(llist_context_set * c)
{
llist_context_set * d, * d_next, * c_next;
if (NULL == c) return NULL;
//printf("clone c: "); dump_llist_context_set(c); puts("");
d_next = d = llist_context_set_create(c->config);
d->ctxt = cloneSymbolList(c->ctxt);
for (c_next = c->next; c_next != NULL; c_next = c_next->next) {
d_next->next = llist_context_set_create(c_next->config);
d_next->next->ctxt = cloneSymbolList(c_next->ctxt);
d_next = d_next->next;
}
//printf("result d: "); dump_llist_context_set(d); puts("");
return d;
}
// llist_int functions. START.
llist_int * llist_int_create(int n)
{
llist_int * item;
HYY_NEW(item, llist_int, 1);
item->n = n;
item->next = NULL;
return item;
}
void llist_int_destroy(llist_int * list)
{
llist_int * tmp;
while (list != NULL) {
tmp = list;
list = list->next;
free(tmp);
}
}
/*
* Add n to the list in INC order.
*/
llist_int * llist_int_add_inc(llist_int * list, int n)
{
llist_int * t, * t_prev;
int cmp;
if (list == NULL) { return llist_int_create(n); }
// else, list is not empty. insert in INC order.
for (t_prev = NULL, t = list; t != NULL; t_prev = t, t = t->next) {
cmp = t->n - n;
if (cmp == 0) return list; // exists already.
if (cmp > 0) { // insert before t.
if (t_prev == NULL) { // insert as the head.
list = llist_int_create(n);
list->next = t;
} else { // insert in the middle
t_prev->next = llist_int_create(n);
t_prev->next->next = t;
}
return list;
}
// else, go on to the next state in list.
}
// now is at the end of the list. Add to list tail.
t_prev->next = llist_int_create(n);
return list;
}
/*
* Add n to the head of list.
* Return: pointer to the head of the list.
*/
llist_int * llist_int_add_head(llist_int * list, int n)
{
llist_int * t;
t = llist_int_create(n);
t->next = list;
return t;
}
void llist_int_dump(llist_int * list)
{
llist_int * x;
if (list == NULL) return;
for (x = list; x != NULL; x = x->next) {
printf("%d ", x->n);
}
}
// llist_int functions. END.
// llist_int2 functions. START.
llist_int2 * llist_int2_create(int n1, int n2)
{
llist_int2 * item;
HYY_NEW(item, llist_int2, 1);
item->n1 = n1;
item->n2 = n2;
item->next = NULL;
return item;
}
void llist_int2_destroy(llist_int2 * list)
{
llist_int2 * tmp;
while (list != NULL) {
tmp = list;
list = list->next;
free(tmp);
}
}
/*
* Add n1, n2 to the list in INC order of n1.
*/
llist_int2 * llist_int2_add_inc(llist_int2 * list, int n1, int n2)
{
llist_int2 * t, * t_prev;
int cmp;
if (list == NULL) { return llist_int2_create(n1, n2); }
// else, list is not empty. insert in INC order.
for (t_prev = NULL, t = list; t != NULL; t_prev = t, t = t->next) {
cmp = t->n1 - n1;
if (cmp == 0) return list; // exists already.
if (cmp > 0) { // insert before t.
if (t_prev == NULL) { // insert as the head.
t_prev = list = llist_int2_create(n1, n2);
list->next = t;
} else { // insert in the middle
t_prev->next = llist_int2_create(n1, n2);
t_prev->next->next = t;
}
return list;
}
// else, go on to the next state in list.
}
// now is at the end of the list. Add to list tail.
t_prev->next = llist_int2_create(n1, n2);
return list;
}
/*
* Add n to the head of list.
* Return: pointer to teh head of the list.
*/
llist_int2 * llist_int2_add_head(llist_int2 * list, int n1, int n2)
{
llist_int2 * t;
t = llist_int2_create(n1, n2);
t->next = list;
return t;
}
/*
* Find the node in a llist_int list whose first entry is n1.
*/
llist_int2 * llist_int2_find_n1(llist_int2 * list, int n1)
{
llist_int2 * t;
for (t = list; t != NULL; t = t->next) {
if (t->n1 == n1) return t;
}
return NULL;
}
/*
* Find the node in a llist_int list whose first entry is n2.
*/
llist_int2 * llist_int2_find_n2(llist_int2 * list, int n2)
{
llist_int2 * t;
for (t = list; t != NULL; t = t->next) {
if (t->n2 == n2) return t;
}
return NULL;
}
void llist_int2_dump(llist_int2 * list)
{
llist_int2 * x;
if (list == NULL) return;
for (x = list; x != NULL; x = x->next) {
printf("(%d, %d) ", x->n1, x->n2);
}
}
// llist_int2 functions, END.
LT_tbl_entry * LT_tbl_entry_create(State * from, State * to)
{
LT_tbl_entry * e;
HYY_NEW(e, LT_tbl_entry, 1);
e->from_state = from->state_no;
if (e->from_state !=
states_new_array->state_list[e->from_state]->state_no) {
printf("ERROR (create_LT_tbl_entry): state_no not equal!\n");
exit(1);
}
e->processed = FALSE;
e->ctxt_set = NULL;
e->to_states = (NULL == to) ? NULL : llist_int_create(to->state_no);
e->next = NULL;
return e;
}
/*
* A LT_tbl_entry can have more than one to_state.
* add to_state in increasing order of the state_no.
*/
void LT_tbl_entry_addToState(LT_tbl_entry * e, State * to)
{
if (to == NULL || e == NULL) return;
e->to_states = llist_int_add_inc(e->to_states, to->state_no);
}
/*
* Add an entry (from_state, to_state) to the LT_tbl,
* don't add the (config, context) information here.
*/
static void LT_tbl_entry_add(State * from, State * to)
{
LT_tbl_entry * e, * e_prev;
if (LT_tbl == NULL) {
LT_tbl = LT_tbl_entry_create(from, to);
return;
}
// search if the from state already exists.
for (e_prev = NULL, e = LT_tbl; e != NULL;
e_prev = e, e = e->next) {
if (e->from_state == from->state_no) {
LT_tbl_entry_addToState(e, to); // add to state if not on list.
return;
}
if (e->from_state > from->state_no) { // insert before e.
if (e_prev == NULL) { // insert as the head.
LT_tbl = LT_tbl_entry_create(from, to);
LT_tbl->next = e;
} else { // insert between e_prev and e
e_prev->next = LT_tbl_entry_create(from, to);
e_prev->next->next = e;
}
return;
}
}
// now is at the end of the table LT_tbl, add to list tail.
e_prev->next = LT_tbl_entry_create(from, to);
return;
}
/*
* Find from state in the LT_tbl.
* If not found, insert it.
*/
LT_tbl_entry * LT_tbl_entry_find_insert(State * from)
{
LT_tbl_entry * e, * e_prev;
if (NULL == LT_tbl) { //insert as the first
LT_tbl = LT_tbl_entry_create(from, NULL);
return LT_tbl;
}
for (e_prev = NULL, e = LT_tbl; e != NULL; e_prev = e, e = e->next) {
if (e->from_state == from->state_no) return e;
if (e->from_state > from->state_no) { // insert here.
if (e_prev == NULL) { // insert as the first.
LT_tbl = LT_tbl_entry_create(from, NULL);
LT_tbl->next = e;
return LT_tbl;
} else { // insert in the middle.
e_prev->next = LT_tbl_entry_create(from, NULL);
e_prev->next->next = e;
return e_prev->next;
}
}
// else, go on to check the next entry.
}
// otherwise, insert at the end.
e_prev->next = LT_tbl_entry_create(from, NULL);
return e_prev->next;
}
/*
* Find from state in the LT_tbl.
* Same as LT_tbl_entry_find_insert() except that this has no insert.
*
* There can be at most one entry found.
*/
LT_tbl_entry * LT_tbl_entry_find(State * from)
{
LT_tbl_entry * e, * e_prev;
if (NULL == LT_tbl) { //insert as the first
LT_tbl = LT_tbl_entry_create(from, NULL);
return LT_tbl;
}
for (e_prev = NULL, e = LT_tbl; e != NULL; e_prev = e, e = e->next) {
if (e->from_state == from->state_no) return e;
if (e->from_state > from->state_no) { // insert here.
if (e_prev == NULL) { // insert as the first.
LT_tbl = LT_tbl_entry_create(from, NULL);
LT_tbl->next = e;
return LT_tbl;
} else { // insert in the middle.
e_prev->next = LT_tbl_entry_create(from, NULL);
e_prev->next->next = e;
return e_prev->next;
}
}
// else, go on to check the next entry.
}
return NULL;
}
/*
* Find the cur_red_config in the LT_tbl_entry e.
* If not found, then insert it.
*/
static llist_context_set * llist_context_set_get(LT_tbl_entry * e)
{
llist_context_set * c, * c_prev;
if (NULL == e) {
printf("llist_context_set_get error: e is NULL\n");
exit(1);
}
if (e->ctxt_set == NULL) { // empty, insert as the first one.
e->ctxt_set = llist_context_set_create(cur_red_config);
return e->ctxt_set;
}
// else, try to find it in the list.
for (c_prev = NULL, c = e->ctxt_set; c != NULL;
c_prev = c, c = c->next) {
if (c->config == cur_red_config) return c; // found.
if (c->config->ruleID > cur_red_config->ruleID) { // insert here
if (c_prev == NULL) { // insert as the first
e->ctxt_set = llist_context_set_create(cur_red_config);
e->ctxt_set->next = c;
return e->ctxt_set;
} else { // insert in the middle.
c_prev->next = llist_context_set_create(cur_red_config);
c_prev->next->next = c;
return c_prev->next;
}
}
// else, go on to check the next.
}
// else, insert at the list tail.
c_prev->next = llist_context_set_create(cur_red_config);
return c_prev->next;
}
/*
* Add the (from_state, config, context)
* information to an entry in the LT_tbl.
*
* Note:
* 1) The from_state is unique for each entry.
* 2) The LT_tbl_entry_add function must have been called on the
* from_state before calling this function, so "from" state
* always exists.
*
* The current config is "cur_red_config" defined at the top.
*/
static void LT_tbl_entry_addContext(State * from, SymbolList ctxt)
{
LT_tbl_entry * e;
llist_context_set * c;
if (ctxt == NULL) return;
// 1) locate the LT_tbl_entry for "from" state.
e = LT_tbl_entry_find_insert(from);
if (NULL == e) {
printf("LT_tbl_entry_addContext error: state %d NOT found\n",
from->state_no);
exit(1);
}
// 2) locate the llist_context_set from the current config.
c = llist_context_set_get(e);
// 3) add/merge the context.
llist_context_set_addContext(c, ctxt);
}
static void dump_LT_tbl_entry(LT_tbl_entry * e)
{
if (NULL == e) return;
printf("%d \t| ", e->from_state);
// dump_config_context.
dump_llist_context_set(e->ctxt_set);
printf("\t| ");
llist_int_dump(e->to_states);
puts("");
}
void dump_LT_tbl()
{
LT_tbl_entry * e;
if (LT_tbl == NULL) return;
printf("FROM \t| CONFIG:{CONTEXT} | TO\n");
for (e = LT_tbl; e != NULL; e = e->next) {
dump_LT_tbl_entry(e);
}
}
/** END */
/************************************************************
* phase2_regeneration2 START.
*/
/*
* Return TRUE if a and b are disjoint, FALSE otherwise.
* Similar to the function hasEmptyIntersection() in y.c.
* This can be put into symbol_table.c.
*/
static BOOL symbolList_disjoint(SymbolList a, SymbolList b)
{
while (a != NULL && b != NULL) {
if (a->snode == b->snode) return FALSE;
if (strcmp(a->snode->symbol, b->snode->symbol) < 0) {
a = a->next;
} else {
b = b->next;
}
}
return TRUE;
}
/*
* Check the given context sets are pair_wise disjoint.
*/
static BOOL pairwise_disjoint(llist_context_set * ctxt_set)
{
llist_context_set * a, * b;
// if ctxt_set contains less than 2 nodes, return TRUE.
if (ctxt_set == NULL || ctxt_set->next == NULL) return TRUE;
for (a = ctxt_set; a->next != NULL; a = a->next) {
for (b = a->next; b != NULL; b = b->next) {
if (symbolList_disjoint(a->ctxt, b->ctxt) == FALSE) {
return FALSE;
}
}
}
return TRUE;
}
/*
* Create a new cluster and add it to the all_clusters set.
* e - the start state/LT_tbl_entry of this cluster.
*
* By default, the states element's n1 and n2 are the same.
*/
static LT_cluster * cluster_create(LT_tbl_entry * e)
{
LT_cluster * c;
HYY_NEW(c, LT_cluster, 1);
// by default, n1 and n2 are the same.
c->states = llist_int2_create(e->from_state, e->from_state);
c->ctxt_set = llist_context_set_clone(e->ctxt_set);
c->pairwise_disjoint = pairwise_disjoint(e->ctxt_set);
c->next = NULL;
if (c->pairwise_disjoint == FALSE) all_pairwise_disjoint = FALSE;
return c;
}
/*
* Not really neccesssary, but can be used to save running time space.
*/
static void cluster_destroy(LT_cluster * c)
{
llist_int2_destroy(c->states);
llist_context_set_destroy(c->ctxt_set);
free(c);
}
void cluster_dump(LT_cluster * c)
{
llist_int2 * n, * m;
llist_int * s;
LT_tbl_entry * e;
printf("states: \n");
for (n = c->states; n != NULL; n = n->next) {
printf("%d/%d [to: ", n->n1, n->n2);
if ((e = LT_tbl_findEntry(n->n1)) != NULL) {
for (s = e->to_states; s != NULL; s = s->next) {
m = llist_int2_find_n1(c->states, s->n);
printf("%d/%d ", s->n, (m == NULL) ? -1 : m->n2);
}
}
printf("]\n");
}
printf("context sets: ");
dump_llist_context_set(c->ctxt_set);
puts("");
}
static void all_clusters_dump()
{
LT_cluster * c;
puts("--all_clusters.START--");
for (c = all_clusters; c != NULL; c = c->next) {
cluster_dump(c);
}
puts("--END--");
}
/*
* Return:
* the splitted state's no if state_no is in c->states list
* -1 otherwise.
*
* Note state_no here is the virtual state_no: the one
* splitted from. So there could be more than one cluster
* contain it.
*/
int cluster_contain_state(LT_cluster * c, int state_no)
{
llist_int2 * s;
if (state_no < 0) return -1;
//printf("cluster_cotain_state(state_no: %d)\n", state_no);
if (c == NULL || c->states == NULL) return -1;
for (s = c->states; s != NULL; s = s->next) {
if (s->n1 == state_no) {
//puts("found");
return s->n2;
}
}
//puts("NOT found");
return -1;
}
/*
* Return:
* the splitted state's no if state_no is in c->states list
* -1 otherwise.
*
* Note state_no here is the actual state_no.
* There could be only one cluster contains it.
*/
int cluster_contain_actual_state(LT_cluster * c, int state_no)
{
llist_int2 * s;
if (state_no < 0) return -1;
//printf("cluster_cotain_state(state_no: %d)\n", state_no);
if (c == NULL || c->states == NULL) return -1;
for (s = c->states; s != NULL; s = s->next) {
if (s->n2 == state_no) {
//puts("found");
return s->n1;
}
}
//puts("NOT found");
return -1;
}
/*
* Combine the two chains dst and src:
* if src contains a llist_context_set node whose config is NOT in dst,
* add it in INC order.
* if src contains a llist_context_set node whose config is in dst,
* combine the context.
*/
llist_context_set * llist_context_set_mergeChain(
llist_context_set * dst, llist_context_set * src)
{
llist_context_set * a, * a_prev, * b;
int cmp;
if (src == NULL) return dst;
if (dst == NULL) return llist_context_set_clone(src);
b = src;
for (a_prev = NULL, a = dst; a != NULL; a_prev = a, a = a->next) {
while (b != NULL) {
cmp = a->config->ruleID - b->config->ruleID;
if (cmp == 0) { // same config, combine contexts.
llist_context_set_addContext(a, b->ctxt);
a_prev = a;
a = a->next;
b = b->next;
} else if (cmp < 0) {
a_prev = a;
a = a->next;
} else { // cmp > 0, insert b to dst before a.
if (a_prev == NULL) { // add to the head
a_prev = dst = llist_context_set_create(b->config);
llist_context_set_addContext(dst, b->ctxt);
dst->next = a;
} else { // add to the middle.
a_prev->next = llist_context_set_create(b->config);
llist_context_set_addContext(a_prev->next, b->ctxt);
a_prev->next->next = a;
}
b = b->next;
}
if (a == NULL) { break; }
} // end of while(b != NULL).
if (b == NULL || a == NULL) break;
}
if (b != NULL) { // clone b and its tail to the end of a_prev.
a_prev->next = llist_context_set_clone(b);
}
return dst;
}
OriginatorList * cloneOriginatorList(OriginatorList * o)
{
int i, ct;
OriginatorList * s;
HYY_NEW(s, OriginatorList, 1);
s->size = o->size;
s->count = o->count;
HYY_NEW(s->list, Configuration *, s->size);
for (i = 0, ct = s->count; i < ct; i ++) {
s->list[i] = o->list[i];
}
return s;
}
void copyConfig_LALR(Configuration * dst, Configuration * src)
{
if (dst == NULL || src == NULL) return;
dst->ruleID = src->ruleID;
dst->nMarker = src->nMarker;
dst->marker = src->marker;
copyContext(dst->context, src->context);
dst->owner = src->owner;
dst->isCoreConfig = src->isCoreConfig;
dst->COMPLETE = src->COMPLETE;
dst->IN_LANE = src->IN_LANE;
dst->ORIGINATOR_CHANGED = src->ORIGINATOR_CHANGED;
dst->LANE_END = src->LANE_END;
dst->LANE_CON = src->LANE_CON;
dst->originators = cloneOriginatorList(src->originators);
dst->transitors = cloneOriginatorList(src->transitors);
}
State * cloneState(State * s)
{
int i, ct;
State * t; // clone
HYY_NEW(t, State, 1);
t->next = s->next;
t->config_max_count = s->config_max_count;
HYY_NEW(t->config, Configuration *, t->config_max_count);
t->config_count = s->config_count;
t->core_config_count = s->core_config_count;
for (i = 0, ct = t->config_count; i < ct; i ++) {
t->config[i] = createConfig(-1, 0, 1);
copyConfig_LALR(t->config[i], s->config[i]);
t->config[i]->owner = t;
}
t->state_no = s->state_no;
t->trans_symbol = createSymbolNode(s->trans_symbol->snode);
t->successor_max_count = s->successor_max_count;
HYY_NEW(t->successor_list, State *, t->successor_max_count);
t->successor_count = s->successor_count;
for (i = 0, ct = t->successor_count; i < ct; i ++) {
t->successor_list[i] = s->successor_list[i];
}
t->parents_list = StateList_clone(s->parents_list);
t->ON_LANE = s->ON_LANE;
t->COMPLETE = s->COMPLETE;
t->PASS_THRU = s->PASS_THRU;
t->REGENERATED = s->REGENERATED;
return t;
}
/*
* In the successor list of src_state, replace s_old with s_new.
*/
void replaceSuccessor(State * src_state,
State * s_new, State * s_old)
{
int i, ct;
if (s_new == s_old) return;
#if DEBUG_PHASE_2_REGENERATE2
printf("replace successor of state %d: %d replaced by %d: ",
src_state->state_no, s_old->state_no, s_new->state_no);
#endif
for (i = 0, ct = src_state->successor_count; i < ct; i ++) {
if (src_state->successor_list[i] == s_old) {
src_state->successor_list[i] = s_new;
#if DEBUG_PHASE_2_REGENERATE2
puts("done");
#endif
return;
}
}
#if DEBUG_PHASE_2_REGENERATE2
puts("NOT done");
#endif
}
/*
* Used for LR(k) purpose only.
* In the lane_head_tail_pairs list, replace
* those entries whose tail config is s to s_copy
* according to cur_red_config.
*
* This solves this question: end_config is not a cur_red_config but
* belongs to a state in the middle of a lane? Using this method
* this is not a problem any more.
*
* Here it's called when a new state is splitted, but
* we replace ALL the pairs related to end_config->owner state.
*
* Algorithm:
* if (end_config->owner == s) {
* for each ConfigPairNode n {
* if (n->end_owner == s &&
* n->start is in the same cluster as s_copy) {
* replace n->end (the one in s) by the one in s_copy;
* }
* }
* }
*/
static void lane_head_tail_pairs_replace(LT_cluster * c,
Configuration * end_config, State * s, State * s_copy)
{
ConfigPairNode * n;
int i;
if (end_config->owner != s) return;
//printf("LHTPR: end_config: %d.%d\n",
// end_config->owner->state_no, end_config->ruleID);
for (n = lane_head_tail_pairs; n != NULL; n = n->next) {
// n->end->owner == end_config->owner.
// n->start->owner is a state in cluster c -- should I
// search as n1 or n2??? I think should be n2, so it
// covers the situation where n->start->owner is a
// splitted state.
if (n->end->owner == s &&
llist_int2_find_n2(c->states, n->start->owner->state_no) != NULL) {
// do replacement for n->end: from that in s to s_copy.