Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b9f8d6e | 1995-01-04 19:08:09 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Grammar implementation */ |
| 33 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | #include "pgenheaders.h" |
| 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include <ctype.h> |
| 37 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | #include "assert.h" |
| 39 | #include "token.h" |
| 40 | #include "grammar.h" |
| 41 | |
| 42 | extern int debugging; |
| 43 | |
| 44 | grammar * |
| 45 | newgrammar(start) |
| 46 | int start; |
| 47 | { |
| 48 | grammar *g; |
| 49 | |
| 50 | g = NEW(grammar, 1); |
| 51 | if (g == NULL) |
| 52 | fatal("no mem for new grammar"); |
| 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; |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 58 | g->g_accel = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | |
| 70 | RESIZE(g->g_dfa, dfa, g->g_ndfas + 1); |
| 71 | if (g->g_dfa == NULL) |
| 72 | fatal("no mem to resize dfa in adddfa"); |
| 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 | |
| 89 | RESIZE(d->d_state, state, d->d_nstates + 1); |
| 90 | if (d->d_state == NULL) |
| 91 | fatal("no mem to resize state in addstate"); |
| 92 | s = &d->d_state[d->d_nstates++]; |
| 93 | s->s_narcs = 0; |
| 94 | s->s_arc = NULL; |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 95 | s->s_lower = 0; |
| 96 | s->s_upper = 0; |
| 97 | s->s_accel = NULL; |
| 98 | s->s_accept = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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]; |
| 114 | RESIZE(s->s_arc, arc, s->s_narcs + 1); |
| 115 | if (s->s_arc == NULL) |
| 116 | fatal("no mem to resize arc list in addarc"); |
| 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 | } |
| 136 | RESIZE(ll->ll_label, label, ll->ll_nlabels + 1); |
| 137 | if (ll->ll_label == NULL) |
| 138 | fatal("no mem to resize labellist in addlabel"); |
| 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; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 161 | fatal("grammar.c:findlabel()"); |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 162 | return 0; /* Make gcc -Wall happy */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 165 | /* Forward */ |
| 166 | static void translabel PROTO((grammar *, label *)); |
| 167 | |
| 168 | void |
| 169 | translatelabels(g) |
| 170 | grammar *g; |
| 171 | { |
| 172 | int i; |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 173 | |
| 174 | #ifdef DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 175 | printf("Translating labels ...\n"); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 176 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | static void |
| 183 | translabel(g, lb) |
| 184 | grammar *g; |
| 185 | label *lb; |
| 186 | { |
| 187 | int i; |
| 188 | |
| 189 | if (debugging) |
| 190 | printf("Translating label %s ...\n", labelrepr(lb)); |
| 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) { |
| 195 | if (debugging) |
| 196 | printf("Label %s is non-terminal %d.\n", |
| 197 | lb->lb_str, |
| 198 | g->g_dfa[i].d_type); |
| 199 | lb->lb_type = g->g_dfa[i].d_type; |
| 200 | lb->lb_str = NULL; |
| 201 | return; |
| 202 | } |
| 203 | } |
| 204 | for (i = 0; i < (int)N_TOKENS; i++) { |
| 205 | if (strcmp(lb->lb_str, tok_name[i]) == 0) { |
| 206 | if (debugging) |
| 207 | printf("Label %s is terminal %d.\n", |
| 208 | lb->lb_str, i); |
| 209 | lb->lb_type = i; |
| 210 | lb->lb_str = NULL; |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | printf("Can't translate NAME label '%s'\n", lb->lb_str); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (lb->lb_type == STRING) { |
| 219 | if (isalpha(lb->lb_str[1])) { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 220 | char *p; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 221 | if (debugging) |
| 222 | printf("Label %s is a keyword\n", lb->lb_str); |
| 223 | lb->lb_type = NAME; |
| 224 | lb->lb_str++; |
| 225 | p = strchr(lb->lb_str, '\''); |
| 226 | if (p) |
| 227 | *p = '\0'; |
| 228 | } |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 229 | else if (lb->lb_str[2] == lb->lb_str[0]) { |
| 230 | int type = (int) tok_1char(lb->lb_str[1]); |
| 231 | if (type != OP) { |
| 232 | lb->lb_type = type; |
| 233 | lb->lb_str = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 234 | } |
| 235 | else |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 236 | printf("Unknown OP label %s\n", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | lb->lb_str); |
| 238 | } |
Guido van Rossum | c64d04d | 1991-10-20 20:20:00 +0000 | [diff] [blame] | 239 | else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) { |
| 240 | int type = (int) tok_2char(lb->lb_str[1], |
| 241 | lb->lb_str[2]); |
| 242 | if (type != OP) { |
| 243 | lb->lb_type = type; |
| 244 | lb->lb_str = NULL; |
| 245 | } |
| 246 | else |
| 247 | printf("Unknown OP label %s\n", |
| 248 | lb->lb_str); |
| 249 | } |
| 250 | else |
| 251 | printf("Can't translate STRING label %s\n", |
| 252 | lb->lb_str); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | } |
| 254 | else |
| 255 | printf("Can't translate label '%s'\n", labelrepr(lb)); |
| 256 | } |