1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Grammar implementation */
13 #include "pgenheaders.h"
21 extern int Py_DebugFlag
;
28 g
= PyMem_NEW(grammar
, 1);
30 Py_FatalError("no mem for new grammar");
34 g
->g_ll
.ll_nlabels
= 0;
35 g
->g_ll
.ll_label
= NULL
;
41 adddfa(grammar
*g
, int type
, char *name
)
45 PyMem_RESIZE(g
->g_dfa
, dfa
, g
->g_ndfas
+ 1);
47 Py_FatalError("no mem to resize dfa in adddfa");
48 d
= &g
->g_dfa
[g
->g_ndfas
++];
55 return d
; /* Only use while fresh! */
63 PyMem_RESIZE(d
->d_state
, state
, d
->d_nstates
+ 1);
64 if (d
->d_state
== NULL
)
65 Py_FatalError("no mem to resize state in addstate");
66 s
= &d
->d_state
[d
->d_nstates
++];
73 return s
- d
->d_state
;
77 addarc(dfa
*d
, int from
, int to
, int lbl
)
82 assert(0 <= from
&& from
< d
->d_nstates
);
83 assert(0 <= to
&& to
< d
->d_nstates
);
85 s
= &d
->d_state
[from
];
86 PyMem_RESIZE(s
->s_arc
, arc
, s
->s_narcs
+ 1);
88 Py_FatalError("no mem to resize arc list in addarc");
89 a
= &s
->s_arc
[s
->s_narcs
++];
95 addlabel(labellist
*ll
, int type
, char *str
)
100 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
101 if (ll
->ll_label
[i
].lb_type
== type
&&
102 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
105 PyMem_RESIZE(ll
->ll_label
, label
, ll
->ll_nlabels
+ 1);
106 if (ll
->ll_label
== NULL
)
107 Py_FatalError("no mem to resize labellist in addlabel");
108 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
110 lb
->lb_str
= str
; /* XXX strdup(str) ??? */
111 return lb
- ll
->ll_label
;
114 /* Same, but rather dies than adds */
117 findlabel(labellist
*ll
, int type
, char *str
)
121 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
122 if (ll
->ll_label
[i
].lb_type
== type
/*&&
123 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
126 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
127 Py_FatalError("grammar.c:findlabel()");
128 return 0; /* Make gcc -Wall happy */
132 static void translabel(grammar
*, label
*);
135 translatelabels(grammar
*g
)
140 printf("Translating labels ...\n");
142 /* Don't translate EMPTY */
143 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
144 translabel(g
, &g
->g_ll
.ll_label
[i
]);
148 translabel(grammar
*g
, label
*lb
)
153 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb
));
155 if (lb
->lb_type
== NAME
) {
156 for (i
= 0; i
< g
->g_ndfas
; i
++) {
157 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
160 "Label %s is non-terminal %d.\n",
163 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
168 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
169 if (strcmp(lb
->lb_str
, _PyParser_TokenNames
[i
]) == 0) {
171 printf("Label %s is terminal %d.\n",
178 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
182 if (lb
->lb_type
== STRING
) {
183 if (isalpha((int)(lb
->lb_str
[1])) || lb
->lb_str
[1] == '_') {
186 printf("Label %s is a keyword\n", lb
->lb_str
);
189 p
= strchr(lb
->lb_str
, '\'');
193 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
194 int type
= (int) PyToken_OneChar(lb
->lb_str
[1]);
200 printf("Unknown OP label %s\n",
203 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
204 int type
= (int) PyToken_TwoChars(lb
->lb_str
[1],
211 printf("Unknown OP label %s\n",
215 printf("Can't translate STRING label %s\n",
219 printf("Can't translate label '%s'\n",
220 PyGrammar_LabelRepr(lb
));