Mercurial > cpython-lru_cache-weakref
annotate Parser/grammar.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 1c9a84e0981d |
| children | 85e886db19b0 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2060
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2044
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2044
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Grammar implementation */ |
| 33 | |
| 86 | 34 #include "pgenheaders.h" |
| 35 | |
| 6 | 36 #include <ctype.h> |
| 37 | |
| 38 #include "assert.h" | |
| 39 #include "token.h" | |
| 40 #include "grammar.h" | |
| 41 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
42 extern int Py_DebugFlag; |
| 6 | 43 |
| 44 grammar * | |
| 45 newgrammar(start) | |
| 46 int start; | |
| 47 { | |
| 48 grammar *g; | |
| 49 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
50 g = PyMem_NEW(grammar, 1); |
| 6 | 51 if (g == NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
52 Py_FatalError("no mem for new grammar"); |
| 6 | 53 g->g_ndfas = 0; |
| 54 g->g_dfa = NULL; | |
| 55 g->g_start = start; | |
| 56 g->g_ll.ll_nlabels = 0; | |
| 57 g->g_ll.ll_label = NULL; | |
|
2044
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
58 g->g_accel = 0; |
| 6 | 59 return g; |
| 60 } | |
| 61 | |
| 62 dfa * | |
| 63 adddfa(g, type, name) | |
| 64 grammar *g; | |
| 65 int type; | |
| 66 char *name; | |
| 67 { | |
| 68 dfa *d; | |
| 69 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
70 PyMem_RESIZE(g->g_dfa, dfa, g->g_ndfas + 1); |
| 6 | 71 if (g->g_dfa == NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
72 Py_FatalError("no mem to resize dfa in adddfa"); |
| 6 | 73 d = &g->g_dfa[g->g_ndfas++]; |
| 74 d->d_type = type; | |
| 75 d->d_name = name; | |
| 76 d->d_nstates = 0; | |
| 77 d->d_state = NULL; | |
| 78 d->d_initial = -1; | |
| 79 d->d_first = NULL; | |
| 80 return d; /* Only use while fresh! */ | |
| 81 } | |
| 82 | |
| 83 int | |
| 84 addstate(d) | |
| 85 dfa *d; | |
| 86 { | |
| 87 state *s; | |
| 88 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
89 PyMem_RESIZE(d->d_state, state, d->d_nstates + 1); |
| 6 | 90 if (d->d_state == NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
91 Py_FatalError("no mem to resize state in addstate"); |
| 6 | 92 s = &d->d_state[d->d_nstates++]; |
| 93 s->s_narcs = 0; | |
| 94 s->s_arc = NULL; | |
|
2044
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
95 s->s_lower = 0; |
|
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
96 s->s_upper = 0; |
|
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
97 s->s_accel = NULL; |
|
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
98 s->s_accept = 0; |
| 6 | 99 return s - d->d_state; |
| 100 } | |
| 101 | |
| 102 void | |
| 103 addarc(d, from, to, lbl) | |
| 104 dfa *d; | |
| 105 int lbl; | |
| 106 { | |
| 107 state *s; | |
| 108 arc *a; | |
| 109 | |
| 110 assert(0 <= from && from < d->d_nstates); | |
| 111 assert(0 <= to && to < d->d_nstates); | |
| 112 | |
| 113 s = &d->d_state[from]; | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
114 PyMem_RESIZE(s->s_arc, arc, s->s_narcs + 1); |
| 6 | 115 if (s->s_arc == NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
116 Py_FatalError("no mem to resize arc list in addarc"); |
| 6 | 117 a = &s->s_arc[s->s_narcs++]; |
| 118 a->a_lbl = lbl; | |
| 119 a->a_arrow = to; | |
| 120 } | |
| 121 | |
| 122 int | |
| 123 addlabel(ll, type, str) | |
| 124 labellist *ll; | |
| 125 int type; | |
| 126 char *str; | |
| 127 { | |
| 128 int i; | |
| 129 label *lb; | |
| 130 | |
| 131 for (i = 0; i < ll->ll_nlabels; i++) { | |
| 132 if (ll->ll_label[i].lb_type == type && | |
| 133 strcmp(ll->ll_label[i].lb_str, str) == 0) | |
| 134 return i; | |
| 135 } | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
136 PyMem_RESIZE(ll->ll_label, label, ll->ll_nlabels + 1); |
| 6 | 137 if (ll->ll_label == NULL) |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
138 Py_FatalError("no mem to resize labellist in addlabel"); |
| 6 | 139 lb = &ll->ll_label[ll->ll_nlabels++]; |
| 140 lb->lb_type = type; | |
| 141 lb->lb_str = str; /* XXX strdup(str) ??? */ | |
| 142 return lb - ll->ll_label; | |
| 143 } | |
| 144 | |
| 145 /* Same, but rather dies than adds */ | |
| 146 | |
| 147 int | |
| 148 findlabel(ll, type, str) | |
| 149 labellist *ll; | |
| 150 int type; | |
| 151 char *str; | |
| 152 { | |
| 153 int i; | |
| 154 | |
| 155 for (i = 0; i < ll->ll_nlabels; i++) { | |
| 156 if (ll->ll_label[i].lb_type == type /*&& | |
| 157 strcmp(ll->ll_label[i].lb_str, str) == 0*/) | |
| 158 return i; | |
| 159 } | |
| 160 fprintf(stderr, "Label %d/'%s' not found\n", type, str); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
161 Py_FatalError("grammar.c:findlabel()"); |
| 4577 | 162 return 0; /* Make gcc -Wall happy */ |
| 6 | 163 } |
| 164 | |
| 86 | 165 /* Forward */ |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
166 static void translabel Py_PROTO((grammar *, label *)); |
| 86 | 167 |
| 168 void | |
| 169 translatelabels(g) | |
| 170 grammar *g; | |
| 171 { | |
| 172 int i; | |
|
2044
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
173 |
|
4839
4567e158d20c
Rename DEBUG macro to Py_DEBUG
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
174 #ifdef Py_DEBUG |
| 86 | 175 printf("Translating labels ...\n"); |
|
2044
281e40652e23
Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
176 #endif |
| 86 | 177 /* Don't translate EMPTY */ |
| 178 for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++) | |
| 179 translabel(g, &g->g_ll.ll_label[i]); | |
| 180 } | |
| 181 | |
| 6 | 182 static void |
| 183 translabel(g, lb) | |
| 184 grammar *g; | |
| 185 label *lb; | |
| 186 { | |
| 187 int i; | |
| 188 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
189 if (Py_DebugFlag) |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
190 printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb)); |
| 6 | 191 |
| 192 if (lb->lb_type == NAME) { | |
| 193 for (i = 0; i < g->g_ndfas; i++) { | |
| 194 if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
195 if (Py_DebugFlag) |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
196 printf( |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
197 "Label %s is non-terminal %d.\n", |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
198 lb->lb_str, |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
199 g->g_dfa[i].d_type); |
| 6 | 200 lb->lb_type = g->g_dfa[i].d_type; |
| 201 lb->lb_str = NULL; | |
| 202 return; | |
| 203 } | |
| 204 } | |
| 205 for (i = 0; i < (int)N_TOKENS; i++) { | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
206 if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) { |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
207 if (Py_DebugFlag) |
| 6 | 208 printf("Label %s is terminal %d.\n", |
| 209 lb->lb_str, i); | |
| 210 lb->lb_type = i; | |
| 211 lb->lb_str = NULL; | |
| 212 return; | |
| 213 } | |
| 214 } | |
| 215 printf("Can't translate NAME label '%s'\n", lb->lb_str); | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 if (lb->lb_type == STRING) { | |
|
5231
93344cbdf542
Allow leading underscore in keywords.
Guido van Rossum <guido@python.org>
parents:
4839
diff
changeset
|
220 if (isalpha(lb->lb_str[1]) || lb->lb_str[1] == '_') { |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
221 char *p; |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
222 if (Py_DebugFlag) |
| 6 | 223 printf("Label %s is a keyword\n", lb->lb_str); |
| 224 lb->lb_type = NAME; | |
| 225 lb->lb_str++; | |
| 226 p = strchr(lb->lb_str, '\''); | |
| 227 if (p) | |
| 228 *p = '\0'; | |
| 229 } | |
|
393
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
230 else if (lb->lb_str[2] == lb->lb_str[0]) { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
231 int type = (int) PyToken_OneChar(lb->lb_str[1]); |
|
393
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
232 if (type != OP) { |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
233 lb->lb_type = type; |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
234 lb->lb_str = NULL; |
| 6 | 235 } |
| 236 else | |
|
393
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
237 printf("Unknown OP label %s\n", |
| 6 | 238 lb->lb_str); |
| 239 } | |
|
393
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
240 else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) { |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
241 int type = (int) PyToken_TwoChars(lb->lb_str[1], |
|
393
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
242 lb->lb_str[2]); |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
243 if (type != OP) { |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
244 lb->lb_type = type; |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
245 lb->lb_str = NULL; |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
246 } |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
247 else |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
248 printf("Unknown OP label %s\n", |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
249 lb->lb_str); |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
250 } |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
251 else |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
252 printf("Can't translate STRING label %s\n", |
|
59609b3af978
Implemented 2-character operators.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
253 lb->lb_str); |
| 6 | 254 } |
| 255 else | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
256 printf("Can't translate label '%s'\n", |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
5231
diff
changeset
|
257 PyGrammar_LabelRepr(lb)); |
| 6 | 258 } |
