cpython/Parser/grammar.c

225 lines
4.6 KiB
C
Raw Normal View History

1991-02-19 12:39:46 +00:00
1990-10-14 12:07:46 +00:00
/* Grammar implementation */
1990-12-20 15:06:42 +00:00
#include "pgenheaders.h"
1990-10-14 12:07:46 +00:00
#include <ctype.h>
#include "assert.h"
#include "token.h"
#include "grammar.h"
1997-04-29 21:03:06 +00:00
extern int Py_DebugFlag;
1990-10-14 12:07:46 +00:00
grammar *
newgrammar(int start)
1990-10-14 12:07:46 +00:00
{
grammar *g;
1997-04-29 21:03:06 +00:00
g = PyMem_NEW(grammar, 1);
1990-10-14 12:07:46 +00:00
if (g == NULL)
1997-04-29 21:03:06 +00:00
Py_FatalError("no mem for new grammar");
1990-10-14 12:07:46 +00: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 12:07:46 +00:00
return g;
}
dfa *
adddfa(grammar *g, int type, char *name)
1990-10-14 12:07:46 +00:00
{
dfa *d;
1997-04-29 21:03:06 +00:00
PyMem_RESIZE(g->g_dfa, dfa, g->g_ndfas + 1);
1990-10-14 12:07:46 +00:00
if (g->g_dfa == NULL)
1997-04-29 21:03:06 +00:00
Py_FatalError("no mem to resize dfa in adddfa");
1990-10-14 12:07:46 +00:00
d = &g->g_dfa[g->g_ndfas++];
d->d_type = type;
d->d_name = name;
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 12:07:46 +00:00
{
state *s;
1997-04-29 21:03:06 +00:00
PyMem_RESIZE(d->d_state, state, d->d_nstates + 1);
1990-10-14 12:07:46 +00:00
if (d->d_state == NULL)
1997-04-29 21:03:06 +00:00
Py_FatalError("no mem to resize state in addstate");
1990-10-14 12:07:46 +00: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 12:07:46 +00:00
return s - d->d_state;
}
void
addarc(dfa *d, int from, int to, int lbl)
1990-10-14 12:07:46 +00: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 21:03:06 +00:00
PyMem_RESIZE(s->s_arc, arc, s->s_narcs + 1);
1990-10-14 12:07:46 +00:00
if (s->s_arc == NULL)
1997-04-29 21:03:06 +00:00
Py_FatalError("no mem to resize arc list in addarc");
1990-10-14 12:07:46 +00: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 12:07:46 +00: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 21:03:06 +00:00
PyMem_RESIZE(ll->ll_label, label, ll->ll_nlabels + 1);
1990-10-14 12:07:46 +00:00
if (ll->ll_label == NULL)
1997-04-29 21:03:06 +00:00
Py_FatalError("no mem to resize labellist in addlabel");
1990-10-14 12:07:46 +00:00
lb = &ll->ll_label[ll->ll_nlabels++];
lb->lb_type = type;
lb->lb_str = str; /* XXX strdup(str) ??? */
return lb - ll->ll_label;
}
/* Same, but rather dies than adds */
int
findlabel(labellist *ll, int type, char *str)
1990-10-14 12:07:46 +00: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 21:03:06 +00:00
Py_FatalError("grammar.c:findlabel()");
1996-12-02 18:27:33 +00:00
return 0; /* Make gcc -Wall happy */
1990-10-14 12:07:46 +00:00
}
1990-12-20 15:06:42 +00:00
/* Forward */
static void translabel(grammar *, label *);
1990-12-20 15:06:42 +00:00
void
translatelabels(grammar *g)
1990-12-20 15:06:42 +00:00
{
int i;
1996-12-30 16:17:54 +00:00
#ifdef Py_DEBUG
1990-12-20 15:06:42 +00:00
printf("Translating labels ...\n");
#endif
1990-12-20 15:06:42 +00: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 12:07:46 +00:00
static void
translabel(grammar *g, label *lb)
1990-10-14 12:07:46 +00:00
{
int i;
1997-04-29 21:03:06 +00:00
if (Py_DebugFlag)
printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb));
1990-10-14 12:07:46 +00: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 21:03:06 +00:00
if (Py_DebugFlag)
printf(
"Label %s is non-terminal %d.\n",
lb->lb_str,
g->g_dfa[i].d_type);
1990-10-14 12:07:46 +00:00
lb->lb_type = g->g_dfa[i].d_type;
lb->lb_str = NULL;
return;
}
}
for (i = 0; i < (int)N_TOKENS; i++) {
1997-04-29 21:03:06 +00:00
if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) {
if (Py_DebugFlag)
1990-10-14 12:07:46 +00:00
printf("Label %s is terminal %d.\n",
lb->lb_str, i);
lb->lb_type = i;
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 22:09:39 +00:00
if (isalpha((int)(lb->lb_str[1])) || lb->lb_str[1] == '_') {
1994-08-30 08:27:36 +00:00
char *p;
1997-04-29 21:03:06 +00:00
if (Py_DebugFlag)
1990-10-14 12:07:46 +00:00
printf("Label %s is a keyword\n", lb->lb_str);
lb->lb_type = NAME;
lb->lb_str++;
p = strchr(lb->lb_str, '\'');
if (p)
*p = '\0';
}
1991-10-20 20:20:00 +00:00
else if (lb->lb_str[2] == lb->lb_str[0]) {
1997-04-29 21:03:06 +00:00
int type = (int) PyToken_OneChar(lb->lb_str[1]);
1991-10-20 20:20:00 +00:00
if (type != OP) {
lb->lb_type = type;
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 21:03:06 +00:00
int type = (int) PyToken_TwoChars(lb->lb_str[1],
1991-10-20 20:20:00 +00:00
lb->lb_str[2]);
if (type != OP) {
lb->lb_type = type;
lb->lb_str = NULL;
1990-10-14 12:07:46 +00:00
}
else
1991-10-20 20:20:00 +00:00
printf("Unknown OP label %s\n",
1990-10-14 12:07:46 +00: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;
lb->lb_str = NULL;
}
else
printf("Unknown OP label %s\n",
lb->lb_str);
}
1991-10-20 20:20:00 +00:00
else
printf("Can't translate STRING label %s\n",
lb->lb_str);
1990-10-14 12:07:46 +00:00
}
else
1997-04-29 21:03:06 +00:00
printf("Can't translate label '%s'\n",
PyGrammar_LabelRepr(lb));
1990-10-14 12:07:46 +00:00
}