cpython/Parser/grammar.c

247 lines
5.1 KiB
C
Raw Normal View History

1991-02-19 08:39:46 -04:00
1990-10-14 09:07:46 -03:00
/* Grammar implementation */
#include "Python.h"
1990-12-20 11:06:42 -04:00
#include "pgenheaders.h"
1990-10-14 09:07:46 -03:00
#include <ctype.h>
#include "token.h"
#include "grammar.h"
#ifdef RISCOS
#include <unixlib.h>
#endif
1997-04-29 18:03:06 -03:00
extern int Py_DebugFlag;
1990-10-14 09:07:46 -03:00
grammar *
newgrammar(int start)
1990-10-14 09:07:46 -03:00
{
grammar *g;
1997-04-29 18:03:06 -03:00
g = PyMem_NEW(grammar, 1);
1990-10-14 09:07:46 -03:00
if (g == NULL)
1997-04-29 18:03:06 -03:00
Py_FatalError("no mem for new grammar");
1990-10-14 09:07:46 -03:00
g->g_ndfas = 0;
g->g_dfa = NULL;
g->g_start = start;
g->g_ll.ll_nlabels = 0;
g->g_ll.ll_label = NULL;
g->g_accel = 0;
1990-10-14 09:07:46 -03:00
return g;
}
dfa *
adddfa(grammar *g, int type, char *name)
1990-10-14 09:07:46 -03:00
{
dfa *d;
1997-04-29 18:03:06 -03:00
PyMem_RESIZE(g->g_dfa, dfa, g->g_ndfas + 1);
1990-10-14 09:07:46 -03:00
if (g->g_dfa == NULL)
1997-04-29 18:03:06 -03:00
Py_FatalError("no mem to resize dfa in adddfa");
1990-10-14 09:07:46 -03:00
d = &g->g_dfa[g->g_ndfas++];
d->d_type = type;
d->d_name = strdup(name);
1990-10-14 09:07:46 -03:00
d->d_nstates = 0;
d->d_state = NULL;
d->d_initial = -1;
d->d_first = NULL;
return d; /* Only use while fresh! */
}
int
addstate(dfa *d)
1990-10-14 09:07:46 -03:00
{
state *s;
1997-04-29 18:03:06 -03:00
PyMem_RESIZE(d->d_state, state, d->d_nstates + 1);
1990-10-14 09:07:46 -03:00
if (d->d_state == NULL)
1997-04-29 18:03:06 -03:00
Py_FatalError("no mem to resize state in addstate");
1990-10-14 09:07:46 -03:00
s = &d->d_state[d->d_nstates++];
s->s_narcs = 0;
s->s_arc = NULL;
s->s_lower = 0;
s->s_upper = 0;
s->s_accel = NULL;
s->s_accept = 0;
1990-10-14 09:07:46 -03:00
return s - d->d_state;
}
void
addarc(dfa *d, int from, int to, int lbl)
1990-10-14 09:07:46 -03:00
{
state *s;
arc *a;
assert(0 <= from && from < d->d_nstates);
assert(0 <= to && to < d->d_nstates);
s = &d->d_state[from];
1997-04-29 18:03:06 -03:00
PyMem_RESIZE(s->s_arc, arc, s->s_narcs + 1);
1990-10-14 09:07:46 -03:00
if (s->s_arc == NULL)
1997-04-29 18:03:06 -03:00
Py_FatalError("no mem to resize arc list in addarc");
1990-10-14 09:07:46 -03:00
a = &s->s_arc[s->s_narcs++];
a->a_lbl = lbl;
a->a_arrow = to;
}
int
addlabel(labellist *ll, int type, char *str)
1990-10-14 09:07:46 -03:00
{
int i;
label *lb;
for (i = 0; i < ll->ll_nlabels; i++) {
if (ll->ll_label[i].lb_type == type &&
strcmp(ll->ll_label[i].lb_str, str) == 0)
return i;
}
1997-04-29 18:03:06 -03:00
PyMem_RESIZE(ll->ll_label, label, ll->ll_nlabels + 1);
1990-10-14 09:07:46 -03:00
if (ll->ll_label == NULL)
1997-04-29 18:03:06 -03:00
Py_FatalError("no mem to resize labellist in addlabel");
1990-10-14 09:07:46 -03:00
lb = &ll->ll_label[ll->ll_nlabels++];
lb->lb_type = type;
lb->lb_str = strdup(str);
if (Py_DebugFlag)
printf("Label @ %08x, %d: %s\n", (unsigned)ll, ll->ll_nlabels,
PyGrammar_LabelRepr(lb));
1990-10-14 09:07:46 -03:00
return lb - ll->ll_label;
}
/* Same, but rather dies than adds */
int
findlabel(labellist *ll, int type, char *str)
1990-10-14 09:07:46 -03:00
{
int i;
for (i = 0; i < ll->ll_nlabels; i++) {
if (ll->ll_label[i].lb_type == type /*&&
strcmp(ll->ll_label[i].lb_str, str) == 0*/)
return i;
}
fprintf(stderr, "Label %d/'%s' not found\n", type, str);
1997-04-29 18:03:06 -03:00
Py_FatalError("grammar.c:findlabel()");
1996-12-02 14:27:33 -04:00
return 0; /* Make gcc -Wall happy */
1990-10-14 09:07:46 -03:00
}
1990-12-20 11:06:42 -04:00
/* Forward */
static void translabel(grammar *, label *);
1990-12-20 11:06:42 -04:00
void
translatelabels(grammar *g)
1990-12-20 11:06:42 -04:00
{
int i;
1996-12-30 12:17:54 -04:00
#ifdef Py_DEBUG
1990-12-20 11:06:42 -04:00
printf("Translating labels ...\n");
#endif
1990-12-20 11:06:42 -04:00
/* Don't translate EMPTY */
for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++)
translabel(g, &g->g_ll.ll_label[i]);
}
1990-10-14 09:07:46 -03:00
static void
translabel(grammar *g, label *lb)
1990-10-14 09:07:46 -03:00
{
int i;
1997-04-29 18:03:06 -03:00
if (Py_DebugFlag)
printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb));
1990-10-14 09:07:46 -03:00
if (lb->lb_type == NAME) {
for (i = 0; i < g->g_ndfas; i++) {
if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) {
1997-04-29 18:03:06 -03:00
if (Py_DebugFlag)
printf(
"Label %s is non-terminal %d.\n",
lb->lb_str,
g->g_dfa[i].d_type);
1990-10-14 09:07:46 -03:00
lb->lb_type = g->g_dfa[i].d_type;
free(lb->lb_str);
1990-10-14 09:07:46 -03:00
lb->lb_str = NULL;
return;
}
}
for (i = 0; i < (int)N_TOKENS; i++) {
1997-04-29 18:03:06 -03:00
if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) {
if (Py_DebugFlag)
1990-10-14 09:07:46 -03:00
printf("Label %s is terminal %d.\n",
lb->lb_str, i);
lb->lb_type = i;
free(lb->lb_str);
1990-10-14 09:07:46 -03:00
lb->lb_str = NULL;
return;
}
}
printf("Can't translate NAME label '%s'\n", lb->lb_str);
return;
}
if (lb->lb_type == STRING) {
1998-04-10 19:09:39 -03:00
if (isalpha((int)(lb->lb_str[1])) || lb->lb_str[1] == '_') {
1994-08-30 05:27:36 -03:00
char *p;
char *src;
char *dest;
size_t name_len;
1997-04-29 18:03:06 -03:00
if (Py_DebugFlag)
1990-10-14 09:07:46 -03:00
printf("Label %s is a keyword\n", lb->lb_str);
lb->lb_type = NAME;
src = lb->lb_str + 1;
p = strchr(src, '\'');
1990-10-14 09:07:46 -03:00
if (p)
name_len = p - src;
else
name_len = strlen(src);
dest = malloc(name_len + 1);
strncpy(dest, src, name_len);
dest[name_len] = '\0';
free(lb->lb_str);
lb->lb_str = dest;
1990-10-14 09:07:46 -03:00
}
1991-10-20 17:20:00 -03:00
else if (lb->lb_str[2] == lb->lb_str[0]) {
1997-04-29 18:03:06 -03:00
int type = (int) PyToken_OneChar(lb->lb_str[1]);
1991-10-20 17:20:00 -03:00
if (type != OP) {
lb->lb_type = type;
free(lb->lb_str);
1991-10-20 17:20:00 -03:00
lb->lb_str = NULL;
}
else
printf("Unknown OP label %s\n",
lb->lb_str);
}
else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) {
1997-04-29 18:03:06 -03:00
int type = (int) PyToken_TwoChars(lb->lb_str[1],
1991-10-20 17:20:00 -03:00
lb->lb_str[2]);
if (type != OP) {
lb->lb_type = type;
free(lb->lb_str);
1991-10-20 17:20:00 -03:00
lb->lb_str = NULL;
1990-10-14 09:07:46 -03:00
}
else
1991-10-20 17:20:00 -03:00
printf("Unknown OP label %s\n",
1990-10-14 09:07:46 -03:00
lb->lb_str);
}
else if (lb->lb_str[2] && lb->lb_str[3] && lb->lb_str[4] == lb->lb_str[0]) {
int type = (int) PyToken_ThreeChars(lb->lb_str[1],
lb->lb_str[2],
lb->lb_str[3]);
if (type != OP) {
lb->lb_type = type;
free(lb->lb_str);
lb->lb_str = NULL;
}
else
printf("Unknown OP label %s\n",
lb->lb_str);
}
1991-10-20 17:20:00 -03:00
else
printf("Can't translate STRING label %s\n",
lb->lb_str);
1990-10-14 09:07:46 -03:00
}
else
1997-04-29 18:03:06 -03:00
printf("Can't translate label '%s'\n",
PyGrammar_LabelRepr(lb));
1990-10-14 09:07:46 -03:00
}