1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Computation of FIRST stets */
27 #include "pgenheaders.h"
34 static void calcfirstset
PROTO((grammar
*, dfa
*));
43 printf("Adding FIRST sets ...\n");
44 for (i
= 0; i
< g
->g_ndfas
; i
++) {
46 if (d
->d_first
== NULL
)
69 printf("Calculate FIRST set for '%s'\n", d
->d_name
);
73 if (d
->d_first
== dummy
) {
74 fprintf(stderr
, "Left-recursion for '%s'\n", d
->d_name
);
77 if (d
->d_first
!= NULL
) {
78 fprintf(stderr
, "Re-calculating FIRST set for '%s' ???\n",
83 l0
= g
->g_ll
.ll_label
;
84 nbits
= g
->g_ll
.ll_nlabels
;
85 result
= newbitset(nbits
);
89 fatal("no mem for new sym in calcfirstset");
91 sym
[0] = findlabel(&g
->g_ll
, d
->d_type
, (char *)NULL
);
93 s
= &d
->d_state
[d
->d_initial
];
94 for (i
= 0; i
< s
->s_narcs
; i
++) {
96 for (j
= 0; j
< nsyms
; j
++) {
97 if (sym
[j
] == a
->a_lbl
)
100 if (j
>= nsyms
) { /* New label */
101 RESIZE(sym
, int, nsyms
+ 1);
103 fatal("no mem to resize sym in calcfirstset");
104 sym
[nsyms
++] = a
->a_lbl
;
105 type
= l0
[a
->a_lbl
].lb_type
;
106 if (ISNONTERMINAL(type
)) {
107 d1
= finddfa(g
, type
);
108 if (d1
->d_first
== dummy
) {
110 "Left-recursion below '%s'\n",
114 if (d1
->d_first
== NULL
)
116 mergebitset(result
, d1
->d_first
, nbits
);
119 else if (ISTERMINAL(type
)) {
120 addbit(result
, a
->a_lbl
);
126 printf("FIRST set for '%s': {", d
->d_name
);
127 for (i
= 0; i
< nbits
; i
++) {
128 if (testbit(result
, i
))
129 printf(" %s", labelrepr(&l0
[i
]));