1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
2000-06-30 20:50:40 -03:00
|
|
|
Copyright (c) 2000, BeOpen.com.
|
|
|
|
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
|
|
|
|
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
See the file "Misc/COPYRIGHT" for information on usage and
|
|
|
|
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
1991-02-19 08:39:46 -04:00
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Grammar implementation */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "pgenheaders.h"
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "assert.h"
|
|
|
|
#include "token.h"
|
|
|
|
#include "grammar.h"
|
|
|
|
|
1997-04-29 18:03:06 -03:00
|
|
|
extern int Py_DebugFlag;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
grammar *
|
|
|
|
newgrammar(start)
|
|
|
|
int start;
|
|
|
|
{
|
|
|
|
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;
|
1994-12-30 11:46:02 -04:00
|
|
|
g->g_accel = 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
|
|
|
dfa *
|
|
|
|
adddfa(g, type, name)
|
|
|
|
grammar *g;
|
|
|
|
int type;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
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 = 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(d)
|
|
|
|
dfa *d;
|
|
|
|
{
|
|
|
|
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;
|
1994-12-30 11:46:02 -04:00
|
|
|
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(d, from, to, lbl)
|
|
|
|
dfa *d;
|
|
|
|
int lbl;
|
|
|
|
{
|
|
|
|
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(ll, type, str)
|
|
|
|
labellist *ll;
|
|
|
|
int type;
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
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 = str; /* XXX strdup(str) ??? */
|
|
|
|
return lb - ll->ll_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same, but rather dies than adds */
|
|
|
|
|
|
|
|
int
|
|
|
|
findlabel(ll, type, str)
|
|
|
|
labellist *ll;
|
|
|
|
int type;
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
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 */
|
1997-04-29 18:03:06 -03:00
|
|
|
static void translabel Py_PROTO((grammar *, label *));
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
translatelabels(g)
|
|
|
|
grammar *g;
|
|
|
|
{
|
|
|
|
int i;
|
1994-12-30 11:46:02 -04:00
|
|
|
|
1996-12-30 12:17:54 -04:00
|
|
|
#ifdef Py_DEBUG
|
1990-12-20 11:06:42 -04:00
|
|
|
printf("Translating labels ...\n");
|
1994-12-30 11:46:02 -04:00
|
|
|
#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(g, lb)
|
|
|
|
grammar *g;
|
|
|
|
label *lb;
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
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;
|
|
|
|
lb->lb_str++;
|
|
|
|
p = strchr(lb->lb_str, '\'');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
}
|
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;
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
}
|