static void printssdfa(int xx_nstates, ss_state *xx_state, int nbits, labellist *ll, char *msg) { int i, ibit, iarc; ss_state *yy; ss_arc *zz; printf("Subset DFA %s\n", msg); for (i = 0; i < xx_nstates; i++) { yy = &xx_state[i]; if (yy->ss_deleted) continue; printf(" Subset %d", i); if (yy->ss_finish) printf(" (finish)"); printf(" { "); for (ibit = 0; ibit < nbits; ibit++) { if (testbit(yy->ss_ss, ibit)) printf("%d ", ibit); } printf("}\n"); for (iarc = 0; iarc < yy->ss_narcs; iarc++) { zz = &yy->ss_arc[iarc]; printf(" Arc to state %d, label %s\n", zz->sa_arrow, PyGrammar_LabelRepr( &ll->ll_label[zz->sa_label])); } } }
static void dumpstate(labellist *ll, nfa *nf, int istate) { nfastate *st; int i; nfaarc *ar; printf("%c%2d%c", istate == nf->nf_start ? '*' : ' ', istate, istate == nf->nf_finish ? '.' : ' '); st = &nf->nf_state[istate]; ar = st->st_arc; for (i = 0; i < st->st_narcs; i++) { if (i > 0) printf("\n "); printf("-> %2d %s", ar->ar_arrow, PyGrammar_LabelRepr(&ll->ll_label[ar->ar_label])); ar++; } printf("\n"); }
int addlabel(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; } PyMem_RESIZE(ll->ll_label, label, ll->ll_nlabels + 1); if (ll->ll_label == NULL) Py_FatalError("no mem to resize labellist in addlabel"); 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)); return lb - ll->ll_label; }
int addlabel(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; } ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label, sizeof(label) * (ll->ll_nlabels + 1)); if (ll->ll_label == NULL) Py_FatalError("no mem to resize labellist in addlabel"); lb = &ll->ll_label[ll->ll_nlabels++]; lb->lb_type = type; lb->lb_str = strdup(str); if (Py_DebugFlag) printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels, PyGrammar_LabelRepr(lb)); return lb - ll->ll_label; }
static void translabel(grammar *g, label *lb) { int i; if (Py_DebugFlag) printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb)); 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) { if (Py_DebugFlag) printf( "Label %s is non-terminal %d.\n", lb->lb_str, g->g_dfa[i].d_type); lb->lb_type = g->g_dfa[i].d_type; free(lb->lb_str); lb->lb_str = NULL; return; } } for (i = 0; i < (int)N_TOKENS; i++) { if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) { if (Py_DebugFlag) printf("Label %s is terminal %d.\n", lb->lb_str, i); lb->lb_type = i; free(lb->lb_str); lb->lb_str = NULL; return; } } printf("Can't translate NAME label '%s'\n", lb->lb_str); return; } if (lb->lb_type == STRING) { if (isalpha(Py_CHARMASK(lb->lb_str[1])) || lb->lb_str[1] == '_') { char *p; char *src; char *dest; size_t name_len; if (Py_DebugFlag) printf("Label %s is a keyword\n", lb->lb_str); lb->lb_type = NAME; src = lb->lb_str + 1; p = strchr(src, '\''); if (p) name_len = p - src; else name_len = strlen(src); dest = (char *)malloc(name_len + 1); if (!dest) { printf("Can't alloc dest '%s'\n", src); return; } strncpy(dest, src, name_len); dest[name_len] = '\0'; free(lb->lb_str); lb->lb_str = dest; } else if (lb->lb_str[2] == lb->lb_str[0]) { int type = (int) PyToken_OneChar(lb->lb_str[1]); 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); } else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) { int type = (int) PyToken_TwoChars(lb->lb_str[1], lb->lb_str[2]); 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); } 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); } else printf("Can't translate STRING label %s\n", lb->lb_str); } else printf("Can't translate label '%s'\n", PyGrammar_LabelRepr(lb)); }
static void calcfirstset(grammar *g, dfa *d) { int i, j; state *s; arc *a; int nsyms; int *sym; int nbits; static bitset dummy; bitset result; int type; dfa *d1; label *l0; if (Py_DebugFlag) printf("Calculate FIRST set for '%s'\n", d->d_name); if (dummy == NULL) dummy = newbitset(1); if (d->d_first == dummy) { fprintf(stderr, "Left-recursion for '%s'\n", d->d_name); return; } if (d->d_first != NULL) { fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n", d->d_name); } d->d_first = dummy; l0 = g->g_ll.ll_label; nbits = g->g_ll.ll_nlabels; result = newbitset(nbits); sym = (int *)PyObject_MALLOC(sizeof(int)); if (sym == NULL) Py_FatalError("no mem for new sym in calcfirstset"); nsyms = 1; sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL); s = &d->d_state[d->d_initial]; for (i = 0; i < s->s_narcs; i++) { a = &s->s_arc[i]; for (j = 0; j < nsyms; j++) { if (sym[j] == a->a_lbl) break; } if (j >= nsyms) { /* New label */ sym = (int *)PyObject_REALLOC(sym, sizeof(int) * (nsyms + 1)); if (sym == NULL) Py_FatalError( "no mem to resize sym in calcfirstset"); sym[nsyms++] = a->a_lbl; type = l0[a->a_lbl].lb_type; if (ISNONTERMINAL(type)) { d1 = PyGrammar_FindDFA(g, type); if (d1->d_first == dummy) { fprintf(stderr, "Left-recursion below '%s'\n", d->d_name); } else { if (d1->d_first == NULL) calcfirstset(g, d1); mergebitset(result, d1->d_first, nbits); } } else if (ISTERMINAL(type)) { addbit(result, a->a_lbl); } } } d->d_first = result; if (Py_DebugFlag) { printf("FIRST set for '%s': {", d->d_name); for (i = 0; i < nbits; i++) { if (testbit(result, i)) printf(" %s", PyGrammar_LabelRepr(&l0[i])); } printf(" }\n"); } PyObject_FREE(sym); }