2 /* Grammar implementation */
4 #include "pgenheaders.h"
12 extern int Py_DebugFlag
;
19 g
= PyMem_NEW(grammar
, 1);
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 PyMem_RESIZE(g
->g_dfa
, dfa
, g
->g_ndfas
+ 1);
38 Py_FatalError("no mem to resize dfa in adddfa");
39 d
= &g
->g_dfa
[g
->g_ndfas
++];
46 return d
; /* Only use while fresh! */
54 PyMem_RESIZE(d
->d_state
, state
, d
->d_nstates
+ 1);
55 if (d
->d_state
== NULL
)
56 Py_FatalError("no mem to resize state in addstate");
57 s
= &d
->d_state
[d
->d_nstates
++];
64 return s
- d
->d_state
;
68 addarc(dfa
*d
, int from
, int to
, int lbl
)
73 assert(0 <= from
&& from
< d
->d_nstates
);
74 assert(0 <= to
&& to
< d
->d_nstates
);
76 s
= &d
->d_state
[from
];
77 PyMem_RESIZE(s
->s_arc
, arc
, s
->s_narcs
+ 1);
79 Py_FatalError("no mem to resize arc list in addarc");
80 a
= &s
->s_arc
[s
->s_narcs
++];
86 addlabel(labellist
*ll
, int type
, char *str
)
91 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
92 if (ll
->ll_label
[i
].lb_type
== type
&&
93 strcmp(ll
->ll_label
[i
].lb_str
, str
) == 0)
96 PyMem_RESIZE(ll
->ll_label
, label
, ll
->ll_nlabels
+ 1);
97 if (ll
->ll_label
== NULL
)
98 Py_FatalError("no mem to resize labellist in addlabel");
99 lb
= &ll
->ll_label
[ll
->ll_nlabels
++];
101 lb
->lb_str
= str
; /* XXX strdup(str) ??? */
102 return lb
- ll
->ll_label
;
105 /* Same, but rather dies than adds */
108 findlabel(labellist
*ll
, int type
, char *str
)
112 for (i
= 0; i
< ll
->ll_nlabels
; i
++) {
113 if (ll
->ll_label
[i
].lb_type
== type
/*&&
114 strcmp(ll->ll_label[i].lb_str, str) == 0*/)
117 fprintf(stderr
, "Label %d/'%s' not found\n", type
, str
);
118 Py_FatalError("grammar.c:findlabel()");
119 return 0; /* Make gcc -Wall happy */
123 static void translabel(grammar
*, label
*);
126 translatelabels(grammar
*g
)
131 printf("Translating labels ...\n");
133 /* Don't translate EMPTY */
134 for (i
= EMPTY
+1; i
< g
->g_ll
.ll_nlabels
; i
++)
135 translabel(g
, &g
->g_ll
.ll_label
[i
]);
139 translabel(grammar
*g
, label
*lb
)
144 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb
));
146 if (lb
->lb_type
== NAME
) {
147 for (i
= 0; i
< g
->g_ndfas
; i
++) {
148 if (strcmp(lb
->lb_str
, g
->g_dfa
[i
].d_name
) == 0) {
151 "Label %s is non-terminal %d.\n",
154 lb
->lb_type
= g
->g_dfa
[i
].d_type
;
159 for (i
= 0; i
< (int)N_TOKENS
; i
++) {
160 if (strcmp(lb
->lb_str
, _PyParser_TokenNames
[i
]) == 0) {
162 printf("Label %s is terminal %d.\n",
169 printf("Can't translate NAME label '%s'\n", lb
->lb_str
);
173 if (lb
->lb_type
== STRING
) {
174 if (isalpha((int)(lb
->lb_str
[1])) || lb
->lb_str
[1] == '_') {
177 printf("Label %s is a keyword\n", lb
->lb_str
);
180 p
= strchr(lb
->lb_str
, '\'');
184 else if (lb
->lb_str
[2] == lb
->lb_str
[0]) {
185 int type
= (int) PyToken_OneChar(lb
->lb_str
[1]);
191 printf("Unknown OP label %s\n",
194 else if (lb
->lb_str
[2] && lb
->lb_str
[3] == lb
->lb_str
[0]) {
195 int type
= (int) PyToken_TwoChars(lb
->lb_str
[1],
202 printf("Unknown OP label %s\n",
205 else if (lb
->lb_str
[2] && lb
->lb_str
[3] && lb
->lb_str
[4] == lb
->lb_str
[0]) {
206 int type
= (int) PyToken_ThreeChars(lb
->lb_str
[1],
214 printf("Unknown OP label %s\n",
218 printf("Can't translate STRING label %s\n",
222 printf("Can't translate label '%s'\n",
223 PyGrammar_LabelRepr(lb
));