2 /* Grammar implementation */
5 #include "pgenheaders.h"
16 extern int Py_DebugFlag
;
23 g
= PyMem_NEW(grammar
, 1);
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 PyMem_RESIZE(g
->g_dfa
, dfa
, g
->g_ndfas
+ 1);
42 Py_FatalError("no mem to resize dfa in adddfa");
43 d
= &g
->g_dfa
[g
->g_ndfas
++];
45 d
->d_name
= strdup(name
);
50 return d
; /* Only use while fresh! */
58 PyMem_RESIZE(d
->d_state
, state
, d
->d_nstates
+ 1);
59 if (d
->d_state
== NULL
)
60 Py_FatalError("no mem to resize state in addstate");
61 s
= &d
->d_state
[d
->d_nstates
++];
68 return s
- d
->d_state
;
72 addarc(dfa
*d
, int from
, int to
, int lbl
)
77 assert(0 <= from
&& from
< d
->d_nstates
);
78 assert(0 <= to
&& to
< d
->d_nstates
);
80 s
= &d
->d_state
[from
];
81 PyMem_RESIZE(s
->s_arc
, arc
, s
->s_narcs
+ 1);
83 Py_FatalError("no mem to resize arc list in addarc");
84 a
= &s
->s_arc
[s
->s_narcs
++];
90 addlabel(labellist
*ll
, int type
, char *str
)
95 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
96 if (ll
->ll_label
[i
].lb_type
== type
&&
97 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
100 PyMem_RESIZE(ll
->ll_label
, label
, ll
->ll_nlabels
+ 1);
101 if (ll
->ll_label
== NULL
)
102 Py_FatalError("no mem to resize labellist in addlabel");
103 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
105 lb
->lb_str
= strdup(str
);
107 printf("Label @ %08x, %d: %s\n", (unsigned)ll
, ll
->ll_nlabels
,
108 PyGrammar_LabelRepr(lb
));
109 return lb
- ll
->ll_label
;
112 /* Same, but rather dies than adds */
115 findlabel(labellist
*ll
, int type
, char *str
)
119 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
120 if (ll
->ll_label
[i
].lb_type
== type
/*&&
121 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
124 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
125 Py_FatalError("grammar.c:findlabel()");
126 return 0; /* Make gcc -Wall happy */
130 static void translabel(grammar
*, label
*);
133 translatelabels(grammar
*g
)
138 printf("Translating labels ...\n");
140 /* Don't translate EMPTY */
141 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
142 translabel(g
, &g
->g_ll
.ll_label
[i
]);
146 translabel(grammar
*g
, label
*lb
)
151 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb
));
153 if (lb
->lb_type
== NAME
) {
154 for (i
= 0; i
< g
->g_ndfas
; i
++) {
155 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
158 "Label %s is non-terminal %d.\n",
161 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
167 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
168 if (strcmp(lb
->lb_str
, _PyParser_TokenNames
[i
]) == 0) {
170 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] == '_') {
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
= malloc(name_len
+ 1);
198 strncpy(dest
, src
, name_len
);
199 dest
[name_len
] = '\0';
203 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
204 int type
= (int) PyToken_OneChar(lb
->lb_str
[1]);
211 printf("Unknown OP label %s\n",
214 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
215 int type
= (int) PyToken_TwoChars(lb
->lb_str
[1],
223 printf("Unknown OP label %s\n",
226 else if (lb
->lb_str
[2] && lb
->lb_str
[3] && lb
->lb_str
[4] == lb
->lb_str
[0]) {
227 int type
= (int) PyToken_ThreeChars(lb
->lb_str
[1],
236 printf("Unknown OP label %s\n",
240 printf("Can't translate STRING label %s\n",
244 printf("Can't translate label '%s'\n",
245 PyGrammar_LabelRepr(lb
));