blob: a9e793a3e9b5ba6754658571aee899fef875619a [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
Guido van Rossum86bea461997-04-29 21:03:06 +000038extern int Py_DebugFlag;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Guido van Rossum3f5da241990-12-20 15:06:42 +000040/* Forward */
Guido van Rossum86bea461997-04-29 21:03:06 +000041static void calcfirstset Py_PROTO((grammar *, dfa *));
Guido van Rossum3f5da241990-12-20 15:06:42 +000042
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
Guido van Rossum86bea461997-04-29 21:03:06 +000075 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 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
Guido van Rossum86bea461997-04-29 21:03:06 +000094 sym = PyMem_NEW(int, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 if (sym == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000096 Py_FatalError("no mem for new sym in calcfirstset");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 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 */
Guido van Rossum86bea461997-04-29 21:03:06 +0000108 PyMem_RESIZE(sym, int, nsyms + 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 if (sym == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000110 Py_FatalError(
111 "no mem to resize sym in calcfirstset");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 sym[nsyms++] = a->a_lbl;
113 type = l0[a->a_lbl].lb_type;
114 if (ISNONTERMINAL(type)) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000115 d1 = PyGrammar_FindDFA(g, type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116 if (d1->d_first == dummy) {
117 fprintf(stderr,
118 "Left-recursion below '%s'\n",
119 d->d_name);
120 }
121 else {
122 if (d1->d_first == NULL)
123 calcfirstset(g, d1);
Guido van Rossum86bea461997-04-29 21:03:06 +0000124 mergebitset(result,
125 d1->d_first, nbits);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126 }
127 }
128 else if (ISTERMINAL(type)) {
129 addbit(result, a->a_lbl);
130 }
131 }
132 }
133 d->d_first = result;
Guido van Rossum86bea461997-04-29 21:03:06 +0000134 if (Py_DebugFlag) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135 printf("FIRST set for '%s': {", d->d_name);
136 for (i = 0; i < nbits; i++) {
137 if (testbit(result, i))
Guido van Rossum86bea461997-04-29 21:03:06 +0000138 printf(" %s", PyGrammar_LabelRepr(&l0[i]));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 }
140 printf(" }\n");
141 }
142}