1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Grammar implementation */
|
|
|
|
|
2001-12-03 23:18:48 -04:00
|
|
|
#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"
|
|
|
|
|
2003-05-10 04:36:56 -03:00
|
|
|
#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 *
|
2000-07-22 16:20:54 -03:00
|
|
|
newgrammar(int start)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
grammar *g;
|
|
|
|
|
2006-04-11 02:39:14 -03:00
|
|
|
g = (grammar *)PyObject_MALLOC(sizeof(grammar));
|
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 *
|
2000-07-22 16:20:54 -03:00
|
|
|
adddfa(grammar *g, int type, char *name)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
dfa *d;
|
|
|
|
|
2006-04-11 02:39:14 -03:00
|
|
|
g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa,
|
|
|
|
sizeof(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;
|
2003-04-17 11:55:42 -03:00
|
|
|
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
|
2000-07-22 16:20:54 -03:00
|
|
|
addstate(dfa *d)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
state *s;
|
|
|
|
|
2006-04-11 02:39:14 -03:00
|
|
|
d->d_state = (state *)PyObject_REALLOC(d->d_state,
|
2006-04-10 03:42:25 -03:00
|
|
|
sizeof(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
|
2000-07-22 16:20:54 -03:00
|
|
|
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];
|
2006-04-11 02:39:14 -03:00
|
|
|
s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(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
|
2000-07-22 16:20:54 -03:00
|
|
|
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;
|
|
|
|
}
|
2006-04-11 02:39:14 -03:00
|
|
|
ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
|
2006-04-10 03:42:25 -03:00
|
|
|
sizeof(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;
|
2003-04-17 11:55:42 -03:00
|
|
|
lb->lb_str = strdup(str);
|
|
|
|
if (Py_DebugFlag)
|
2004-03-20 18:34:14 -04:00
|
|
|
printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels,
|
2003-04-17 11:55:42 -03:00
|
|
|
PyGrammar_LabelRepr(lb));
|
1990-10-14 09:07:46 -03:00
|
|
|
return lb - ll->ll_label;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same, but rather dies than adds */
|
|
|
|
|
|
|
|
int
|
2000-07-22 16:20:54 -03:00
|
|
|
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 */
|
2000-07-09 00:09:57 -03:00
|
|
|
static void translabel(grammar *, label *);
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
void
|
2000-07-22 16:20:54 -03:00
|
|
|
translatelabels(grammar *g)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
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
|
2000-07-22 16:20:54 -03:00
|
|
|
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;
|
2003-04-17 11:55:42 -03:00
|
|
|
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;
|
2003-04-17 11:55:42 -03:00
|
|
|
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) {
|
2005-12-19 02:05:18 -04:00
|
|
|
if (isalpha(Py_CHARMASK(lb->lb_str[1])) ||
|
|
|
|
lb->lb_str[1] == '_') {
|
1994-08-30 05:27:36 -03:00
|
|
|
char *p;
|
2003-04-17 11:55:42 -03:00
|
|
|
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;
|
2003-04-17 11:55:42 -03:00
|
|
|
src = lb->lb_str + 1;
|
|
|
|
p = strchr(src, '\'');
|
1990-10-14 09:07:46 -03:00
|
|
|
if (p)
|
2003-04-17 11:55:42 -03:00
|
|
|
name_len = p - src;
|
|
|
|
else
|
|
|
|
name_len = strlen(src);
|
2006-04-11 02:39:14 -03:00
|
|
|
dest = (char *)malloc(name_len + 1);
|
2003-04-17 11:55:42 -03:00
|
|
|
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;
|
2003-04-17 11:55:42 -03:00
|
|
|
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;
|
2003-04-17 11:55:42 -03:00
|
|
|
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);
|
|
|
|
}
|
2000-08-24 17:11:32 -03:00
|
|
|
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;
|
2003-04-17 11:55:42 -03:00
|
|
|
free(lb->lb_str);
|
2000-08-24 17:11:32 -03:00
|
|
|
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
|
|
|
}
|