blob: 5c2a872516d4cc29c35183f1e3fbfcfdcc945a47 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Computation of FIRST stets */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035#include "grammar.h"
36#include "token.h"
37
38extern int debugging;
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040/* Forward */
41static void calcfirstset PROTO((grammar *, dfa *));
42
43void
44addfirstsets(g)
45 grammar *g;
46{
47 int i;
48 dfa *d;
49
50 printf("Adding FIRST sets ...\n");
51 for (i = 0; i < g->g_ndfas; i++) {
52 d = &g->g_dfa[i];
53 if (d->d_first == NULL)
54 calcfirstset(g, d);
55 }
56}
57
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058static void
59calcfirstset(g, d)
60 grammar *g;
61 dfa *d;
62{
63 int i, j;
64 state *s;
65 arc *a;
66 int nsyms;
67 int *sym;
68 int nbits;
69 static bitset dummy;
70 bitset result;
71 int type;
72 dfa *d1;
73 label *l0;
74
75 if (debugging)
76 printf("Calculate FIRST set for '%s'\n", d->d_name);
77
78 if (dummy == NULL)
79 dummy = newbitset(1);
80 if (d->d_first == dummy) {
81 fprintf(stderr, "Left-recursion for '%s'\n", d->d_name);
82 return;
83 }
84 if (d->d_first != NULL) {
85 fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n",
86 d->d_name);
87 }
88 d->d_first = dummy;
89
90 l0 = g->g_ll.ll_label;
91 nbits = g->g_ll.ll_nlabels;
92 result = newbitset(nbits);
93
94 sym = NEW(int, 1);
95 if (sym == NULL)
96 fatal("no mem for new sym in calcfirstset");
97 nsyms = 1;
98 sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL);
99
100 s = &d->d_state[d->d_initial];
101 for (i = 0; i < s->s_narcs; i++) {
102 a = &s->s_arc[i];
103 for (j = 0; j < nsyms; j++) {
104 if (sym[j] == a->a_lbl)
105 break;
106 }
107 if (j >= nsyms) { /* New label */
108 RESIZE(sym, int, nsyms + 1);
109 if (sym == NULL)
110 fatal("no mem to resize sym in calcfirstset");
111 sym[nsyms++] = a->a_lbl;
112 type = l0[a->a_lbl].lb_type;
113 if (ISNONTERMINAL(type)) {
114 d1 = finddfa(g, type);
115 if (d1->d_first == dummy) {
116 fprintf(stderr,
117 "Left-recursion below '%s'\n",
118 d->d_name);
119 }
120 else {
121 if (d1->d_first == NULL)
122 calcfirstset(g, d1);
123 mergebitset(result, d1->d_first, nbits);
124 }
125 }
126 else if (ISTERMINAL(type)) {
127 addbit(result, a->a_lbl);
128 }
129 }
130 }
131 d->d_first = result;
132 if (debugging) {
133 printf("FIRST set for '%s': {", d->d_name);
134 for (i = 0; i < nbits; i++) {
135 if (testbit(result, i))
136 printf(" %s", labelrepr(&l0[i]));
137 }
138 printf(" }\n");
139 }
140}