2 /* Grammar implementation */
5 #include "pgenheaders.h"
16 extern int Py_DebugFlag
;
23 g
= (grammar
*)PyObject_MALLOC(sizeof(grammar
));
25 Py_FatalError("no mem for new grammar");
29 g
->g_ll
.ll_nlabels
= 0;
30 g
->g_ll
.ll_label
= NULL
;
36 adddfa(grammar
*g
, int type
, char *name
)
40 g
->g_dfa
= (dfa
*)PyObject_REALLOC(g
->g_dfa
,
41 sizeof(dfa
) * (g
->g_ndfas
+ 1));
43 Py_FatalError("no mem to resize dfa in adddfa");
44 d
= &g
->g_dfa
[g
->g_ndfas
++];
46 d
->d_name
= strdup(name
);
51 return d
; /* Only use while fresh! */
59 d
->d_state
= (state
*)PyObject_REALLOC(d
->d_state
,
60 sizeof(state
) * (d
->d_nstates
+ 1));
61 if (d
->d_state
== NULL
)
62 Py_FatalError("no mem to resize state in addstate");
63 s
= &d
->d_state
[d
->d_nstates
++];
70 return s
- d
->d_state
;
74 addarc(dfa
*d
, int from
, int to
, int lbl
)
79 assert(0 <= from
&& from
< d
->d_nstates
);
80 assert(0 <= to
&& to
< d
->d_nstates
);
82 s
= &d
->d_state
[from
];
83 s
->s_arc
= (arc
*)PyObject_REALLOC(s
->s_arc
, sizeof(arc
) * (s
->s_narcs
+ 1));
85 Py_FatalError("no mem to resize arc list in addarc");
86 a
= &s
->s_arc
[s
->s_narcs
++];
92 addlabel(labellist
*ll
, int type
, char *str
)
97 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
98 if (ll
->ll_label
[i
].lb_type
== type
&&
99 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
102 ll
->ll_label
= (label
*)PyObject_REALLOC(ll
->ll_label
,
103 sizeof(label
) * (ll
->ll_nlabels
+ 1));
104 if (ll
->ll_label
== NULL
)
105 Py_FatalError("no mem to resize labellist in addlabel");
106 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
108 lb
->lb_str
= strdup(str
);
110 printf("Label @ %8p, %d: %s\n", ll
, ll
->ll_nlabels
,
111 PyGrammar_LabelRepr(lb
));
112 return lb
- ll
->ll_label
;
115 /* Same, but rather dies than adds */
118 findlabel(labellist
*ll
, int type
, char *str
)
122 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
123 if (ll
->ll_label
[i
].lb_type
== type
/*&&
124 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
127 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
128 Py_FatalError("grammar.c:findlabel()");
129 return 0; /* Make gcc -Wall happy */
133 static void translabel(grammar
*, label
*);
136 translatelabels(grammar
*g
)
141 printf("Translating labels ...\n");
143 /* Don't translate EMPTY */
144 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
145 translabel(g
, &g
->g_ll
.ll_label
[i
]);
149 translabel(grammar
*g
, label
*lb
)
154 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb
));
156 if (lb
->lb_type
== NAME
) {
157 for (i
= 0; i
< g
->g_ndfas
; i
++) {
158 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
161 "Label %s is non-terminal %d.\n",
164 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
170 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
171 if (strcmp(lb
->lb_str
, _PyParser_TokenNames
[i
]) == 0) {
173 printf("Label %s is terminal %d.\n",
181 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
185 if (lb
->lb_type
== STRING
) {
186 if (isalpha(Py_CHARMASK(lb
->lb_str
[1])) ||
187 lb
->lb_str
[1] == '_') {
193 printf("Label %s is a keyword\n", lb
->lb_str
);
195 src
= lb
->lb_str
+ 1;
196 p
= strchr(src
, '\'');
200 name_len
= strlen(src
);
201 dest
= (char *)malloc(name_len
+ 1);
203 printf("Can't alloc dest '%s'\n", src
);
206 strncpy(dest
, src
, name_len
);
207 dest
[name_len
] = '\0';
211 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
212 int type
= (int) PyToken_OneChar(lb
->lb_str
[1]);
219 printf("Unknown OP label %s\n",
222 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
223 int type
= (int) PyToken_TwoChars(lb
->lb_str
[1],
231 printf("Unknown OP label %s\n",
234 else if (lb
->lb_str
[2] && lb
->lb_str
[3] && lb
->lb_str
[4] == lb
->lb_str
[0]) {
235 int type
= (int) PyToken_ThreeChars(lb
->lb_str
[1],
244 printf("Unknown OP label %s\n",
248 printf("Can't translate STRING label %s\n",
252 printf("Can't translate label '%s'\n",
253 PyGrammar_LabelRepr(lb
));