blob: 9e7c49aab652499dcebf7c25f6cecf181c2ac190 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Grammar implementation */
3
Tim Peters1ca12962001-12-04 03:18:48 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005#include "pgenheaders.h"
6
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include <ctype.h>
8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009#include "token.h"
10#include "grammar.h"
11
Martin v. Löwisa94568a2003-05-10 07:36:56 +000012#ifdef RISCOS
13#include <unixlib.h>
14#endif
15
Guido van Rossum86bea461997-04-29 21:03:06 +000016extern int Py_DebugFlag;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
18grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +000019newgrammar(int start)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020{
21 grammar *g;
22
Anthony Baxter11490022006-04-11 05:39:14 +000023 g = (grammar *)PyObject_MALLOC(sizeof(grammar));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024 if (g == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000025 Py_FatalError("no mem for new grammar");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026 g->g_ndfas = 0;
27 g->g_dfa = NULL;
28 g->g_start = start;
29 g->g_ll.ll_nlabels = 0;
30 g->g_ll.ll_label = NULL;
Guido van Rossum588633d1994-12-30 15:46:02 +000031 g->g_accel = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032 return g;
33}
34
35dfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +000036adddfa(grammar *g, int type, char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037{
38 dfa *d;
39
Anthony Baxter11490022006-04-11 05:39:14 +000040 g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa,
41 sizeof(dfa) * (g->g_ndfas + 1));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042 if (g->g_dfa == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000043 Py_FatalError("no mem to resize dfa in adddfa");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 d = &g->g_dfa[g->g_ndfas++];
45 d->d_type = type;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +000046 d->d_name = strdup(name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047 d->d_nstates = 0;
48 d->d_state = NULL;
49 d->d_initial = -1;
50 d->d_first = NULL;
51 return d; /* Only use while fresh! */
52}
53
54int
Thomas Wouters23c9e002000-07-22 19:20:54 +000055addstate(dfa *d)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056{
57 state *s;
58
Anthony Baxter11490022006-04-11 05:39:14 +000059 d->d_state = (state *)PyObject_REALLOC(d->d_state,
Neal Norwitz2c4e4f92006-04-10 06:42:25 +000060 sizeof(state) * (d->d_nstates + 1));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 if (d->d_state == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000062 Py_FatalError("no mem to resize state in addstate");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063 s = &d->d_state[d->d_nstates++];
64 s->s_narcs = 0;
65 s->s_arc = NULL;
Guido van Rossum588633d1994-12-30 15:46:02 +000066 s->s_lower = 0;
67 s->s_upper = 0;
68 s->s_accel = NULL;
69 s->s_accept = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 return s - d->d_state;
71}
72
73void
Thomas Wouters23c9e002000-07-22 19:20:54 +000074addarc(dfa *d, int from, int to, int lbl)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075{
76 state *s;
77 arc *a;
78
79 assert(0 <= from && from < d->d_nstates);
80 assert(0 <= to && to < d->d_nstates);
81
82 s = &d->d_state[from];
Anthony Baxter11490022006-04-11 05:39:14 +000083 s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 if (s->s_arc == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000085 Py_FatalError("no mem to resize arc list in addarc");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 a = &s->s_arc[s->s_narcs++];
87 a->a_lbl = lbl;
88 a->a_arrow = to;
89}
90
91int
Thomas Wouters23c9e002000-07-22 19:20:54 +000092addlabel(labellist *ll, int type, char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
94 int i;
95 label *lb;
96
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)
100 return i;
101 }
Anthony Baxter11490022006-04-11 05:39:14 +0000102 ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
Neal Norwitz2c4e4f92006-04-10 06:42:25 +0000103 sizeof(label) * (ll->ll_nlabels + 1));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 if (ll->ll_label == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000105 Py_FatalError("no mem to resize labellist in addlabel");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 lb = &ll->ll_label[ll->ll_nlabels++];
107 lb->lb_type = type;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000108 lb->lb_str = strdup(str);
109 if (Py_DebugFlag)
Guido van Rossumc6966172004-03-20 22:34:14 +0000110 printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels,
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000111 PyGrammar_LabelRepr(lb));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 return lb - ll->ll_label;
113}
114
115/* Same, but rather dies than adds */
116
117int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000118findlabel(labellist *ll, int type, char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119{
120 int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121
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*/)
125 return i;
126 }
127 fprintf(stderr, "Label %d/'%s' not found\n", type, str);
Guido van Rossum86bea461997-04-29 21:03:06 +0000128 Py_FatalError("grammar.c:findlabel()");
Guido van Rossumfd8a3931996-12-02 18:27:33 +0000129 return 0; /* Make gcc -Wall happy */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130}
131
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000133static void translabel(grammar *, label *);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134
135void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000136translatelabels(grammar *g)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137{
138 int i;
Guido van Rossum588633d1994-12-30 15:46:02 +0000139
Guido van Rossum408027e1996-12-30 16:17:54 +0000140#ifdef Py_DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +0000141 printf("Translating labels ...\n");
Guido van Rossum588633d1994-12-30 15:46:02 +0000142#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000143 /* 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]);
146}
147
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000149translabel(grammar *g, label *lb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
151 int i;
152
Guido van Rossum86bea461997-04-29 21:03:06 +0000153 if (Py_DebugFlag)
154 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155
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) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000159 if (Py_DebugFlag)
160 printf(
161 "Label %s is non-terminal %d.\n",
162 lb->lb_str,
163 g->g_dfa[i].d_type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164 lb->lb_type = g->g_dfa[i].d_type;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000165 free(lb->lb_str);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166 lb->lb_str = NULL;
167 return;
168 }
169 }
170 for (i = 0; i < (int)N_TOKENS; i++) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000171 if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) {
172 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173 printf("Label %s is terminal %d.\n",
174 lb->lb_str, i);
175 lb->lb_type = i;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000176 free(lb->lb_str);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000177 lb->lb_str = NULL;
178 return;
179 }
180 }
181 printf("Can't translate NAME label '%s'\n", lb->lb_str);
182 return;
183 }
184
185 if (lb->lb_type == STRING) {
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000186 if (isalpha(Py_CHARMASK(lb->lb_str[1])) ||
187 lb->lb_str[1] == '_') {
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000188 char *p;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000189 char *src;
190 char *dest;
191 size_t name_len;
Guido van Rossum86bea461997-04-29 21:03:06 +0000192 if (Py_DebugFlag)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193 printf("Label %s is a keyword\n", lb->lb_str);
194 lb->lb_type = NAME;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000195 src = lb->lb_str + 1;
196 p = strchr(src, '\'');
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197 if (p)
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000198 name_len = p - src;
199 else
200 name_len = strlen(src);
Anthony Baxter11490022006-04-11 05:39:14 +0000201 dest = (char *)malloc(name_len + 1);
Neal Norwitz9ac89532006-08-13 18:13:36 +0000202 if (!dest) {
203 printf("Can't alloc dest '%s'\n", src);
204 return;
205 }
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000206 strncpy(dest, src, name_len);
207 dest[name_len] = '\0';
208 free(lb->lb_str);
209 lb->lb_str = dest;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000210 }
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000211 else if (lb->lb_str[2] == lb->lb_str[0]) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000212 int type = (int) PyToken_OneChar(lb->lb_str[1]);
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000213 if (type != OP) {
214 lb->lb_type = type;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000215 free(lb->lb_str);
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000216 lb->lb_str = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000217 }
218 else
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000219 printf("Unknown OP label %s\n",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220 lb->lb_str);
221 }
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000222 else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000223 int type = (int) PyToken_TwoChars(lb->lb_str[1],
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000224 lb->lb_str[2]);
225 if (type != OP) {
226 lb->lb_type = type;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000227 free(lb->lb_str);
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000228 lb->lb_str = NULL;
229 }
230 else
231 printf("Unknown OP label %s\n",
232 lb->lb_str);
233 }
Thomas Wouters434d0822000-08-24 20:11:32 +0000234 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],
236 lb->lb_str[2],
237 lb->lb_str[3]);
238 if (type != OP) {
239 lb->lb_type = type;
Guido van Rossumd3ab37f2003-04-17 14:55:42 +0000240 free(lb->lb_str);
Thomas Wouters434d0822000-08-24 20:11:32 +0000241 lb->lb_str = NULL;
242 }
243 else
244 printf("Unknown OP label %s\n",
245 lb->lb_str);
246 }
Guido van Rossumc64d04d1991-10-20 20:20:00 +0000247 else
248 printf("Can't translate STRING label %s\n",
249 lb->lb_str);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 }
251 else
Guido van Rossum86bea461997-04-29 21:03:06 +0000252 printf("Can't translate label '%s'\n",
253 PyGrammar_LabelRepr(lb));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254}