1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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 or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Computation of FIRST stets */
34 #include "pgenheaders.h"
38 extern int Py_DebugFlag
;
41 static void calcfirstset
Py_PROTO((grammar
*, dfa
*));
50 printf("Adding FIRST sets ...\n");
51 for (i
= 0; i
< g
->g_ndfas
; i
++) {
53 if (d
->d_first
== NULL
)
76 printf("Calculate FIRST set for '%s'\n", d
->d_name
);
80 if (d
->d_first
== dummy
) {
81 fprintf(stderr
, "Left-recursion for '%s'\n", d
->d_name
);
84 if (d
->d_first
!= NULL
) {
85 fprintf(stderr
, "Re-calculating FIRST set for '%s' ???\n",
90 l0
= g
->g_ll
.ll_label
;
91 nbits
= g
->g_ll
.ll_nlabels
;
92 result
= newbitset(nbits
);
94 sym
= PyMem_NEW(int, 1);
96 Py_FatalError("no mem for new sym in calcfirstset");
98 sym
[0] = findlabel(&g
->g_ll
, d
->d_type
, (char *)NULL
);
100 s
= &d
->d_state
[d
->d_initial
];
101 for (i
= 0; i
< s
->s_narcs
; i
++) {
103 for (j
= 0; j
< nsyms
; j
++) {
104 if (sym
[j
] == a
->a_lbl
)
107 if (j
>= nsyms
) { /* New label */
108 PyMem_RESIZE(sym
, int, nsyms
+ 1);
111 "no mem to resize sym in calcfirstset");
112 sym
[nsyms
++] = a
->a_lbl
;
113 type
= l0
[a
->a_lbl
].lb_type
;
114 if (ISNONTERMINAL(type
)) {
115 d1
= PyGrammar_FindDFA(g
, type
);
116 if (d1
->d_first
== dummy
) {
118 "Left-recursion below '%s'\n",
122 if (d1
->d_first
== NULL
)
128 else if (ISTERMINAL(type
)) {
129 addbit(result
, a
->a_lbl
);
135 printf("FIRST set for '%s': {", d
->d_name
);
136 for (i
= 0; i
< nbits
; i
++) {
137 if (testbit(result
, i
))
138 printf(" %s", PyGrammar_LabelRepr(&l0
[i
]));