| 1 |
|
|---|
| 2 | /* Grammar implementation */
|
|---|
| 3 |
|
|---|
| 4 | #include "Python.h"
|
|---|
| 5 | #include "pgenheaders.h"
|
|---|
| 6 |
|
|---|
| 7 | #include <ctype.h>
|
|---|
| 8 |
|
|---|
| 9 | #include "token.h"
|
|---|
| 10 | #include "grammar.h"
|
|---|
| 11 |
|
|---|
| 12 | #ifdef RISCOS
|
|---|
| 13 | #include <unixlib.h>
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | extern int Py_DebugFlag;
|
|---|
| 17 |
|
|---|
| 18 | grammar *
|
|---|
| 19 | newgrammar(int start)
|
|---|
| 20 | {
|
|---|
| 21 | grammar *g;
|
|---|
| 22 |
|
|---|
| 23 | g = (grammar *)PyObject_MALLOC(sizeof(grammar));
|
|---|
| 24 | if (g == NULL)
|
|---|
| 25 | Py_FatalError("no mem for new grammar");
|
|---|
| 26 | g->g_ndfas = 0;
|
|---|
| 27 | g->g_dfa = NULL;
|
|---|
| 28 | g->g_start = start;
|
|---|
| 29 | g->g_ll.ll_nlabels = 0;
|
|---|
| 30 | g->g_ll.ll_label = NULL;
|
|---|
| 31 | g->g_accel = 0;
|
|---|
| 32 | return g;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | dfa *
|
|---|
| 36 | adddfa(grammar *g, int type, char *name)
|
|---|
| 37 | {
|
|---|
| 38 | dfa *d;
|
|---|
| 39 |
|
|---|
| 40 | g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa,
|
|---|
| 41 | sizeof(dfa) * (g->g_ndfas + 1));
|
|---|
| 42 | if (g->g_dfa == NULL)
|
|---|
| 43 | Py_FatalError("no mem to resize dfa in adddfa");
|
|---|
| 44 | d = &g->g_dfa[g->g_ndfas++];
|
|---|
| 45 | d->d_type = type;
|
|---|
| 46 | d->d_name = strdup(name);
|
|---|
| 47 | d->d_nstates = 0;
|
|---|
| 48 | d->d_state = NULL;
|
|---|
| 49 | d->d_initial = -1;
|
|---|
| 50 | d->d_first = NULL;
|
|---|
| 51 | return d; /* Only use while fresh! */
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | int
|
|---|
| 55 | addstate(dfa *d)
|
|---|
| 56 | {
|
|---|
| 57 | state *s;
|
|---|
| 58 |
|
|---|
| 59 | d->d_state = (state *)PyObject_REALLOC(d->d_state,
|
|---|
| 60 | sizeof(state) * (d->d_nstates + 1));
|
|---|
| 61 | if (d->d_state == NULL)
|
|---|
| 62 | Py_FatalError("no mem to resize state in addstate");
|
|---|
| 63 | s = &d->d_state[d->d_nstates++];
|
|---|
| 64 | s->s_narcs = 0;
|
|---|
| 65 | s->s_arc = NULL;
|
|---|
| 66 | s->s_lower = 0;
|
|---|
| 67 | s->s_upper = 0;
|
|---|
| 68 | s->s_accel = NULL;
|
|---|
| 69 | s->s_accept = 0;
|
|---|
| 70 | return s - d->d_state;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | void
|
|---|
| 74 | addarc(dfa *d, int from, int to, int lbl)
|
|---|
| 75 | {
|
|---|
| 76 | state *s;
|
|---|
| 77 | arc *a;
|
|---|
| 78 |
|
|---|
| 79 | assert(0 <= from && from < d->d_nstates);
|
|---|
| 80 | assert(0 <= to && to < d->d_nstates);
|
|---|
| 81 |
|
|---|
| 82 | s = &d->d_state[from];
|
|---|
| 83 | s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
|
|---|
| 84 | if (s->s_arc == NULL)
|
|---|
| 85 | Py_FatalError("no mem to resize arc list in addarc");
|
|---|
| 86 | a = &s->s_arc[s->s_narcs++];
|
|---|
| 87 | a->a_lbl = lbl;
|
|---|
| 88 | a->a_arrow = to;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | int
|
|---|
| 92 | addlabel(labellist *ll, int type, char *str)
|
|---|
| 93 | {
|
|---|
| 94 | int i;
|
|---|
| 95 | label *lb;
|
|---|
| 96 |
|
|---|
| 97 | for (i = 0; i < ll->ll_nlabels; i++) {
|
|---|
| 98 | if (ll->ll_label[i].lb_type == type &&
|
|---|
| 99 | strcmp(ll->ll_label[i].lb_str, str) == 0)
|
|---|
| 100 | return i;
|
|---|
| 101 | }
|
|---|
| 102 | ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
|
|---|
| 103 | sizeof(label) * (ll->ll_nlabels + 1));
|
|---|
| 104 | if (ll->ll_label == NULL)
|
|---|
| 105 | Py_FatalError("no mem to resize labellist in addlabel");
|
|---|
| 106 | lb = &ll->ll_label[ll->ll_nlabels++];
|
|---|
| 107 | lb->lb_type = type;
|
|---|
|
|---|