-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsub2.c
1225 lines (1187 loc) · 24.2 KB
/
sub2.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc.
* All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/* from OpenSolaris "sub2.c 6.15 05/06/08 SMI" */
/*
* Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
*
* Sccsid @(#)sub2.c 1.7 (gritter) 01/12/07
*/
#include "ldefs.h"
static void add(int **array, int n);
static void follow(int v);
static void first(int v);
static void nextstate(int s, int c);
static void packtrans(int st, CHR *tch, int *tst, int cnt, int tryit);
static void acompute(int s);
static void rprint(int *a, char *s, int n);
static void shiftr(int *a, int n);
static void upone(int *a, int n);
static void bprint(char *a, char *s, int n);
static int notin(int n);
static int member(int d, CHR *t);
#ifdef PP
static void padd(int **array, int n);
#endif
void
cfoll(int v)
{
int i, j, k;
CHR *p;
i = name[v];
if (!ISOPERATOR(i))
i = 1;
switch (i) {
case 1: case RSTR: case RCCL: case RNCCL: case RNULLS:
for (j = 0; j < tptr; j++)
tmpstat[j] = FALSE;
count = 0;
follow(v);
#ifdef PP
padd(foll, v); /* packing version */
#else
add(foll, v); /* no packing version */
#endif
if (i == RSTR)
cfoll(left[v]);
else if (i == RCCL || i == RNCCL) {
for (j = 1; j < ncg; j++)
symbol[j] = (i == RNCCL);
p = (CHR *) left[v];
while (*p)
symbol[*p++] = (i == RCCL);
p = pcptr;
for (j = 1; j < ncg; j++)
if (symbol[j]) {
for (k = 0; p + k < pcptr; k++)
if (cindex[j] == *(p + k))
break;
if (p + k >= pcptr)
*pcptr++ = cindex[j];
}
*pcptr++ = 0;
if (pcptr > pchar + pchlen)
error(
"Too many packed character classes");
left[v] = (intptr_t)p;
name[v] = RCCL; /* RNCCL eliminated */
#ifdef DEBUG
if (debug && *p) {
printf("ccl %d: %d", v, *p++);
while (*p)
printf(", %d", *p++);
putchar('\n');
}
#endif
}
break;
case CARAT:
cfoll(left[v]);
break;
/* XCU4: add RXSCON */
case RXSCON:
case STAR: case PLUS: case QUEST: case RSCON:
cfoll(left[v]);
break;
case BAR: case RCAT: case DIV: case RNEWE:
cfoll(left[v]);
cfoll(right[v]);
break;
#ifdef DEBUG
case FINAL:
case S1FINAL:
case S2FINAL:
break;
default:
warning("bad switch cfoll %d", v);
#endif
}
}
#ifdef DEBUG
void
pfoll(void)
{
int i, k, *p;
int j;
/* print sets of chars which may follow positions */
printf("pos\tchars\n");
for (i = 0; i < tptr; i++)
if (p = foll[i]) {
j = *p++;
if (j >= 1) {
printf("%d:\t%d", i, *p++);
for (k = 2; k <= j; k++)
printf(", %d", *p++);
putchar('\n');
}
}
}
#endif
static void
add(int **array, int n)
{
int i, *temp;
CHR *ctemp;
temp = nxtpos;
ctemp = tmpstat;
array[n] = nxtpos; /* note no packing is done in positions */
*temp++ = count;
for (i = 0; i < tptr; i++)
if (ctemp[i] == TRUE)
*temp++ = i;
nxtpos = temp;
if (nxtpos >= positions+maxpos)
error(
"Too many positions %s",
(maxpos == MAXPOS ? "\nTry using %p num" : ""));
}
static void
follow(int v)
{
int p;
if (v >= tptr-1)
return;
p = parent[v];
if (p == 0)
return;
switch (name[p]) {
/* will not be CHAR RNULLS FINAL S1FINAL S2FINAL RCCL RNCCL */
case RSTR:
if (tmpstat[p] == FALSE) {
count++;
tmpstat[p] = TRUE;
}
break;
case STAR: case PLUS:
first(v);
follow(p);
break;
case BAR: case QUEST: case RNEWE:
follow(p);
break;
case RCAT: case DIV:
if (v == left[p]) {
if (nullstr[right[p]])
follow(p);
first(right[p]);
}
else
follow(p);
break;
/* XCU4: add RXSCON */
case RXSCON:
case RSCON: case CARAT:
follow(p);
break;
#ifdef DEBUG
default:
warning("bad switch follow %d", p);
#endif
}
}
/*
* Check if I have a RXSCON in my upper node
*/
static int
check_me(int v)
{
int tmp = parent[v];
while (name[tmp] != RNEWE) {
if (name[tmp] == RXSCON)
return (1);
tmp = parent[tmp];
}
return (0);
}
/* calculate set of positions with v as root which can be active initially */
static void
first(int v)
{
int i;
CHR *p;
i = name[v];
if (!ISOPERATOR(i))
i = 1;
switch (i) {
case 1: case RCCL: case RNCCL:
case RNULLS: case FINAL:
case S1FINAL: case S2FINAL:
/*
* XCU4: if we are working on an exclusive start state and
* the parent of this position is not RXSCON or RSTR this
* is not an active position.
*
* (There is a possibility that RSXCON appreas as the
* (parent)* node. Check it by check_me().)
*/
if ((exclusive[stnum/2]) &&
ISOPERATOR(name[parent[v]]) &&
(name[parent[v]] != RXSCON) &&
(name[parent[v]] != RSTR) &&
(check_me(v) == 0)) {
break;
}
if (tmpstat[v] == FALSE) {
count++;
tmpstat[v] = TRUE;
}
break;
case BAR: case RNEWE:
first(left[v]);
first(right[v]);
break;
case CARAT:
if (stnum % 2 == 1)
first(left[v]);
break;
/* XCU4: add RXSCON */
case RXSCON:
case RSCON:
i = stnum/2 +1;
p = (CHR *) right[v];
while (*p)
if (*p++ == i) {
first(left[v]);
break;
}
break;
case STAR: case QUEST:
case PLUS: case RSTR:
/*
* XCU4: if we are working on an exclusive start state and
* the parent of this position is not RXSCON or RSTR this
* is not an active position.
*
* (There is a possibility that RSXCON appreas as the
* (parent)* node. Check it by check_me().)
*/
if ((exclusive[stnum/2]) &&
ISOPERATOR(name[parent[v]]) &&
(name[parent[v]] != RXSCON) &&
(name[parent[v]] != RSTR) &&
(check_me(v) == 0)) {
break;
}
first(left[v]);
break;
case RCAT: case DIV:
first(left[v]);
if (nullstr[left[v]])
first(right[v]);
break;
#ifdef DEBUG
default:
warning("bad switch first %d", v);
#endif
}
}
void
cgoto(void)
{
int i, j;
static int s;
int npos, curpos, n;
int tryit;
CHR tch[MAXNCG];
int tst[MAXNCG];
CHR *q;
/* generate initial state, for each start condition */
if (ratfor) {
fprintf(fout,
"blockdata\n"
"common /Lvstop/ vstop\n"
"define Svstop %d\n"
"integer vstop(Svstop)\n"
, nstates+1);
} else
fprintf(fout, "int yyvstop[] = {\n0,\n");
while (stnum < 2 || stnum/2 < sptr) {
for (i = 0; i < tptr; i++)
tmpstat[i] = 0;
count = 0;
if (tptr > 0)
first(tptr-1);
add(state, stnum);
#ifdef DEBUG
if (debug) {
if (stnum > 1)
printf("%ls:\n", sname[stnum/2]);
pstate(stnum);
}
#endif
stnum++;
}
stnum--;
/* even stnum = might not be at line begin */
/* odd stnum = must be at line begin */
/* even states can occur anywhere, odd states only at line begin */
for (s = 0; s <= stnum; s++) {
tryit = FALSE;
cpackflg[s] = FALSE;
sfall[s] = -1;
acompute(s);
for (i = 0; i < ncg; i++)
symbol[i] = 0;
npos = *state[s];
for (i = 1; i <= npos; i++) {
curpos = *(state[s]+i);
if (!ISOPERATOR(name[curpos]))
symbol[name[curpos]] = TRUE;
else {
switch (name[curpos]) {
case RCCL:
tryit = TRUE;
q = (CHR *)left[curpos];
while (*q) {
for (j = 1; j < ncg; j++)
if (cindex[j] == *q)
symbol[j] = TRUE;
q++;
}
break;
case RSTR:
symbol[right[curpos]] = TRUE;
break;
#ifdef DEBUG
case RNULLS:
case FINAL:
case S1FINAL:
case S2FINAL:
break;
default:
warning(
"bad switch cgoto %d state %d",
curpos, s);
break;
#endif
}
}
}
#ifdef DEBUG
if (debug) {
printf("State %d transitions on char-group {", s);
charc = 0;
for (i = 1; i < ncg; i++) {
if (symbol[i]) {
printf("%d,", i);
}
if (i == ncg-1)
printf("}\n");
if (charc > LINESIZE/4) {
charc = 0;
printf("\n\t");
}
}
}
#endif
/* for each char, calculate next state */
n = 0;
for (i = 1; i < ncg; i++) {
if (symbol[i]) {
/* executed for each state, transition pair */
nextstate(s, i);
xstate = notin(stnum);
if (xstate == -2)
warning("bad state %d %o", s, i);
else if (xstate == -1) {
if (stnum+1 >= nstates) {
stnum++;
error("Too many states %s",
(nstates == NSTATES ?
"\nTry using %n num":""));
}
add(state, ++stnum);
#ifdef DEBUG
if (debug)
pstate(stnum);
#endif
tch[n] = i;
tst[n++] = stnum;
} else { /* xstate >= 0 ==> state exists */
tch[n] = i;
tst[n++] = xstate;
}
}
}
tch[n] = 0;
tst[n] = -1;
/* pack transitions into permanent array */
if (n > 0)
packtrans(s, tch, tst, n, tryit);
else
gotof[s] = -1;
}
ratfor ? fprintf(fout, "end\n") : fprintf(fout, "0};\n");
}
/*
* Beware -- 70% of total CPU time is spent in this subroutine -
* if you don't believe me - try it yourself !
*/
static void
nextstate(int s, int c)
{
int j, *newpos;
CHR *temp, *tz;
int *pos, i, *f, num, curpos, number;
/* state to goto from state s on char c */
num = *state[s];
temp = tmpstat;
pos = state[s] + 1;
for (i = 0; i < num; i++) {
curpos = *pos++;
j = name[curpos];
if (((!ISOPERATOR(j)) && j == c) ||
(j == RSTR && c == right[curpos]) ||
(j == RCCL && member(c, (CHR *) left[curpos]))) {
f = foll[curpos];
number = *f;
newpos = f+1;
for (j = 0; j < number; j++)
temp[*newpos++] = 2;
}
}
j = 0;
tz = temp + tptr;
while (temp < tz) {
if (*temp == 2) {
j++;
*temp++ = 1;
}
else
*temp++ = 0;
}
count = j;
}
static int
notin(int n)
{ /* see if tmpstat occurs previously */
int *j, k;
CHR *temp;
int i;
if (count == 0)
return (-2);
temp = tmpstat;
for (i = n; i >= 0; i--) { /* for each state */
j = state[i];
if (count == *j++) {
for (k = 0; k < count; k++)
if (!temp[*j++])
break;
if (k >= count)
return (i);
}
}
return (-1);
}
static void
packtrans(int st, CHR *tch, int *tst, int cnt, int tryit)
{
/*
* pack transitions into nchar, nexts
* nchar is terminated by '\0', nexts uses cnt, followed by elements
* gotof[st] = index into nchr, nexts for state st
* sfall[st] = t implies t is fall back state for st
* == -1 implies no fall back
*/
int cmin, cval, tcnt, diff, p, *ast;
int i, j, k;
CHR *ach;
int go[MAXNCG], temp[MAXNCG], index, c;
int swork[MAXNCG];
CHR cwork[MAXNCG];
int upper;
rcount += (long)cnt;
cmin = -1;
cval = ncg;
ast = tst;
ach = tch;
/* try to pack transitions using ccl's */
if (!optim)
goto nopack; /* skip all compaction */
if (tryit) { /* ccl's used */
for (i = 1; i < ncg; i++) {
go[i] = temp[i] = -1;
symbol[i] = 1;
}
for (i = 0; i < cnt; i++) {
index = (unsigned char) tch[i];
if ((index >= 0) && (index < NCH)) {
go[index] = tst[i];
symbol[index] = 0;
} else {
fprintf(stderr,
"lex`sub2`packtran: tch[%d] out of bounds (%ld)\n",
i, (long)tch[i]);
}
}
for (i = 0; i < cnt; i++) {
c = match[tch[i]];
if (go[c] != tst[i] || c == tch[i])
temp[tch[i]] = tst[i];
}
/* fill in error entries */
for (i = 1; i < ncg; i++)
if (symbol[i])
temp[i] = -2; /* error trans */
/* count them */
k = 0;
for (i = 1; i < ncg; i++)
if (temp[i] != -1)
k++;
if (k < cnt) { /* compress by char */
#ifdef DEBUG
if (debug)
printf(
"use compression %d, %d vs %d\n", st, k, cnt);
#endif
k = 0;
for (i = 1; i < ncg; i++)
if (temp[i] != -1) {
cwork[k] = i;
swork[k++] =
(temp[i] == -2 ? -1 : temp[i]);
}
cwork[k] = 0;
#ifdef PC
ach = cwork;
ast = swork;
cnt = k;
cpackflg[st] = TRUE;
#endif
}
}
/*
* get most similar state
* reject state with more transitions,
* state already represented by a third state,
* and state which is compressed by char if ours is not to be
*/
for (i = 0; i < st; i++) {
if (sfall[i] != -1)
continue;
if (cpackflg[st] == 1)
if (!(cpackflg[i] == 1))
continue;
p = gotof[i];
if (p == -1) /* no transitions */
continue;
tcnt = nexts[p];
if (tcnt > cnt)
continue;
diff = 0;
k = 0;
j = 0;
upper = p + tcnt;
while (ach[j] && p < upper) {
while (ach[j] < nchar[p] && ach[j]) {
diff++;
j++;
}
if (ach[j] == 0)
break;
if (ach[j] > nchar[p]) {
diff = ncg;
break;
}
/* ach[j] == nchar[p] */
if (ast[j] != nexts[++p] ||
ast[j] == -1 ||
(cpackflg[st] && ach[j] != match[ach[j]]))
diff++;
j++;
}
while (ach[j]) {
diff++;
j++;
}
if (p < upper)
diff = ncg;
if (diff < cval && diff < tcnt) {
cval = diff;
cmin = i;
if (cval == 0)
break;
}
}
/* cmin = state "most like" state st */
#ifdef DEBUG
if (debug)
printf("select st %d for st %d diff %d\n",
cmin, st, cval);
#endif
#ifdef PS
if (cmin != -1) { /* if we can use st cmin */
gotof[st] = nptr;
k = 0;
sfall[st] = cmin;
p = gotof[cmin] + 1;
j = 0;
while (ach[j]) {
/* if cmin has a transition on c, then so will st */
/* st may be "larger" than cmin, however */
while (ach[j] < nchar[p-1] && ach[j]) {
k++;
nchar[nptr] = ach[j];
nexts[++nptr] = ast[j];
j++;
}
if (nchar[p-1] == 0)
break;
if (ach[j] > nchar[p-1]) {
warning("bad transition %d %d", st, cmin);
goto nopack;
}
/* ach[j] == nchar[p-1] */
if (ast[j] != nexts[p] ||
ast[j] == -1 ||
(cpackflg[st] && ach[j] != match[ach[j]])) {
k++;
nchar[nptr] = ach[j];
nexts[++nptr] = ast[j];
}
p++;
j++;
}
while (ach[j]) {
nchar[nptr] = ach[j];
nexts[++nptr] = ast[j++];
k++;
}
nexts[gotof[st]] = cnt = k;
nchar[nptr++] = 0;
} else {
#endif
nopack:
/* stick it in */
gotof[st] = nptr;
nexts[nptr] = cnt;
for (i = 0; i < cnt; i++) {
nchar[nptr] = ach[i];
nexts[++nptr] = ast[i];
}
nchar[nptr++] = 0;
#ifdef PS
}
#endif
if (cnt < 1) {
gotof[st] = -1;
nptr--;
} else
if (nptr > ntrans)
error(
"Too many transitions %s",
(ntrans == NTRANS ? "\nTry using %a num" : ""));
}
#ifdef DEBUG
void
pstate(int s)
{
int *p, i, j;
printf("State %d:\n", s);
p = state[s];
i = *p++;
if (i == 0)
return;
printf("%4d", *p++);
for (j = 1; j < i; j++) {
printf(", %4d", *p++);
if (j%30 == 0)
putchar('\n');
}
putchar('\n');
}
#endif
static int
member(int d, CHR *t)
{
int c;
CHR *s;
c = d;
s = t;
c = cindex[c];
while (*s)
if (*s++ == c)
return (1);
return (0);
}
#ifdef DEBUG
void
stprt(int i)
{
int p, t;
printf("State %d:", i);
/* print actions, if any */
t = atable[i];
if (t != -1)
printf(" final");
putchar('\n');
if (cpackflg[i] == TRUE)
printf("backup char in use\n");
if (sfall[i] != -1)
printf("fall back state %d\n", sfall[i]);
p = gotof[i];
if (p == -1)
return;
printf("(%d transitions)\n", nexts[p]);
while (nchar[p]) {
charc = 0;
if (nexts[p+1] >= 0)
printf("%d\t", nexts[p+1]);
else
printf("err\t");
allprint(nchar[p++]);
while (nexts[p] == nexts[p+1] && nchar[p]) {
if (charc > LINESIZE) {
charc = 0;
printf("\n\t");
}
allprint(nchar[p++]);
}
putchar('\n');
}
putchar('\n');
}
#endif
/* compute action list = set of poss. actions */
static void
acompute(int s)
{
int *p, i, j;
int q, r;
int cnt, m;
int temp[MAXPOSSTATE], k, neg[MAXPOSSTATE], n;
k = 0;
n = 0;
p = state[s];
cnt = *p++;
if (cnt > MAXPOSSTATE)
error("Too many positions for one state - acompute");
for (i = 0; i < cnt; i++) {
q = *p;
if (name[q] == FINAL)
temp[k++] = left[q];
else if (name[q] == S1FINAL) {
temp[k++] = left[q];
if ((r = left[q]) >= NACTIONS)
error(
"INTERNAL ERROR:left[%d]==%d>=NACTIONS", q, r);
extra[r] = 1;
} else if (name[q] == S2FINAL)
neg[n++] = left[q];
p++;
}
atable[s] = -1;
if (k < 1 && n < 1)
return;
#ifdef DEBUG
if (debug)
printf("final %d actions:", s);
#endif
/* sort action list */
for (i = 0; i < k; i++)
for (j = i+1; j < k; j++)
if (temp[j] < temp[i]) {
m = temp[j];
temp[j] = temp[i];
temp[i] = m;
}
/* remove dups */
for (i = 0; i < k-1; i++)
if (temp[i] == temp[i+1])
temp[i] = 0;
/* copy to permanent quarters */
atable[s] = aptr;
#ifdef DEBUG
if (!ratfor)
fprintf(fout, "/* actions for state %d */", s);
#endif
putc('\n', fout);
for (i = 0; i < k; i++)
if (temp[i] != 0) {
ratfor ?
fprintf(fout, "data vstop(%d)/%d/\n", aptr, temp[i]) :
fprintf(fout, "%d,\n", temp[i]);
#ifdef DEBUG
if (debug)
printf("%d ", temp[i]);
#endif
aptr++;
}
for (i = 0; i < n; i++) { /* copy fall back actions - all neg */
ratfor ?
fprintf(fout, "data vstop(%d)/%d/\n", aptr, neg[i]) :
fprintf(fout, "%d,\n", neg[i]);
aptr++;
#ifdef DEBUG
if (debug)
printf("%d ", neg[i]);
#endif
}
#ifdef DEBUG
if (debug)
putchar('\n');
#endif
ratfor ? fprintf(fout, "data vstop (%d)/0/\n", aptr) :
fprintf(fout, "0, \n");
aptr++;
}
#ifdef DEBUG
void
pccl(void)
{
/* print character class sets */
int i, j;
printf("char class intersection\n");
for (i = 0; i < ccount; i++) {
charc = 0;
printf("class %d:\n\t", i);
for (j = 1; j < ncg; j++)
if (cindex[j] == i) {
allprint(j);
if (charc > LINESIZE) {
printf("\n\t");
charc = 0;
}
}
putchar('\n');
}
charc = 0;
printf("match:\n");
for (i = 0; i < ncg; i++) {
allprint(match[i]);
if (charc > LINESIZE) {
putchar('\n');
charc = 0;
}
}
putchar('\n');
}
#endif
void
mkmatch(void)
{
int i;
CHR tab[MAXNCG];
for (i = 0; i < ccount; i++)
tab[i] = 0;
for (i = 1; i < ncg; i++)
if (tab[cindex[i]] == 0)
tab[cindex[i]] = i;
/* tab[i] = principal char for new ccl i */
for (i = 1; i < ncg; i++)
match[i] = tab[cindex[i]];
}
void
layout(void)
{
/* format and output final program's tables */
int i, j, k;
int top, bot, startup, omin;
startup = 0;
for (i = 0; i < outsize; i++)
verify[i] = advance[i] = 0;
omin = 0;
yytop = 0;
for (i = 0; i <= stnum; i++) { /* for each state */
j = gotof[i];
if (j == -1) {
stoff[i] = 0;
continue;
}
bot = j;
while (nchar[j])
j++;
top = j - 1;
#if DEBUG
if (debug) {
printf("State %d: (layout)\n", i);
for (j = bot; j <= top; j++) {
printf(" %o", nchar[j]);
if (j % 10 == 0)
putchar('\n');
}
putchar('\n');
}
#endif
while (verify[omin+ZCH])
omin++;
startup = omin;
#if DEBUG
if (debug)
printf(
"bot,top %d, %d startup begins %d\n",
bot, top, startup);
#endif
if (chset) {
do {
startup += 1;
if (startup > outsize - ZCH)
error("output table overflow");
for (j = bot; j <= top; j++) {
k = startup+ctable[nchar[j]];
if (verify[k])
break;
}
} while (j <= top);
#if DEBUG
if (debug)
printf(" startup will be %d\n", startup);
#endif
/* have found place */
for (j = bot; j <= top; j++) {
k = startup + ctable[nchar[j]];
if (ctable[nchar[j]] <= 0)
printf(
"j %d nchar %ld ctable.nch %d\n",
j, (long)nchar[j], ctable[nchar[k]]);
verify[k] = i + 1; /* state number + 1 */
advance[k] = nexts[j+1]+1;
if (yytop < k)
yytop = k;
}
} else {
do {
startup += 1;
if (startup > outsize - ZCH)
error("output table overflow");
for (j = bot; j <= top; j++) {
k = startup + nchar[j];