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 /* Grammar implementation */
34 #include "pgenheaders.h"
42 extern int Py_DebugFlag
;
50 g
= PyMem_NEW(grammar
, 1);
52 Py_FatalError("no mem for new grammar");
56 g
->g_ll
.ll_nlabels
= 0;
57 g
->g_ll
.ll_label
= NULL
;
70 PyMem_RESIZE(g
->g_dfa
, dfa
, g
->g_ndfas
+ 1);
72 Py_FatalError("no mem to resize dfa in adddfa");
73 d
= &g
->g_dfa
[g
->g_ndfas
++];
80 return d
; /* Only use while fresh! */
89 PyMem_RESIZE(d
->d_state
, state
, d
->d_nstates
+ 1);
90 if (d
->d_state
== NULL
)
91 Py_FatalError("no mem to resize state in addstate");
92 s
= &d
->d_state
[d
->d_nstates
++];
99 return s
- d
->d_state
;
103 addarc(d
, from
, to
, lbl
)
110 assert(0 <= from
&& from
< d
->d_nstates
);
111 assert(0 <= to
&& to
< d
->d_nstates
);
113 s
= &d
->d_state
[from
];
114 PyMem_RESIZE(s
->s_arc
, arc
, s
->s_narcs
+ 1);
115 if (s
->s_arc
== NULL
)
116 Py_FatalError("no mem to resize arc list in addarc");
117 a
= &s
->s_arc
[s
->s_narcs
++];
123 addlabel(ll
, type
, str
)
131 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
132 if (ll
->ll_label
[i
].lb_type
== type
&&
133 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
136 PyMem_RESIZE(ll
->ll_label
, label
, ll
->ll_nlabels
+ 1);
137 if (ll
->ll_label
== NULL
)
138 Py_FatalError("no mem to resize labellist in addlabel");
139 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
141 lb
->lb_str
= str
; /* XXX strdup(str) ??? */
142 return lb
- ll
->ll_label
;
145 /* Same, but rather dies than adds */
148 findlabel(ll
, type
, str
)
155 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
156 if (ll
->ll_label
[i
].lb_type
== type
/*&&
157 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
160 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
161 Py_FatalError("grammar.c:findlabel()");
162 return 0; /* Make gcc -Wall happy */
166 static void translabel
Py_PROTO((grammar
*, label
*));
175 printf("Translating labels ...\n");
177 /* Don't translate EMPTY */
178 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
179 translabel(g
, &g
->g_ll
.ll_label
[i
]);
190 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb
));
192 if (lb
->lb_type
== NAME
) {
193 for (i
= 0; i
< g
->g_ndfas
; i
++) {
194 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
197 "Label %s is non-terminal %d.\n",
200 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
205 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
206 if (strcmp(lb
->lb_str
, _PyParser_TokenNames
[i
]) == 0) {
208 printf("Label %s is terminal %d.\n",
215 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
219 if (lb
->lb_type
== STRING
) {
220 if (isalpha((int)(lb
->lb_str
[1])) || lb
->lb_str
[1] == '_') {
223 printf("Label %s is a keyword\n", lb
->lb_str
);
226 p
= strchr(lb
->lb_str
, '\'');
230 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
231 int type
= (int) PyToken_OneChar(lb
->lb_str
[1]);
237 printf("Unknown OP label %s\n",
240 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
241 int type
= (int) PyToken_TwoChars(lb
->lb_str
[1],
248 printf("Unknown OP label %s\n",
252 printf("Can't translate STRING label %s\n",
256 printf("Can't translate label '%s'\n",
257 PyGrammar_LabelRepr(lb
));