2 /* Grammar implementation */
5 #include "pgenheaders.h"
12 extern int Py_DebugFlag
;
19 g
= (grammar
*)PyObject_MALLOC(sizeof(grammar
));
21 Py_FatalError("no mem for new grammar");
25 g
->g_ll
.ll_nlabels
= 0;
26 g
->g_ll
.ll_label
= NULL
;
32 adddfa(grammar
*g
, int type
, char *name
)
36 g
->g_dfa
= (dfa
*)PyObject_REALLOC(g
->g_dfa
,
37 sizeof(dfa
) * (g
->g_ndfas
+ 1));
39 Py_FatalError("no mem to resize dfa in adddfa");
40 d
= &g
->g_dfa
[g
->g_ndfas
++];
42 d
->d_name
= strdup(name
);
47 return d
; /* Only use while fresh! */
55 d
->d_state
= (state
*)PyObject_REALLOC(d
->d_state
,
56 sizeof(state
) * (d
->d_nstates
+ 1));
57 if (d
->d_state
== NULL
)
58 Py_FatalError("no mem to resize state in addstate");
59 s
= &d
->d_state
[d
->d_nstates
++];
66 return s
- d
->d_state
;
70 addarc(dfa
*d
, int from
, int to
, int lbl
)
75 assert(0 <= from
&& from
< d
->d_nstates
);
76 assert(0 <= to
&& to
< d
->d_nstates
);
78 s
= &d
->d_state
[from
];
79 s
->s_arc
= (arc
*)PyObject_REALLOC(s
->s_arc
, sizeof(arc
) * (s
->s_narcs
+ 1));
81 Py_FatalError("no mem to resize arc list in addarc");
82 a
= &s
->s_arc
[s
->s_narcs
++];
88 addlabel(labellist
*ll
, int type
, char *str
)
93 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
94 if (ll
->ll_label
[i
].lb_type
== type
&&
95 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
98 ll
->ll_label
= (label
*)PyObject_REALLOC(ll
->ll_label
,
99 sizeof(label
) * (ll
->ll_nlabels
+ 1));
100 if (ll
->ll_label
== NULL
)
101 Py_FatalError("no mem to resize labellist in addlabel");
102 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
104 lb
->lb_str
= strdup(str
);
106 printf("Label @ %8p, %d: %s\n", ll
, ll
->ll_nlabels
,
107 PyGrammar_LabelRepr(lb
));
108 return lb
- ll
->ll_label
;
111 /* Same, but rather dies than adds */
114 findlabel(labellist
*ll
, int type
, char *str
)
118 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
119 if (ll
->ll_label
[i
].lb_type
== type
/*&&
120 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
123 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
124 Py_FatalError("grammar.c:findlabel()");
125 return 0; /* Make gcc -Wall happy */
129 static void translabel(grammar
*, label
*);
132 translatelabels(grammar
*g
)
137 printf("Translating labels ...\n");
139 /* Don't translate EMPTY */
140 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
141 translabel(g
, &g
->g_ll
.ll_label
[i
]);
145 translabel(grammar
*g
, label
*lb
)
150 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb
));
152 if (lb
->lb_type
== NAME
) {
153 for (i
= 0; i
< g
->g_ndfas
; i
++) {
154 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
157 "Label %s is non-terminal %d.\n",
160 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
166 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
167 if (strcmp(lb
->lb_str
, _PyParser_TokenNames
[i
]) == 0) {
169 printf("Label %s is terminal %d.\n",
177 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
181 if (lb
->lb_type
== STRING
) {
182 if (isalpha(Py_CHARMASK(lb
->lb_str
[1])) ||
183 lb
->lb_str
[1] == '_') {
189 printf("Label %s is a keyword\n", lb
->lb_str
);
191 src
= lb
->lb_str
+ 1;
192 p
= strchr(src
, '\'');
196 name_len
= strlen(src
);
197 dest
= (char *)malloc(name_len
+ 1);
199 printf("Can't alloc dest '%s'\n", src
);
202 strncpy(dest
, src
, name_len
);
203 dest
[name_len
] = '\0';
207 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
208 int type
= (int) PyToken_OneChar(lb
->lb_str
[1]);
215 printf("Unknown OP label %s\n",
218 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
219 int type
= (int) PyToken_TwoChars(lb
->lb_str
[1],
227 printf("Unknown OP label %s\n",
230 else if (lb
->lb_str
[2] && lb
->lb_str
[3] && lb
->lb_str
[4] == lb
->lb_str
[0]) {
231 int type
= (int) PyToken_ThreeChars(lb
->lb_str
[1],
240 printf("Unknown OP label %s\n",
244 printf("Can't translate STRING label %s\n",
248 printf("Can't translate label '%s'\n",
249 PyGrammar_LabelRepr(lb
));