1 | n/a | |
---|
2 | n/a | /* Grammar implementation */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "pgenheaders.h" |
---|
6 | n/a | |
---|
7 | n/a | #include <ctype.h> |
---|
8 | n/a | |
---|
9 | n/a | #include "token.h" |
---|
10 | n/a | #include "grammar.h" |
---|
11 | n/a | |
---|
12 | n/a | extern int Py_DebugFlag; |
---|
13 | n/a | |
---|
14 | n/a | grammar * |
---|
15 | n/a | newgrammar(int start) |
---|
16 | n/a | { |
---|
17 | n/a | grammar *g; |
---|
18 | n/a | |
---|
19 | n/a | g = (grammar *)PyObject_MALLOC(sizeof(grammar)); |
---|
20 | n/a | if (g == NULL) |
---|
21 | n/a | Py_FatalError("no mem for new grammar"); |
---|
22 | n/a | g->g_ndfas = 0; |
---|
23 | n/a | g->g_dfa = NULL; |
---|
24 | n/a | g->g_start = start; |
---|
25 | n/a | g->g_ll.ll_nlabels = 0; |
---|
26 | n/a | g->g_ll.ll_label = NULL; |
---|
27 | n/a | g->g_accel = 0; |
---|
28 | n/a | return g; |
---|
29 | n/a | } |
---|
30 | n/a | |
---|
31 | n/a | void |
---|
32 | n/a | freegrammar(grammar *g) |
---|
33 | n/a | { |
---|
34 | n/a | int i; |
---|
35 | n/a | for (i = 0; i < g->g_ndfas; i++) { |
---|
36 | n/a | free(g->g_dfa[i].d_name); |
---|
37 | n/a | for (int j = 0; j < g->g_dfa[i].d_nstates; j++) |
---|
38 | n/a | PyObject_FREE(g->g_dfa[i].d_state[j].s_arc); |
---|
39 | n/a | PyObject_FREE(g->g_dfa[i].d_state); |
---|
40 | n/a | } |
---|
41 | n/a | PyObject_FREE(g->g_dfa); |
---|
42 | n/a | for (i = 0; i < g->g_ll.ll_nlabels; i++) |
---|
43 | n/a | free(g->g_ll.ll_label[i].lb_str); |
---|
44 | n/a | PyObject_FREE(g->g_ll.ll_label); |
---|
45 | n/a | PyObject_FREE(g); |
---|
46 | n/a | } |
---|
47 | n/a | |
---|
48 | n/a | dfa * |
---|
49 | n/a | adddfa(grammar *g, int type, const char *name) |
---|
50 | n/a | { |
---|
51 | n/a | dfa *d; |
---|
52 | n/a | |
---|
53 | n/a | g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa, |
---|
54 | n/a | sizeof(dfa) * (g->g_ndfas + 1)); |
---|
55 | n/a | if (g->g_dfa == NULL) |
---|
56 | n/a | Py_FatalError("no mem to resize dfa in adddfa"); |
---|
57 | n/a | d = &g->g_dfa[g->g_ndfas++]; |
---|
58 | n/a | d->d_type = type; |
---|
59 | n/a | d->d_name = strdup(name); |
---|
60 | n/a | d->d_nstates = 0; |
---|
61 | n/a | d->d_state = NULL; |
---|
62 | n/a | d->d_initial = -1; |
---|
63 | n/a | d->d_first = NULL; |
---|
64 | n/a | return d; /* Only use while fresh! */ |
---|
65 | n/a | } |
---|
66 | n/a | |
---|
67 | n/a | int |
---|
68 | n/a | addstate(dfa *d) |
---|
69 | n/a | { |
---|
70 | n/a | state *s; |
---|
71 | n/a | |
---|
72 | n/a | d->d_state = (state *)PyObject_REALLOC(d->d_state, |
---|
73 | n/a | sizeof(state) * (d->d_nstates + 1)); |
---|
74 | n/a | if (d->d_state == NULL) |
---|
75 | n/a | Py_FatalError("no mem to resize state in addstate"); |
---|
76 | n/a | s = &d->d_state[d->d_nstates++]; |
---|
77 | n/a | s->s_narcs = 0; |
---|
78 | n/a | s->s_arc = NULL; |
---|
79 | n/a | s->s_lower = 0; |
---|
80 | n/a | s->s_upper = 0; |
---|
81 | n/a | s->s_accel = NULL; |
---|
82 | n/a | s->s_accept = 0; |
---|
83 | n/a | return Py_SAFE_DOWNCAST(s - d->d_state, intptr_t, int); |
---|
84 | n/a | } |
---|
85 | n/a | |
---|
86 | n/a | void |
---|
87 | n/a | addarc(dfa *d, int from, int to, int lbl) |
---|
88 | n/a | { |
---|
89 | n/a | state *s; |
---|
90 | n/a | arc *a; |
---|
91 | n/a | |
---|
92 | n/a | assert(0 <= from && from < d->d_nstates); |
---|
93 | n/a | assert(0 <= to && to < d->d_nstates); |
---|
94 | n/a | |
---|
95 | n/a | s = &d->d_state[from]; |
---|
96 | n/a | s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1)); |
---|
97 | n/a | if (s->s_arc == NULL) |
---|
98 | n/a | Py_FatalError("no mem to resize arc list in addarc"); |
---|
99 | n/a | a = &s->s_arc[s->s_narcs++]; |
---|
100 | n/a | a->a_lbl = lbl; |
---|
101 | n/a | a->a_arrow = to; |
---|
102 | n/a | } |
---|
103 | n/a | |
---|
104 | n/a | int |
---|
105 | n/a | addlabel(labellist *ll, int type, const char *str) |
---|
106 | n/a | { |
---|
107 | n/a | int i; |
---|
108 | n/a | label *lb; |
---|
109 | n/a | |
---|
110 | n/a | for (i = 0; i < ll->ll_nlabels; i++) { |
---|
111 | n/a | if (ll->ll_label[i].lb_type == type && |
---|
112 | n/a | strcmp(ll->ll_label[i].lb_str, str) == 0) |
---|
113 | n/a | return i; |
---|
114 | n/a | } |
---|
115 | n/a | ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label, |
---|
116 | n/a | sizeof(label) * (ll->ll_nlabels + 1)); |
---|
117 | n/a | if (ll->ll_label == NULL) |
---|
118 | n/a | Py_FatalError("no mem to resize labellist in addlabel"); |
---|
119 | n/a | lb = &ll->ll_label[ll->ll_nlabels++]; |
---|
120 | n/a | lb->lb_type = type; |
---|
121 | n/a | lb->lb_str = strdup(str); |
---|
122 | n/a | if (Py_DebugFlag) |
---|
123 | n/a | printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels, |
---|
124 | n/a | PyGrammar_LabelRepr(lb)); |
---|
125 | n/a | return Py_SAFE_DOWNCAST(lb - ll->ll_label, intptr_t, int); |
---|
126 | n/a | } |
---|
127 | n/a | |
---|
128 | n/a | /* Same, but rather dies than adds */ |
---|
129 | n/a | |
---|
130 | n/a | int |
---|
131 | n/a | findlabel(labellist *ll, int type, const char *str) |
---|
132 | n/a | { |
---|
133 | n/a | int i; |
---|
134 | n/a | |
---|
135 | n/a | for (i = 0; i < ll->ll_nlabels; i++) { |
---|
136 | n/a | if (ll->ll_label[i].lb_type == type /*&& |
---|
137 | n/a | strcmp(ll->ll_label[i].lb_str, str) == 0*/) |
---|
138 | n/a | return i; |
---|
139 | n/a | } |
---|
140 | n/a | fprintf(stderr, "Label %d/'%s' not found\n", type, str); |
---|
141 | n/a | Py_FatalError("grammar.c:findlabel()"); |
---|
142 | n/a | |
---|
143 | n/a | /* Py_FatalError() is declared with __attribute__((__noreturn__)). |
---|
144 | n/a | GCC emits a warning without "return 0;" (compiler bug!), but Clang is |
---|
145 | n/a | smarter and emits a warning on the return... */ |
---|
146 | n/a | #ifndef __clang__ |
---|
147 | n/a | return 0; /* Make gcc -Wall happy */ |
---|
148 | n/a | #endif |
---|
149 | n/a | } |
---|
150 | n/a | |
---|
151 | n/a | /* Forward */ |
---|
152 | n/a | static void translabel(grammar *, label *); |
---|
153 | n/a | |
---|
154 | n/a | void |
---|
155 | n/a | translatelabels(grammar *g) |
---|
156 | n/a | { |
---|
157 | n/a | int i; |
---|
158 | n/a | |
---|
159 | n/a | #ifdef Py_DEBUG |
---|
160 | n/a | printf("Translating labels ...\n"); |
---|
161 | n/a | #endif |
---|
162 | n/a | /* Don't translate EMPTY */ |
---|
163 | n/a | for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++) |
---|
164 | n/a | translabel(g, &g->g_ll.ll_label[i]); |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | static void |
---|
168 | n/a | translabel(grammar *g, label *lb) |
---|
169 | n/a | { |
---|
170 | n/a | int i; |
---|
171 | n/a | |
---|
172 | n/a | if (Py_DebugFlag) |
---|
173 | n/a | printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb)); |
---|
174 | n/a | |
---|
175 | n/a | if (lb->lb_type == NAME) { |
---|
176 | n/a | for (i = 0; i < g->g_ndfas; i++) { |
---|
177 | n/a | if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) { |
---|
178 | n/a | if (Py_DebugFlag) |
---|
179 | n/a | printf( |
---|
180 | n/a | "Label %s is non-terminal %d.\n", |
---|
181 | n/a | lb->lb_str, |
---|
182 | n/a | g->g_dfa[i].d_type); |
---|
183 | n/a | lb->lb_type = g->g_dfa[i].d_type; |
---|
184 | n/a | free(lb->lb_str); |
---|
185 | n/a | lb->lb_str = NULL; |
---|
186 | n/a | return; |
---|
187 | n/a | } |
---|
188 | n/a | } |
---|
189 | n/a | for (i = 0; i < (int)N_TOKENS; i++) { |
---|
190 | n/a | if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) { |
---|
191 | n/a | if (Py_DebugFlag) |
---|
192 | n/a | printf("Label %s is terminal %d.\n", |
---|
193 | n/a | lb->lb_str, i); |
---|
194 | n/a | lb->lb_type = i; |
---|
195 | n/a | free(lb->lb_str); |
---|
196 | n/a | lb->lb_str = NULL; |
---|
197 | n/a | return; |
---|
198 | n/a | } |
---|
199 | n/a | } |
---|
200 | n/a | printf("Can't translate NAME label '%s'\n", lb->lb_str); |
---|
201 | n/a | return; |
---|
202 | n/a | } |
---|
203 | n/a | |
---|
204 | n/a | if (lb->lb_type == STRING) { |
---|
205 | n/a | if (isalpha(Py_CHARMASK(lb->lb_str[1])) || |
---|
206 | n/a | lb->lb_str[1] == '_') { |
---|
207 | n/a | char *p; |
---|
208 | n/a | char *src; |
---|
209 | n/a | char *dest; |
---|
210 | n/a | size_t name_len; |
---|
211 | n/a | if (Py_DebugFlag) |
---|
212 | n/a | printf("Label %s is a keyword\n", lb->lb_str); |
---|
213 | n/a | lb->lb_type = NAME; |
---|
214 | n/a | src = lb->lb_str + 1; |
---|
215 | n/a | p = strchr(src, '\''); |
---|
216 | n/a | if (p) |
---|
217 | n/a | name_len = p - src; |
---|
218 | n/a | else |
---|
219 | n/a | name_len = strlen(src); |
---|
220 | n/a | dest = (char *)malloc(name_len + 1); |
---|
221 | n/a | if (!dest) { |
---|
222 | n/a | printf("Can't alloc dest '%s'\n", src); |
---|
223 | n/a | return; |
---|
224 | n/a | } |
---|
225 | n/a | strncpy(dest, src, name_len); |
---|
226 | n/a | dest[name_len] = '\0'; |
---|
227 | n/a | free(lb->lb_str); |
---|
228 | n/a | lb->lb_str = dest; |
---|
229 | n/a | } |
---|
230 | n/a | else if (lb->lb_str[2] == lb->lb_str[0]) { |
---|
231 | n/a | int type = (int) PyToken_OneChar(lb->lb_str[1]); |
---|
232 | n/a | if (type != OP) { |
---|
233 | n/a | lb->lb_type = type; |
---|
234 | n/a | free(lb->lb_str); |
---|
235 | n/a | lb->lb_str = NULL; |
---|
236 | n/a | } |
---|
237 | n/a | else |
---|
238 | n/a | printf("Unknown OP label %s\n", |
---|
239 | n/a | lb->lb_str); |
---|
240 | n/a | } |
---|
241 | n/a | else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) { |
---|
242 | n/a | int type = (int) PyToken_TwoChars(lb->lb_str[1], |
---|
243 | n/a | lb->lb_str[2]); |
---|
244 | n/a | if (type != OP) { |
---|
245 | n/a | lb->lb_type = type; |
---|
246 | n/a | free(lb->lb_str); |
---|
247 | n/a | lb->lb_str = NULL; |
---|
248 | n/a | } |
---|
249 | n/a | else |
---|
250 | n/a | printf("Unknown OP label %s\n", |
---|
251 | n/a | lb->lb_str); |
---|
252 | n/a | } |
---|
253 | n/a | else if (lb->lb_str[2] && lb->lb_str[3] && lb->lb_str[4] == lb->lb_str[0]) { |
---|
254 | n/a | int type = (int) PyToken_ThreeChars(lb->lb_str[1], |
---|
255 | n/a | lb->lb_str[2], |
---|
256 | n/a | lb->lb_str[3]); |
---|
257 | n/a | if (type != OP) { |
---|
258 | n/a | lb->lb_type = type; |
---|
259 | n/a | free(lb->lb_str); |
---|
260 | n/a | lb->lb_str = NULL; |
---|
261 | n/a | } |
---|
262 | n/a | else |
---|
263 | n/a | printf("Unknown OP label %s\n", |
---|
264 | n/a | lb->lb_str); |
---|
265 | n/a | } |
---|
266 | n/a | else |
---|
267 | n/a | printf("Can't translate STRING label %s\n", |
---|
268 | n/a | lb->lb_str); |
---|
269 | n/a | } |
---|
270 | n/a | else |
---|
271 | n/a | printf("Can't translate label '%s'\n", |
---|
272 | n/a | PyGrammar_LabelRepr(lb)); |
---|
273 | n/a | } |
---|