ยปCore Development>Code coverage>Parser/firstsets.c

Python code coverage for Parser/firstsets.c

#countcontent
1n/a
2n/a/* Computation of FIRST stets */
3n/a
4n/a#include "pgenheaders.h"
5n/a#include "grammar.h"
6n/a#include "token.h"
7n/a
8n/aextern int Py_DebugFlag;
9n/a
10n/a/* Forward */
11n/astatic void calcfirstset(grammar *, dfa *);
12n/a
13n/avoid
14n/aaddfirstsets(grammar *g)
15n/a{
16n/a int i;
17n/a dfa *d;
18n/a
19n/a if (Py_DebugFlag)
20n/a printf("Adding FIRST sets ...\n");
21n/a for (i = 0; i < g->g_ndfas; i++) {
22n/a d = &g->g_dfa[i];
23n/a if (d->d_first == NULL)
24n/a calcfirstset(g, d);
25n/a }
26n/a}
27n/a
28n/astatic void
29n/acalcfirstset(grammar *g, dfa *d)
30n/a{
31n/a int i, j;
32n/a state *s;
33n/a arc *a;
34n/a int nsyms;
35n/a int *sym;
36n/a int nbits;
37n/a static bitset dummy;
38n/a bitset result;
39n/a int type;
40n/a dfa *d1;
41n/a label *l0;
42n/a
43n/a if (Py_DebugFlag)
44n/a printf("Calculate FIRST set for '%s'\n", d->d_name);
45n/a
46n/a if (dummy == NULL)
47n/a dummy = newbitset(1);
48n/a if (d->d_first == dummy) {
49n/a fprintf(stderr, "Left-recursion for '%s'\n", d->d_name);
50n/a return;
51n/a }
52n/a if (d->d_first != NULL) {
53n/a fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n",
54n/a d->d_name);
55n/a }
56n/a d->d_first = dummy;
57n/a
58n/a l0 = g->g_ll.ll_label;
59n/a nbits = g->g_ll.ll_nlabels;
60n/a result = newbitset(nbits);
61n/a
62n/a sym = (int *)PyObject_MALLOC(sizeof(int));
63n/a if (sym == NULL)
64n/a Py_FatalError("no mem for new sym in calcfirstset");
65n/a nsyms = 1;
66n/a sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL);
67n/a
68n/a s = &d->d_state[d->d_initial];
69n/a for (i = 0; i < s->s_narcs; i++) {
70n/a a = &s->s_arc[i];
71n/a for (j = 0; j < nsyms; j++) {
72n/a if (sym[j] == a->a_lbl)
73n/a break;
74n/a }
75n/a if (j >= nsyms) { /* New label */
76n/a sym = (int *)PyObject_REALLOC(sym,
77n/a sizeof(int) * (nsyms + 1));
78n/a if (sym == NULL)
79n/a Py_FatalError(
80n/a "no mem to resize sym in calcfirstset");
81n/a sym[nsyms++] = a->a_lbl;
82n/a type = l0[a->a_lbl].lb_type;
83n/a if (ISNONTERMINAL(type)) {
84n/a d1 = PyGrammar_FindDFA(g, type);
85n/a if (d1->d_first == dummy) {
86n/a fprintf(stderr,
87n/a "Left-recursion below '%s'\n",
88n/a d->d_name);
89n/a }
90n/a else {
91n/a if (d1->d_first == NULL)
92n/a calcfirstset(g, d1);
93n/a mergebitset(result,
94n/a d1->d_first, nbits);
95n/a }
96n/a }
97n/a else if (ISTERMINAL(type)) {
98n/a addbit(result, a->a_lbl);
99n/a }
100n/a }
101n/a }
102n/a d->d_first = result;
103n/a if (Py_DebugFlag) {
104n/a printf("FIRST set for '%s': {", d->d_name);
105n/a for (i = 0; i < nbits; i++) {
106n/a if (testbit(result, i))
107n/a printf(" %s", PyGrammar_LabelRepr(&l0[i]));
108n/a }
109n/a printf(" }\n");
110n/a }
111n/a
112n/a PyObject_FREE(sym);
113n/a}